Vba 重新运行宏
我有一个宏,可以清除以前的内容并替换为新信息。我需要对能够成功编码的行进行调整。我的一个问题是,如果我以前的数据少于我的新数据,它需要重新运行几次才能获取所有信息。如何修复代码以确保捕获所有新数据Vba 重新运行宏,vba,excel,Vba,Excel,我有一个宏,可以清除以前的内容并替换为新信息。我需要对能够成功编码的行进行调整。我的一个问题是,如果我以前的数据少于我的新数据,它需要重新运行几次才能获取所有信息。如何修复代码以确保捕获所有新数据 Worksheets("Before n After Remap Review").Range("A7:E" & LastRow).Select Selection.ClearContents Worksheets("Before n After Remap Review").Range("
Worksheets("Before n After Remap Review").Range("A7:E" & LastRow).Select
Selection.ClearContents
Worksheets("Before n After Remap Review").Range("I7:J" & LastRow).Select
Selection.ClearContents
Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("A1:E" & LastRow).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("A7:E" & LastRow).PasteSpecial
Application.CutCopyMode = False
Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("F1:G" & LastRow).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("I7:J" & LastRow).PasteSpecial
Application.CutCopyMode = False
您需要:
LastRow
,或LastRow
变量行.Count
和.End(xlUp)
方法来确定最后一行
Function FindLastRow(ByRef ws As Worksheet, Optional ByVal col As Variant = "A") As Long
FindLastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
End Function
选择1
选择2
或者。。。
考虑直接使用范围,而不是每次使用LastRow创建新范围。您需要:
LastRow
,或LastRow
变量行.Count
和.End(xlUp)
方法来确定最后一行
Function FindLastRow(ByRef ws As Worksheet, Optional ByVal col As Variant = "A") As Long
FindLastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
End Function
选择1
选择2
或者。。。
考虑直接使用范围,而不是每次使用LastRow构建新范围
Sub Option2()
Dim sheetTarget As Worksheet, sheetSource As Worksheet
Dim LastRowT As Long, LastRowS As Long
Set sheetTarget = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review") ' our target sheet
Set sheetSource = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro") ' our source sheet
' Set sheetTarget = ActiveWorkbook.Worksheets(1)
' Set sheetSource = ActiveWorkbook.Worksheets(2)
LastRowT = FindLastRow(sheetTarget) ' last row of our target sheet, based on column A (default)
sheetTarget.Range("A7:E" & LastRowT).ClearContents ' clear our target sheet
sheetTarget.Range("I7:J" & LastRowT).ClearContents ' clear our target sheet
' our target sheet is cleared, we no longer need to work with LastRowT
LastRowS = FindLastRow(sheetSource) ' last row of our source sheet, based on column A (default)
sheetSource.Range("A1:E" & LastRowS).Copy ' copy our source range
' while we don't work with LastRowT anymore, we still need the size of the target range to paste to, and we need to calculate this based on LastRowS
LastRowT = LastRowS + 7 - 1 ' the target range starts at row 7 (A7), the source range starts at row 1 (A1)
sheetTarget.Range("A7:E" & LastRowT).PasteSpecial ' paste to our target range
sheetSource.Range("F1:G" & LastRowS).Copy
sheetTarget.Range("I7:J" & LastRowT).PasteSpecial
Application.CutCopyMode = False
End Sub