使用variables调暗工作表

我有一个单元格的工作表=我想要变暗的文件夹的名称。 我想要做的是设置该单元格=文件名variables。 这可能会更容易看我的代码。 我目前正在得到“我的”设置input1“的对象所需的错误,我的方式来设置variables也可能是错误的

Dim WbkA As Workbook Dim Input1 as string Set Input1 = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls").worksheets("Sheet4").Range("A4").Value Set wbkA = Workbooks.Open(FileName:"Input1") 

您尝试使用关键字Set数据typesstring )分配对象的引用。

删除关键字Set ,它会没事的。

代码需要稍微重新sorting以突破步骤。

  • 从工作簿中获取文件path和名称,并将其存储为stringvariables(Input1)。

  • 使用存储在stringvariables(Input1)中的值打开文件。

  • 将打开文件的引用设置为对象variables(WbkA)。

下面列出的是代码

 Sub test() Dim Input1 As String Dim WbkA As Workbook Input1 = Worksheets("Sheet4").Range("A4").Value 'Get the path and file name Workbooks.Open Filename:=Input1 'Open the file Set WbkA = ActiveWorkbook 'Set the reference to the workbook MsgBox WbkA.Name 'Show the name value from the object. End Sub