VBA 用户表单单张链接库存条目

标签 vba excel

我有一个用于库存目的的 Excel 表/用户表单。

我的用户表单的第一个功能是从表单上的文本框中获取连接器和配对的数据,并将其放在我告诉它的行/单元格中的工作表上。该代码工作正常。

第二个功能是交叉引用连接器和配合,以便当我搜索或更改零件数据时,我可以获得最新信息。
我通过在创建连接器和配合时将单元格地址存储在第 1 行中来做到这一点。然后,当我点击“确认”时,地址将被复制到它们自己的行中的指定单元格中,并添加了“=”符号,以创建引用公式。

表格中的一行用于后面带有配合引用的连接器,下一行用于后面带有连接器引用的配合。然后我继续下一行并再次执行此操作。

我只能用一对零件号来做到这一点,之后就没有其他零件号了。
我究竟做错了什么?

代码:

    Private Sub XREFCONFIRM1_Click()

    Dim iRow1 As Long
    Dim iRow2 As Long
    Dim ws As Worksheet
    Set ws = Worksheets("1STDRAFT")
    row_number = 4

    iRow1 = ws.Cells.Find(What:=PART1.Text, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row

    iRow2 = ws.Cells.Find(What:=PART2.Text, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row

        row_number = row_number + 1

        item_in_review = Sheets("1STDRAFT").Range("A" & row_number).Value

    With ws
       If item_in_review = PART1.Text Then

      .Cells(iRow1, 15).Value = "=" & Sheets("1STDRAFT").Cells(1, 15).Value
      .Cells(iRow1, 16).Value = "=" & Sheets("1STDRAFT").Cells(1, 16).Value
      .Cells(iRow1, 17).Value = "=" & Sheets("1STDRAFT").Cells(1, 17).Value
      .Cells(iRow1, 18).Value = "=" & Sheets("1STDRAFT").Cells(1, 18).Value
      .Cells(iRow1, 19).Value = "=" & Sheets("1STDRAFT").Cells(1, 19).Value
      .Cells(iRow1, 20).Value = "=" & Sheets("1STDRAFT").Cells(1, 20).Value
      .Cells(iRow1, 21).Value = "=" & Sheets("1STDRAFT").Cells(1, 21).Value
      .Cells(iRow1, 22).Value = "=" & Sheets("1STDRAFT").Cells(1, 22).Value
      .Cells(iRow1, 23).Value = "=" & Sheets("1STDRAFT").Cells(1, 23).Value
      .Cells(iRow1, 24).Value = "=" & Sheets("1STDRAFT").Cells(1, 24).Value
      .Cells(iRow1, 25).Value = "=" & Sheets("1STDRAFT").Cells(1, 25).Value

     'With ws
            'If item_in_review = PART2.Text Then
      .Cells(iRow2, 15).Value = "=" & Sheets("1STDRAFT").Cells(1, 1).Value
      .Cells(iRow2, 16).Value = "=" & Sheets("1STDRAFT").Cells(1, 2).Value
      .Cells(iRow2, 17).Value = "=" & Sheets("1STDRAFT").Cells(1, 3).Value
      .Cells(iRow2, 18).Value = "=" & Sheets("1STDRAFT").Cells(1, 5).Value
      .Cells(iRow2, 19).Value = "=" & Sheets("1STDRAFT").Cells(1, 8).Value
      .Cells(iRow2, 20).Value = "=" & Sheets("1STDRAFT").Cells(1, 9).Value
      .Cells(iRow2, 21).Value = "=" & Sheets("1STDRAFT").Cells(1, 10).Value
      .Cells(iRow2, 22).Value = "=" & Sheets("1STDRAFT").Cells(1, 11).Value
      .Cells(iRow2, 23).Value = "=" & Sheets("1STDRAFT").Cells(1, 12).Value
      .Cells(iRow2, 24).Value = "=" & Sheets("1STDRAFT").Cells(1, 13).Value
      .Cells(iRow2, 25).Value = "=" & Sheets("1STDRAFT").Cells(1, 14).Value

            End If
        End With


    End Sub

驱动器文件夹链接:https://drive.google.com/drive/folders/0Bz4rZ9ZqoU0tbkdaQjBBZVgyRTA?usp=sharing

文件直接链接:https://drive.google.com/file/d/0Bz4rZ9ZqoU0tVmJKa0xlUXBNcGc/view?usp=sharing

图片链接:https://docs.google.com/spreadsheets/d/1Tzi_2gaaXypBen2Ls7p21USat8uS_qhcRDwmGQLCcXE/edit?usp=sharing

最佳答案

对于任何想知道的人来说,这就是我的代码现在的样子。我将更新我提供的一些链接,以帮助任何想要使用我的表格的人。

   Private Sub XREFCONFIRM1_Click()

    Dim iRow1 As Long
    Dim iRow2 As Long
    Dim ws As Worksheet
    Set ws = Worksheets("1STDRAFT")
    row_number = 4

    iRow1 = ws.Cells.Find(What:=PART1.Text, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row

    iRow2 = ws.Cells.Find(What:=PART2.Text, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row

    Do  
    row_number = row_number + 1

        item_in_review = Sheets("1STDRAFT").Range("A" & row_number).Value

    With ws
       If item_in_review = PART1.Text Then

      .Cells(iRow1, 15).Value = "=" & Sheets("1STDRAFT").Cells(1, 15).Value
      .Cells(iRow1, 16).Value = "=" & Sheets("1STDRAFT").Cells(1, 16).Value
      .Cells(iRow1, 17).Value = "=" & Sheets("1STDRAFT").Cells(1, 17).Value
      .Cells(iRow1, 18).Value = "=" & Sheets("1STDRAFT").Cells(1, 18).Value
      .Cells(iRow1, 19).Value = "=" & Sheets("1STDRAFT").Cells(1, 19).Value
      .Cells(iRow1, 20).Value = "=" & Sheets("1STDRAFT").Cells(1, 20).Value
      .Cells(iRow1, 21).Value = "=" & Sheets("1STDRAFT").Cells(1, 21).Value
      .Cells(iRow1, 22).Value = "=" & Sheets("1STDRAFT").Cells(1, 22).Value
      .Cells(iRow1, 23).Value = "=" & Sheets("1STDRAFT").Cells(1, 23).Value
      .Cells(iRow1, 24).Value = "=" & Sheets("1STDRAFT").Cells(1, 24).Value
      .Cells(iRow1, 25).Value = "=" & Sheets("1STDRAFT").Cells(1, 25).Value

     'With ws
            'If item_in_review = PART2.Text Then
      .Cells(iRow2, 15).Value = "=" & Sheets("1STDRAFT").Cells(1, 1).Value
      .Cells(iRow2, 16).Value = "=" & Sheets("1STDRAFT").Cells(1, 2).Value
      .Cells(iRow2, 17).Value = "=" & Sheets("1STDRAFT").Cells(1, 3).Value
      .Cells(iRow2, 18).Value = "=" & Sheets("1STDRAFT").Cells(1, 5).Value
      .Cells(iRow2, 19).Value = "=" & Sheets("1STDRAFT").Cells(1, 8).Value
      .Cells(iRow2, 20).Value = "=" & Sheets("1STDRAFT").Cells(1, 9).Value
      .Cells(iRow2, 21).Value = "=" & Sheets("1STDRAFT").Cells(1, 10).Value
      .Cells(iRow2, 22).Value = "=" & Sheets("1STDRAFT").Cells(1, 11).Value
      .Cells(iRow2, 23).Value = "=" & Sheets("1STDRAFT").Cells(1, 12).Value
      .Cells(iRow2, 24).Value = "=" & Sheets("1STDRAFT").Cells(1, 13).Value
      .Cells(iRow2, 25).Value = "=" & Sheets("1STDRAFT").Cells(1, 14).Value

            End If
        End With
          Loop Until item_in_review = ""

    End Sub

关于VBA 用户表单单张链接库存条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808222/

相关文章:

vba - 在每个工作表的基础上存储设置的最佳方式是什么?

vba - 为每个唯一代理创建一个新工作表,并将所有数据 move 到每个工作表

vba - 控制标题文本显示得更小

excel - 下标超出范围 IF ISERROR 问题

vba - 将 now() 转换为 MS Access VBA 中的特定格式

excel - 在 Excel 中启动一个新的浏览器窗口(小)

ms-access - 如何搜索 VBA 代码文件

.net - 使用OLEDB读取文件名中带有额外点的Excel csv文件

C++ 64 位 Excel 加载项 (XLL) 无法在 64 位 Excel 中正确加载

vba - 打开工作簿时,Excel 宏刷新所有数据连接表和数据透视表,然后将数据透视表导出到 csv