我每天都会手动剪切并将每日报告中的数据粘贴到此主电子表格中。新文件每天都是相同的,每天只有文件的名称不同,因为它会添加日期。恩。 2017-03-11-18875,2017-03-12-18875,2017-03-13-18875等。
我正在阅读的内容说我需要创建一个代码来搜索新文件,然后打开文件,剪切数据并将其粘贴到我现有的电子表格中。
答案 0 :(得分:1)
这可能需要一些调试,具体取决于您的文件名和其他内容。如果复制到主工作表时,请转到要粘贴的第一个空行,那么您必须合并一些代码才能获得第一个未使用的行编号。没问题,请告诉我。其余的我试着解释,因为我去了。如果您有任何问题,请告诉我。制作主工作簿的副本,并在练习此代码时使用它。在复制的工作簿中打开一个模块并粘贴此代码。看看你是否可以遵循逻辑。
Sub getOpenExcel()
' Your daily report has a date in it's name
' to select an open workbook we must first know it's name
' AND - it must be already open
' Your examples are 2017-03-11-18875, 2017-03-12-18875, 2017-03-13-18875
' If the name is the current date then this would work to get the filename
Dim fileName As String, monthNum As String, dayNum As String, wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet, rng1 As Range, rng2 As Range
' this adds a ZERO to the front of month numbers less than 10
If Month(Date) < 10 Then
monthNum = "0" & CStr(Month(Date))
Else
monthNum = CStr(Month(Date))
End If
' You may or may not need this section
' it adds a ZERO to the front of day numbers less than 10
If Day(Date) < 10 Then
dayNum = "0" & CStr(Day(Date))
Else
dayNum = CStr(Day(Date))
End If
' many cases the daily report will come from the previous day
' If your file has yesterday's date, then comment out the above code and uncomment the following code
'
'If Day(DateAdd("d", -1, Date)) < 10 Then
' dayNum = "0" & Day(DateAdd("d", -1, Date))
'Else
' dayNum = Day(DateAdd("d", -1, Date))
'End If
fileName = CStr(Year(Date)) & "-" & monthNum & "-" & dayNum & "-" & "18875"
' if today's date is 3/14/17 then "fileNem" = "2017-03-12-18875"
' If your daily report is an excel book, then we need to add the proper extension.
' It could be one of many, "xls", ".xlsx" , ".xlsm", etc....
' If your daily report is open - look at the top. It should have the file name and extension.'
' Replace the below extension with the correct one.
fileName = fileName & ".xlsx"
' Again, if today's date is 3/14/17 then "fileNem" = "2017-03-12-18875.xlsx"
' This is where we set both workbooks to variables
'
Set wb1 = ThisWorkbook ' This is your master sheet
Set ws1 = wb1.Worksheets("Sheet1")
On Error GoTo notOpen
Set wb2 = Workbooks(fileName) ' This is your daily report
On Error GoTo 0
Set ws2 = wb2.Worksheets("Sheet1")
ws1.Activate
'*************************************************************************************
' If successful this is the area where you put your code to copy and paste automatically
'
' If you need this pasted to the first empty row at bottom of page then
' put code here to find the first empty row and use that varaible
' with range("a" & firstUnusedRow) intstead of A1 ...
wb2.Activate
Range("A1:Z500").Copy _
Destination:=wb1.Worksheets("Sheet1").Range("A1") 'change A1 to A & firstUnusedRow
'*************************************************************************************
' This is the clean up and exit code
Set wb1 = Nothing
Set wb2 = Nothing
Exit Sub
notOpen:
On Error GoTo 0
Set wb1 = Nothing
MsgBox "The file " & fileName & " is not open"
Exit Sub
End Sub