【问题标题】:How to copy data from one worksheet to another to a specific row如何将数据从一个工作表复制到另一个工作表到特定行
【发布时间】:2019-02-23 19:13:37
【问题描述】:

对所有人来说, 提前感谢您的宝贵时间。

我们已经有了工作代码,可以在 excel 中使用 vb 将数据从一个 wrksht 移动到另一个 wrksht。

我们使用:

Set lastrow = Sheets ("SR log").Cells(Rows.Count, 1).End(x1UP)
    With LastRow

这会将我们选择的数据放在工作表 2 的最后一个打开的行中

是否有可能,而不是最后一行,从第一张纸上搜索已经在第二张纸上的参考号,比如说单元格 G3。使用单元格 g3 中第一张纸上的信息并在第二张纸上查找。

一旦找到该行(第一张表中的 G3 数据将在第二张表的 A 列中)

现在将数据应用到适用的那一行。

任何帮助将不胜感激。

2/22/19

这是我的回应。

感谢您抽出宝贵时间

我已经把一些东西放在一起,但想在执行之前运行它

[代码]

 Private Sub CommandButton2_Click()




    Workbooks.Open Filename:="G:\General\COVER  SHEET_Protective\Protective     Packaging Order Log.xlsm", Password:="PP", WriteResPassword:="PP"





   Dim FoundRow As Variant
   FoundRow = Application.Match(Sheets(1).Range("G3"), Sheets(2).Columns(1), 0)
    If IsNumeric(FoundRow) Then
    With FoundRow
        ' found, use FoundRow like LastRow before
    End With
   Else
    ' not found :(
    End If



 .Offset(1).Font.Size = 14

                .Offset(1, 9) = ws.[I10]
                .Offset(1, 10) = ws.[I11]

     End Sub

[/代码]

我对这一行有点不确定

[代码]

 Application.Match(Sheets(1).Range("G3"), Sheets(2).Columns(1), 0)

[/代码]

第一个工作簿上的匹配表 1 称为工作表

在第二个工作簿上进行搜索的第一个工作簿上 柱子 该工作表称为 orderlog

谢谢

【问题讨论】:

  • 第一次使用,所以我不确定我是否将响应放在正确的位置。
  • 您的代码格式可读性很差 :) 但使用附加信息或代码编辑您的问题是正确的方法。我编辑了我的答案,希望能给你一个详细的帮助。如果有帮助,请考虑将其标记为答案,如下所述:What should I do when someone answers my question?。如果没有帮助,请直接在下方评论。

标签: excel vba


【解决方案1】:

您可以找到与Application.Match匹配的行:

Private Sub CommandButton2_Click()
    Dim wb1 As Workbook         ' first workbook
    Dim wb2 As Workbook         ' second workbook
    Dim wsCheck As Worksheet    ' sheet in the first workbook
    Dim wsOrderlog As Worksheet ' sheet in the second workbook

    ' address the first workbook and its sheet
    ' if this VBA-code is in the frist workbook, it's "ThisWorkbook"
    Set wb1 = ThisWorkbook
    Set wsCheck = wb1.Worksheets("Worksheet")

    ' check, if second workbook is already open
    For Each wb2 In Workbooks
        If wb2.Name = "Protective Packaging Order Log.xlsm" Then Exit For
    Next wb2

    ' if not already open, then open it, and address its sheet also
    If wb2 Is Nothing Then
        Set wb2 = Workbooks.Open( _
            Filename:="G:\General\COVERSHEET_Protective\Protective Packaging Order Log.xlsm", _
            Password:="PP", _
            WriteResPassword:="PP")
    End If
    Set wsOrderlog = wb2.Worksheets("orderlog")

    ' search a value from the first workbook's sheet within second workbook's sheet
    Dim FoundRow As Variant
    FoundRow = Application.Match(wsCheck.Range("G3").Value, wsOrderlog.Range("A:A"), 0)
    If IsNumeric(FoundRow) Then ' if found
        ' please adapt to your needs:
        wsOrderlog.Cells(FoundRow, 1).Font.Size = 14
        wsOrderlog.Cells(FoundRow, 9).Value = wsCheck.Range("I10").Value
        wsOrderlog.Cells(FoundRow, 10).Value = wsCheck.Range("I11").Value
    Else
        MsgBox "Sorry, the value in cell G3" & vbLf & _
            wsCheck.Range("G3").Value & vbLf & _
            "could not be found in orderlog column A."
    End If

    ' close the second workbook (Excel will ask, if to save)
    wb2.Close
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多