【问题标题】:Excel VBA use found column and row to paste dataExcel VBA 使用找到的列和行来粘贴数据
【发布时间】:2018-12-15 00:23:40
【问题描述】:

我想将数据从一个工作簿发送到另一个工作簿(工作簿 A 将数据发送到工作簿 B)。

我编写了一个代码,可以打开工作簿 B 并搜索工作簿 A 中某些单元格中给出的值。

我唯一不能做和需要帮助的事情是从工作簿 A 复制一个范围(范围:G71 到并包括 DI71)并将此范围粘贴到工作簿 B 中找到的列和行中这段代码

我到现在为止的代码:

Private Sub CommandButton1_Click()
Dim Fstring As String
Dim Pstring As String
Dim Bureauplanning As String
Dim wb As Workbook
Dim cFind As Range
Dim rFind As Range
Dim rngc As Range
Dim rngp As Range

    'cell with data to find
    Fstring = Range("G13").Value
    Pstring = Range("A2").Value


Bureauplanning = "\\nel-data\Document\Planning\Bureauplanning.xlsm"
    Workbooks.Open (Bureauplanning)

With Sheets("Blad1").Range("G13:DI13")
Set rFind = .find(What:=Fstring, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
    MsgBox rFind.Column

End If
End With

With Sheets("Blad1").Range("F:F")
Set cFind = .find(What:=Pstring, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cFind Is Nothing Then
    MsgBox cFind.Row

End If
End With

End Sub

msgboxes 只是用于检查我是否得到了好的行和列。

希望有人能帮忙。

【问题讨论】:

  • 如果例如在 I13 中找到 Fstring 而在 F17 中找到 Pstring,您能否准确地说出您想要复制的内容?表 Blad1 上的 Fstring 和 Pstring 值的范围是什么?以及 Bureauplanning 中工作表的名称(可能是 ActiveSheet)?您的数据从 F 列的哪一行开始?您想在范围 H13:DI13 中找到与 G13 中相同的值还是这些不同的工作表?
  • 如果rFind 不是Nothing,你能不能只在With 语句中这样做:.Copy rFind.Address
  • 我破解了它,但它几乎是我的死亡。令人难以置信的是,在我在第一条评论中写了这么多之后,你实际上发布了(几乎没有)足够的信息。祝你好运。

标签: excel vba


【解决方案1】:

我不知道您的第二个工作簿或工作表是什么,所以您必须填写“?”。我还将您正在复制的工作簿放在 Sheets("Blad1") 前面。但是,这里是使用变量的基本复制粘贴。

Sheets("Blad1").Range("G13:DI13").Copy Destination:=Workbook(?).Sheets(?).Cells(rFind, cFind)

【讨论】:

    【解决方案2】:

    复制方法

    只需在您的代码后添加:

    With Sheets("Blad1")
        Range("G71:DI71").Copy .Cells(cFind.Row, rFind.Column) _
            .Resize(, .Range("G71:DI71").Columns.Count)
    End With
    

    如果你想检查一个工作簿是否打开,你可以使用这个:

      Const wbName As String = "Bureauplanning.xls"
      Dim wb As Workbook
    
     ' Check if workbook is open.
      For Each wb In Workbooks
        If wb.Name = wbName Then Exit For
      Next
      If wb Is Nothing Then Set wb = Workbooks.Open(Bureauplanning)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多