简体   繁体   English

根据单元格值从可变工作表名称复制的 VBA 代码

[英]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.

相关问题 VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date VBA代码将单元格从sheet_A复制到sheet_B - VBA code to copy a cell from sheet_A to sheet_B 根据列表复制工作表和单元格值,并使用 excel vba 代码根据列表重命名 - copy sheet and cell value based on list and rename as per list with excel vba code 根据其他工作簿中其他工作表中的单元格值(月份名称)选择要复制到的工作表 - Selecting sheet to copy to based on cell value(Month name) from other sheet in other workbook Excel VBA代码将基于单元格引用的数据复制到另一个工作表 - Excel VBA Code to Copy Data based on a cell reference to another sheet Excel VBA 根据第二列单元格值将列从一张表复制到另一张表 - Excel VBA to Copy Column from one sheet to another based on a second columns cell value 基于标题名称将列从 Excel 复制到另一个工作表的 VBA 代码 - VBA code to copy column from Excel based on header name to another sheet 将工作表的副本命名为原始工作表中单元格中的值 - Name a copy of a Sheet the value in a cell in original sheet 如何使用 VBA 代码根据单元格值隐藏 Excel 表 - How to hide Excel Sheet based on cell value using VBA Code 在每个工作表匹配的单元格上复制一个单元格并将其粘贴到另一个VBA - Copy a cell on one sheet and paste it on another VBA based on cell from each sheet matching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM