我有一个代码可以比较不同工作簿中两个工作表之间的标题,并在主工作簿中复制粘贴数据。
'lastCol = Worksheets("Dashboard").Cells(3, Columns.Count).End(xlToLeft).Column
lastCol = 15
lastrow = Worksheets("Dashboard").Cells(Rows.Count, 1).End(xlUp).Row
Set cmpRng = Range(Cells(1, 1), Cells(3, lastCol))
a = cmpRng
i = Cells(Rows.Count, 1).End(xlUp).Row
Set Wbk = Workbooks.Open("Z:\RMG\RMG Data Master\Global_HEADCOUNT.xls")
Worksheets("GLOBAL_HEADCOUNT").Select
Mastcol = Cells(1, Columns.Count).End(xlToLeft).Column
j = Cells(Rows.Count, 1).End(xlUp).Row
Set mastRng = Range(Cells(1, 1), Cells(1, Mastcol))
b = mastRng
For k = 1 To lastCol
For n = 1 To Mastcol
If UCase(a(3, k)) = UCase(b(1, n)) Then
Windows("Global_HEADCOUNT").Activate
Worksheets("GLOBAL_HEADCOUNT").Range(Cells(2, n), Cells(j, n)).Copy
Windows("Dashboard.xlsm").Activate
Worksheets("Dashboard").Select
Cells(i + 1, k).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Exit For
End If
Next
Next
Call Wbk.Close(False)
现在的问题是,每次文件名更改时,用户都必须进入代码并更改它,这可能会导致问题,所以我想避免为他们提供使用对话框选择文件的替代方法
我知道的:
我对如何实现这一点有一点了解,
'The folder containing the files to be recap'd
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialFileName = "G:\Work\" '<----- Change this to where the files are stored.
fd.InitialView = msoFileDialogViewList
'allow multiple file selection
fd.AllowMultiSelect = False
fd.Filters.Add "Excel Files", "*.xls*"
filechosen = fd.Show
'Create a workbook for the recap report
Set Master = ThisWorkbook
If filechosen = -1 Then
但是我将上面的代码用于其他东西,我很难将我的代码与之集成......我所要求的只是一些指导,因为我试图实现的结果有点不同:) ty in进步