【问题标题】:Daily macro to copy and paste based on column header每日宏根据列标题复制和粘贴
【发布时间】:2021-03-19 18:50:36
【问题描述】:

我对在 Excel 中编写宏非常陌生,并且已经环顾四周试图解决我的问题,但我还没有找到可行的解决方案。

我正在尝试编写一个宏来执行以下操作:

根据列标题从源A(工作表Daily_Inventory(m3),工作簿Daily_Inventory_YYYYMMDD)复制数据(例如,我想复制列名“所有活动”下的所有数据)。数据的行数在这一行可能会增加/减少。

然后我想将此数据粘贴到目标 B(工作表 Daily_Inventory(m3)、工作簿 Daily_Inventory_Master)中对应的列名“MMDDYYYY”下。

工作簿 Daily_Inventory_YYYYMMDD 将在 365 天内每天存储在我的指定文件夹中,然后我必须每天将信息从其中复制到另一个工作簿,其中包含每个相关的列标题。如果列标题不存在,添加新值(与源文件相同的日期时间)然后粘贴。

请在这种情况下帮助我,我将非常感谢任何帮助。 非常感谢!!!

【问题讨论】:

  • 如果代码进入工作簿Daily_Inventory_Master.xlsm 中的模块,那么您必须决定代码将如何或为什么要准确运行Daily_Inventory_YYYYMMDD.xlsm。另一方面,如果代码进入Daily_Inventory_YYYYMMDD.xlsm 中的模块,那么它很简单,您可以使用Daily_Inventory_Master.xlsx 作为结果(注意.xlsx)。想想这些并澄清一下。
  • 谢谢你的建议,我已经澄清然后在另一个文件中做了。

标签: excel vba


【解决方案1】:

这都是在我的脑海中完成的,没有经过测试,但应该不会太远:

bookName1 = "Daily_Inventory_YYYYMMDD.xlsx"
sheetName1 = "Daily_Inventory(m3)"
headerRow1 = 8

bookName2 = "Daily_Inventory_Master.xlsm"  'note, xlsm as the macro would need to reside somewhere, better in the book you are using every day
sheetName2 = "Daily_Inventory(m3)"
headerRow2 = 12

DestHeader = mid(bookName1,20,2) & mid(bookName1,22,2) & mid(bookName1,16,4) 
'Parsing the input date, play around with the numbers or use len(bookName1)-X, Y Z to work backwards if the first part of the name changes

'Oops, forgot to open the workbooks
Workbooks.Open "C:\folderName1\" & bookName1
Workbooks.Open "C:\folderName2\" & bookName2


'1. Find the source column
iCol = 1
DoUntil Workbooks(bookName1).Sheets(sheetName1).cells(headerRow1, iCol) = "All activities")
    iCol = iCol + 1
Loop

'2. Find the last row for data within that column 
lastRow = Workbooks(bookName1).Sheets(sheetName1).cells(1048576,iCol).End(xlUp).Row

'3. Copy the data   
Range(Workbooks(bookName1).Sheets(sheetName1).cells(headerRow1+2,iCol), _
    Sheets(Workbooks(bookName1).sheetName1).cells(lastRow,iCol)).Copy

'4. Use variation of step 1 to find the destination column
iiCol = 1
DoUntil Workbooks(bookName2).Sheets(sheetname2).cells(headerRow2,iiCol) = DestHeader
    iiCol = iiCol + 1
Loop

'5. Paste data in 
Workbooks(bookName2).Sheets(sheetname2).cells(headerRow2+2,iiCol)._
    PasteSpecial Paste:=xlPasteValues

'Close the books
Application.DisplayAlerts = False  'Disable the popups asking for confirm for saving
Workbooks(bookName1).close saveChanges:=False
Workbooks(bookName2).close saveChanges:=True
Application.DisplayAlerts = True

除此之外,如果要每天运行,您可以自动获取今天的日期并使用它

currDay = Format(Date, "dd")
currMonth = Format(Date, "mm")
currYear = Format(Date, "yyyy")

所以 bookName1 和 DestHeader 将是:

bookName1 = "Daily_Inventory_" & currYear & currMonth & currDay & ".xlsx"

DestHeader = currMonth&currDay&CurrYear

【讨论】:

  • 我几乎可以肯定你对 bookName1 和 bookName1.xlsx 有问题
  • 正确,只是在这里查看了另一本宏的书-它将使用 .xlsx(以及 .xlsm 用于存储宏的位置)将修改
  • 非常感谢,它对我帮助很大,您的代码非常干净清晰。
  • 对不起,我的家乡回复晚了,互联网太贵了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多