我想将数据从一个工作簿发送到另一工作簿(工作簿A将数据发送到工作簿B)。
我编写了一个代码,用于打开工作簿B,并搜索在工作簿A的某些单元格中给出的值。
我唯一需要帮助的是从工作簿A复制一个范围(范围:G71至DI71,包括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
msgbox仅用于检查我是否获得了良好的行和列。
我希望有人能提供帮助。
答案 0 :(得分:0)
我不知道您的第二个工作簿或工作表是什么,因此您将必须填写“?”。我还要将要复制的工作簿放在Sheets(“ Blad1”)的前面。但是,这是使用变量的基本复制粘贴。
Sheets(“ Blad1”)。Range(“ G13:DI13”)。复制目标:= Workbook(?)。Sheets(?)。Cells(rFind,cFind)
答案 1 :(得分:0)
只需在您的代码后添加它:
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)