[英]VBA code to copy from a variable sheet name based on cell value
Hoping I might get some help as I'm a bit lost!希望我能得到一些帮助,因为我有点迷茫!
I have a Master template and a user template.我有一个主模板和一个用户模板。 Multiple (10+) users will be submitting their worksheets.
多个 (10+) 用户将提交他们的工作表。
The users have a "Submission" tab that they complete.用户有一个他们完成的“提交”选项卡。 When they submit their worksheet, it creates a new tab in their file, and pastes the values of their submission in the new tab (so that formulae aren't retained) and names the new tab based on the monthly submission it corresponds to ie 'Jan-19'
当他们提交工作表时,它会在他们的文件中创建一个新选项卡,并将他们提交的值粘贴到新选项卡中(以便不保留公式)并根据它对应的每月提交来命名新选项卡,即 ' 1 月 19 日'
I am then looking to copy a specific column of their submission into my master file using a macro, however in January I will copy from the Jan-19 tab, and in February I will copy from the Feb-19 tab.然后,我希望使用宏将他们提交的特定列复制到我的主文件中,但是在 1 月我将从 1 月 19 日选项卡中复制,而在 2 月我将从 2 月 19 日选项卡中复制。 Therefore, in my macro, I am struggling to come up with the right code where the Sheet name will vary based on the month name.
因此,在我的宏中,我正在努力想出正确的代码,其中工作表名称将根据月份名称而变化。
Currently I have the following code:目前我有以下代码:
Dim OpenFileName As String
Dim wb As Workbook
'Select and Open workbook
OpenFileName = Application.GetOpenFilename(",*.xlsm")
If OpenFileName = "False" Then Exit Sub
Set wb = Workbooks.Open(OpenFileName)
ThisWorkbook.Sheets("User 1").Range("B19:B36").Value = wb.Sheets("***Variable month name***").Range("B19:B36").Value
ThisWorkbook.Sheets("Control").Range("D15") = Now
wb.Close
Any help you can provide would be much appreciated!您能提供的任何帮助将不胜感激!
For current month use当月使用
Format(Now,"mmm-yy")
This will give you Feb-20
.这会给你
Feb-20
。 So incorporating it in your code所以将它合并到你的代码中
wb.Sheets(Format(Now,"mmm-yy"))
For a specific month/year use对于特定的月/年使用
Dim dt As Date
Dim m As Long, y As Long
m = 2 '<~~ Feb (Month)
y = 2019 '<~~ Year
dt = DateSerial(y, m, 1)
Usage would be用法是
wb.Sheets(Format(dt,"mmm-yy"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.