You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA宏无法定位最后一行,覆盖最后条目问题求助

VBA宏覆盖最后一条记录而非新增行的问题修复

问题根源分析

你的宏出现覆盖最后一条记录的情况,核心有两个问题:

  1. 工作表保护操作对象错误:代码中Worksheets("Invoicing").Unprotect/Protect未指定目标工作簿,默认操作的是本地工作簿(wbLocal)的工作表,而非需要更新的主工作簿(wbMaster)的表,这可能导致主表权限异常或定位逻辑受干扰。
  2. 最后一行定位逻辑的潜在漏洞:依赖列A查找最后一行时,若列A的最后一条记录为空、存在合并单元格,或中间有空白行,会导致End(xlUp)定位到错误行,最终计算出的masterNextRow等于已有记录的行号,而非下一行。

修正后的代码

Sub another_workbook()
    Dim wbMaster As Workbook
    Dim wbLocal As Workbook
    Dim wsMaster As Worksheet
    Dim wsLocal As Worksheet
    Dim masterNextRow As Long

    Set wbLocal = ThisWorkbook
    ' 初始化工作表对象,减少重复代码
    Set wsLocal = wbLocal.Worksheets("Placement Form")
    
    Set wbMaster = Workbooks.Open("I:\GT Month End\Bookkeeper\Project Abacus\Forecasting and Invoicing MASTERTEST.xlsm")
    Set wsMaster = wbMaster.Worksheets("Invoicing")
    
    ' 更稳妥的最后一行定位:直接取列A最后非空行+1
    masterNextRow = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row + 1
    
    ' 操作主工作簿的工作表解锁
    wsMaster.Unprotect "1312"
    
    ' 批量赋值,简化代码结构
    With wsMaster
        .Cells(masterNextRow, 1).Value = wsLocal.Range("C3").Value
        .Cells(masterNextRow, 2).Value = wsLocal.Range("I3").Value
        .Cells(masterNextRow, 3).Value = wsLocal.Range("N3").Value
        .Cells(masterNextRow, 4).Value = wsLocal.Range("F5").Value
        .Cells(masterNextRow, 5).Value = wsLocal.Range("F9").Value
        .Cells(masterNextRow, 6).Value = wsLocal.Range("F11").Value
        .Cells(masterNextRow, 7).Value = wsLocal.Range("F19").Value
        .Cells(masterNextRow, 8).Value = wsLocal.Range("F27").Value
        .Cells(masterNextRow, 9).Value = wsLocal.Range("H27").Value
        .Cells(masterNextRow, 10).Value = wsLocal.Range("A27").Value
        .Cells(masterNextRow, 11).Value = wsLocal.Range("C27").Value
        .Cells(masterNextRow, 12).Value = wsLocal.Range("H27").Value
        .Cells(masterNextRow, 13).Value = wsLocal.Range("M27").Value
        .Cells(masterNextRow, 14).Value = wsLocal.Range("F38").Value
        .Cells(masterNextRow, 15).Value = wsLocal.Range("N38").Value
        .Cells(masterNextRow, 16).Value = wsLocal.Range("C32").Value
        .Cells(masterNextRow, 17).Value = wsLocal.Range("D99").Value
        .Cells(masterNextRow, 19).Value = wsLocal.Range("M95").Value
        .Cells(masterNextRow, 21).Value = wsLocal.Range("D95").Value
        .Cells(masterNextRow, 20).Value = wsLocal.Range("M97").Value
        .Cells(masterNextRow, 24).Value = wsLocal.Range("D97").Value
        .Cells(masterNextRow, 29).Value = wsLocal.Range("F36").Value
        .Cells(masterNextRow, 30).Value = wsLocal.Range("F17").Value
        .Cells(masterNextRow, 31).Value = wsLocal.Range("F13").Value
        .Cells(masterNextRow, 32).Value = wsLocal.Range("F15").Value
        .Cells(masterNextRow, 33).Value = wsLocal.Range("F40").Value
        .Cells(masterNextRow, 34).Value = wsLocal.Range("M21").Value
        .Cells(masterNextRow, 35).Value = wsLocal.Range("N40").Value
        .Cells(masterNextRow, 36).Value = wsLocal.Range("H32").Value
        .Cells(masterNextRow, 37).Value = wsLocal.Range("L32").Value
        .Cells(masterNextRow, 38).Value = wsLocal.Range("F34").Value
        .Cells(masterNextRow, 39).Value = wsLocal.Range("K34").Value
    End With
    
    ' 重新保护主工作簿的工作表
    wsMaster.Protect "1312"
    wbMaster.Close True
    
    MsgBox "Input saved."     
End Sub

额外检查建议

  • 打开主工作簿的Invoicing工作表,检查列A的最后一条记录是否为空,或存在合并单元格、隐藏行,这些都会干扰End(xlUp)的定位结果。
  • 如果Invoicing表是结构化表格(ListObject),可以改用ListObject.ListRows.Add的方式新增行,定位逻辑更可靠,示例如下:
    Dim tbl As ListObject
    Set tbl = wsMaster.ListObjects("表的名称")
    Dim newRow As ListRow
    Set newRow = tbl.ListRows.Add
    newRow.Range(1).Value = wsLocal.Range("C3").Value
    ' 后续字段赋值以此类推
    

内容的提问来源于stack exchange,提问作者Jayneecm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 07:20:53