excel 使用 xls 每日报告中的新数据自动更新现有电子表格

excel Automatically Update an existing spreadsheet with new data from an xls daily report

我每天都在手动将每日报告中的数据剪切并粘贴到此 Master spread sheet 中。新文件每天都一样,只是文件名每天不同,因为它添加了日期。前任。 2017-03-11-18875、2017-03-12-18875、2017-03-13-18875 等

我读到的内容说我需要创建一个代码来搜索新文件,然后打开该文件,剪切数据并将其粘贴到我现有的传播中sheet。

这可能需要一些调试,具体取决于您的文件名和其他内容。如果在复制到您的母版 sheet 时您转到第一个空行进行粘贴,那么您将必须合并一些代码来获取第一个未使用的行号。没问题,让我知道。其余的我一边说一边试着解释。如果您有任何问题,请告诉我。复制您的主工作簿,并在练习此代码时使用它。在复制的工作簿中打开一个模块并粘贴此代码。看看你能不能跟得上逻辑。

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