简体   繁体   English

如何将引用的工作表更改为变量? 我想让它激活

[英]How do I change the worksheet referenced to be a variable? I want it to activate

The macro I wrote - it pulls information from two different spreadsheets and formats it all.我编写的宏 - 它从两个不同的电子表格中提取信息并将其全部格式化。 Those spreadsheets will change every month.这些电子表格每个月都会更改。 How do I get the "ACTIVATE" command to reference the worksheet I have open instead of the one that WAS open when I recorded this macro?如何获取“激活”命令来引用我打开的工作表,而不是在我录制此宏时打开的工作表? One of the commands loos like this:其中一个命令如下所示:

Windows("273517_0273517M190831_08_31_2019_Toll Detail.csv").Activate

I tried to copy and paste a few lines of the code but THIS site rejected it, saying it wasn't formatted properly.我试图复制并粘贴几行代码,但这个网站拒绝了它,说它的格式不正确。 Since it was an exact copy of a fully functioning macro - I haven't got a clue what it wants.因为它是一个功能齐全的宏的精确副本——我不知道它想要什么。

It is the "...Toll Detail.csv" file in line 4 that needs to be a variable as it changes monthly.第 4 行中的“...Toll Detail.csv”文件需要成为一个变量,因为它每月都会发生变化。 This file is referenced several times in my recorded macro.这个文件在我录制的宏中被多次引用。 I will need to be able to go in and change each of them in whatever manner you suggest.我需要能够输入 go 并以您建议的任何方式更改它们。 I am TOTALLY NEW to VBA so please, dumb it down as best you can.我对 VBA 完全陌生,所以请尽可能地把它哑巴。 :) Thank you! :) 谢谢!

I would suggest declaring a variable in the beginning of the sub, like so:我建议在 sub 的开头声明一个变量,如下所示:

Dim strFile As String
strFile = "273517_0273517M190831_08_31_2019_Toll Detail.csv"

Throughout the sub you can reference the variable, so in every instance where there is the workbook name in the code replace it with strFile like this:在整个 sub 中,您可以引用该变量,因此在代码中存在工作簿名称的每个实例中,将其替换为strFile ,如下所示:

Windows(strFile).Activate

That way you only have to update it once for the entire procedure.这样,您只需为整个过程更新一次。 There might be more elegant solutions, but more information on the use case would be required for that.可能会有更优雅的解决方案,但需要更多关于用例的信息。

You can use GetOpenFileName to select and open your file each time.您可以使用 GetOpenFileName 到 select 并每次打开您的文件。 Following is the code for your reference:以下是供您参考的代码:

Sub OpenFile()
Dim myFile As String
Dim Wb As Workbook

myFile = Application.GetOpenFilename("CSV Files (*.csv), *.csv")

'Exit if no file selected
If myFile = "False" Then
    MsgBox "No file selected!"
    Exit Sub
End If

'Open the file
Set Wb = Workbooks.Open(myFile)
'Do what you want with the file here onward
'
'
'
End Sub

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

相关问题 Worksheet_change:当单元格中的值不变时,我不希望它激活 - Worksheet_change: i do not want it to activate when value in cell does not change 如何获取单元格公式引用的工作表 - How do I get the worksheet referenced by a cell formula 如何从存储的工作表变量创建工作表 - How do I create a worksheet from a stored worksheet variable 如何将特定工作表更改为当前工作表? - How do I change specific worksheet to current worksheet? 如何获取工作表代码名称以激活特定工作表? - How can I get worksheet code name to activate a specific worksheet? 如何根据变量激活特定的工作表。 该变量将具有要激活的所需工作表的名称 - How can I active a specific worksheet based on a variable. The variable will have the name of the desired Sheet to activate 单元格中的工作表名称/如何编写公式以便我可以在单元格中引用的工作表中调用值 - Worksheet Name in Cell / How do I write the formula so I can Call a Value in the Worksheet that's referenced in cell 如何在 VBA 中手动触发工作表的 Activate 事件? - How can I manually trigger the Activate event of a worksheet in VBA? 如何将变量工作表名称插入多个excel公式? - How do I insert a variable worksheet name into multiple excel formulas? 如何使用变量名称在工作表上引用控件对象? - How do I refer to a controls object, on a worksheet, using a variable name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM