Dim 是您如何维度(声明变量),在您的情况下,告诉代码FName
将引用您分配的字符串值。有关不错的教程,请参阅此链接
要使您的代码正常工作,请尝试以下操作:
Dim FName As String
Dim FPath As String
FPath = "G:"
FName = ThisWorkbook.Sheets("sheet 1").Range("A1").Text 'use ThisWorkbook since you are running from Workbook1
With Workbooks("Workbook2.xlsx")
.SaveAs Filename:=FPath & "\" & FName
.Close True '-> use false to not save changes
End With
'here you need to tell VBA what specific workbook you want to save
'if you used ThisWorkbook here, you would save the workbook where the code runs from, which is Workbook1
您还可以采取更好的方法,如下所示:
Dim FName as String, FPath as String
Dim wkb1 as Workbook, wkb2 as Workbook
Set wkb1 = ThisWorkbook '-> or Set wkb1 = Workbooks("workbook1")
FPath = "G:"
FName = wkb1.Sheets("sheet 1").Range("A1").Text
Set wkb2 = Workbooks("workbook2")
With wkb2
.SaveAs Filename:=FPath & "\" & FName
.Close True '-> use false to not save changes
End With