简体   繁体   English

VBA使用变量打开另一个工作簿和工作表

[英]VBA Open another workbook and sheet with variables

I could not find an answer to this yet: 我还没找到答案:

I have a sheet with the Customer name in Cell A2 and the product code in Cell B2. 我有一个单元格A2中的客户名称和单元格B2中的产品代码。 I have a file location "C:\\Users\\Reception\\Documents\\Shared\\Item Master Data\\Customer BOMS\\" with a workbook for each Customer. 我有一个文件位置“C:\\ Users \\ Reception \\ Documents \\ Shared \\ Item Master Data \\ Customer BOMS \\”,其中包含每个客户的工作簿。 In these workbooks, there is a tab per product code with the formulation to make the product. 在这些工作簿中,每个产品代码都有一个选项卡,其中包含制作产品的配方。

What I need to do is to tell the Macro to open the workbook for the specific customer in cell A2 and to then go to the tab in cell B2. 我需要做的是告诉宏在单元格A2中打开特定客户的工作簿,然后转到单元格B2中的选项卡。

I have been able to open the workbook, but not the sheet. 我已经能够打开工作簿,但不能打开工作表。 Here is my code: 这是我的代码:

Range("A2").Select

Dim CName As String

Dim PCode As String

Dim BOM As Workbook

Dim ws As Worksheet

CName = ActiveCell.Value

PCode = ActiveCell.Offset(0, 1).Value

Set BOM = Workbooks.Open("C:\Users\Reception\Documents\Shared\Item Master Data\Customer BOMS\" & CName & ".xlsm")

Set ws = BOM.Sheets(Range(PCode))

Try this: 尝试这个:

Range("A2").Select

Dim CName As String

Dim PCode As String

Dim BOM As Workbook

Dim ws As Worksheet

CName = ActiveCell.Value

PCode = ActiveCell.Offset(0, 1).Value

Set BOM = Workbooks.Open("C:\Users\Reception\Documents\Shared\Item Master Data\Customer BOMS\" & CName & ".xlsm")

Set ws = BOM.Sheets(PCode)
 ws.activate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM