我需要一些帮助才能找到并更正下面的代码。 首先解释它的作用:
我遇到问题的部分是最后一部分,因为我似乎无法使vlookup工作。我不知道如何在公式中引用第一个打开的工作簿,因为我不能用它的名字来做(因为它会变化并且每个月都在变化)
非常感谢任何帮助。
以下代码:
Sub Activate_sheet()
Application.ScreenUpdating = False
Call ReconFile
Application.ScreenUpdating = True
End Sub
Sub ReconFile()
' Activate_sheet Macro
' Select the source workbook and activates the proper sheet and prepares columns
Dim vFileName1 As Variant
' Prompt user to select a file
vFileName1 = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*, All Files (*.*),*.*", , "Select the Recon file to be used")
If vFileName1 = "False" Then
MsgBox "No file selected", vbExclamation, "Cancelled"
Exit Sub 'user canceled
End If
With Workbooks.Open(Filename:=vFileName1, UpdateLinks:=0)
Worksheets("Data").Select
Columns("A:B").Insert Shift:=xlToRight
Columns("T:T").Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Columns("A:A").TextToColumns Destination:=Range("A1")
Lngth = Range("$A$" & Rows.Count).End(xlUp).Row
Final = "$B$3:$B" & Lngth
Range(Final).FormulaR1C1 = "=RC[-1]&RC[14]"
Call WorkbenchFiles
.Close False
End With
End Sub
Sub WorkbenchFiles()
Dim CF As Long
Dim vFileName2 As Variant
CF = MsgBox("Would you like to select a new Workbench file?", vbYesNo)
If CF = vbYes Then
vFileName2 = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*, All Files (*.*),*.*", , "Select the Workbench File to update")
If vFileName2 = "False" Then
MsgBox "No file selected", vbExclamation, "Cancelled"
Exit Sub 'user canceled
End If
Workbooks.Open (vFileName2)
Worksheets("Sheet1").Select
Lngth = Range("$A$" & Rows.Count).End(xlUp).Row
Final = "$K$2:$K" & Lngth
Range(Final).FormulaR1C1 = "=MID(RC[-10],8,8)"
Final = "$L$2:$L" & Lngth
Range(Final).FormulaR1C1 = "=RC[-1]&RC[-7]"
Final = "$M$2:$M" & Lngth
Range(Final).FormulaR1C1 = "=VLOOKUP(RC[-1], '[" & vFileName1 & "]Data'!$B:$H,3,0)"
Final = "$N$2:$N" & Lngth
Range(Final).FormulaR1C1 = "=VLOOKUP(RC[-2],'[" & vFileName1 & "]Data'!$B:$H,4,0)"
Final = "$O$2:$O" & Lngth
Range(Final).FormulaR1C1 = "=VLOOKUP(RC[-3],'[" & vFileName1 & "]Data'!$B:$H,7,0)"
Else: Exit Sub
End If
End Sub