用于更新日志的Excelmacros

我正在寻找创build一个macros,我把我有一个主文件,不断更新最后一行,并把它放在一个单独的日志文件。

我只想从原始文件的最后一行复制特定的单元格,并将它们粘贴到日志文件中,我已经closures了,我相信但需要以下方面的帮助:

  1. select最后一行中的特定列
  2. 如何在运行macros时不会在任何时候打开或激活日志文件。

我很抱歉,如果这个问题已经得到解答,但我似乎无法将我在网上find的所有信息都整理成适用的解决scheme。 这是我迄今为止的macros…

' copy2 Macro ' Range("B5000").End(xlUp).Select Selection.EntireRow.copy Windows("Daily Referral Log.xlsx").Activate Range("A65536").End(xlUp).Offset(1, 0).Select ActiveSheet.Paste End Sub 

看起来你正试图打开日志工作簿,并从数据文件中粘贴最后一行。

我想这是你要找的。

请让我知道,如果你需要别的东西。

如何在运行macros时不会在任何时候打开或激活日志文件。

关于这一点,我想我是理解你的权利。

你不想手动打开它; 你想要的代码来做到这一点。

如果这不是你的意思,请让我知道(你将不得不locking文件的写访问修改,无论)。

 Sub CopyLastRowToLog() Dim ThisWb, wb As Workbook Set ThisWb = ThisWorkbook Application.EnableEvents = False Application.DisplayAlerts = False Application.ScreenUpdating = False On Error Resume Next Set wb = Workbooks.Open("FileName and Path Goes Here") 'Edit This Line On Error GoTo 0 'Basic Error Handling - Probably worth adding ReadOnly check but keeping code short If wb Is Nothing Then Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True MsgBox ("Error Opening File") Exit Sub End If 'Put the names of the sheets you're using below. 'The first 2 sheets should be the sheet you're copying from 'The second 2 sheets should be the sheet you're copying to ThisWb.Sheets("Sheet1").Range("A" & Sheets("Sheet1").UsedRange.Rows.Count).EntireRow.Copy _ wb.Sheets("Sheet1").Range("A" & Sheets("Sheet1").UsedRange.Rows.Count + 1) wb.Close (True) Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub 

编辑:

要回答你的第一个问题,文件名看起来很奇怪:

 Workbooks.Open("C:\Users\EYousefi\Desktop[Daily Referral Log.xlsx]Sheet1") 

它可能应该是:

 Workbooks.Open("C:\Users\EYousefi\Desktop\Daily Referral Log.xlsx") 

你不需要在那里指定工作表。

其次,要复制特定的列,您需要更改复制行。 我添加了2个新的variables,使其更易于阅读:

 'Get the last rows for the workbooks Dim ThisWBLR, FinalWBLR ThisWBLR = ThisWB.Sheets("Sheet5").UsedRange.Rows.Count FinalWBLR = wb.Sheets("Sheet1").UsedRange.Rows.Count+1 ThisWb.Sheets("Sheet5").Range("B" & ThisWBLR & ":D" & ThisWBLR).Copy _ wb.Sheets("Sheet1").Range("B" & FinalWBLR) 

如果您更容易,您也可以一次指定一个数据片段:

 'Copy B to B ThisWB.Sheets("Sheet5").Range("B" & ThisWBLR).Copy wb.Sheets("Sheet1").Range("B" & FinalWBLR) 'Copy C to C ThisWB.Sheets("Sheet5").Range("C" & ThisWBLR).Copy wb.Sheets("Sheet1").Range("C" & FinalWBLR)