下面的代码是从“data.xlsx”excel 工作簿运行的,应该提示用户打开 excel 工作簿文件并将“data.xlsx”中的数据复制到用户打开的 excel 工作簿中。
Sub test()
Dim ToSheet As Worksheet
Dim FromBook As String
Dim FromSheet As Worksheet
'--------------------------------------------------------------------
Set ToSheet = ActiveSheet
FromBook = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx, All Files (*.*), *.*", 1, "Select Log File ")
If FromBook = "False" Then Exit Sub
Workbooks.Open FromBook
Set FromSheet = ActiveSheet.Worksheets("Sheet1")
'--------------------------------------------------------------------
'Copy / Paste Data
ToSheet.Activate
DoColumnCopy "Apple", "Orange"
End Sub
'-------------------------------------------------------
'I need below code to work between the workbook, currently it
'only works between the worksheet.
'Get data from FromColName and copy to ToColName
Sub DoColumnCopy(FromColName As String, ToColName As String)
Dim rng As Range, rngCopy As Range, rng2 As Range
Set rng = workbooks(1).Sheets("data").Rows(1).Find(What:=FromColName, LookIn:=xlValues, _
LookAt:=xlWhole)
If Not rng Is Nothing Then
Set rngCopy = workbooks(1).Sheets("data").Range(rng).End(xlDown)
Set rng2 = workbooks(2).Sheets("log").Rows(1).Find(What:=ToColName, LookIn:=xlValues, _
LookAt:=xlWhole)
If Not rng2 Is Nothing Then rngCopy.Copy rng2.Offset(1, 0)
End If
End Sub
如何修复这workbooks(2).sheets...
部分代码,以便它知道用户打开的工作簿的名称?或者如果打开了多个工作簿?
如果您有更好的方法,我欢迎
您非常感谢!