我正在寻找创建一个宏,我把我在主文件中的内容放在最后一行不断更新并将其放入一个单独的日志文件中。
我只想从原始的最后一行复制特定的单元格并将它们粘贴到日志文件中,我已经接近我相信但需要帮助以下几个方面:
如果这个问题得到解答我很抱歉,但我似乎无法将我在网上找到的所有信息都用于适用的解决方案。这是我到目前为止的宏......
' 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
答案 0 :(得分:0)
您似乎正在尝试打开日志工作簿并将数据文件中的最后一行粘贴到其中。
我认为这是你正在寻找的。 p>
如果您需要别的东西,请告诉我。
如何在运行宏时不会打开或激活日志文件。
关于这一行,我认为我理解你是对的。
您不想手动打开它;你希望代码能够做到这一点。
如果这不是您的意思,请告诉我们(无论如何,您都必须锁定文件以进行写入访问以进行修改。)
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个新变量,以便于阅读:
'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)