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

Excel VBA代码运行时错误求助:Run Time Error 1004对象错误

Fixing Run Time Error 1004 on Your VBA Line

Hey there, let's work through why that lC4 = Cells(155, Columns.Count).End(xlToLeft).Column line is throwing a 1004 error. Here are the most common issues and straightforward fixes:

  • You're not targeting the correct worksheet
    The Cells and Columns objects automatically use whichever sheet is active when your code runs. If the sheet with row 155 isn't the active one, this can trigger the error. Fix it by explicitly naming your worksheet to avoid ambiguity:

    Dim targetSheet As Worksheet
    Set targetSheet = ThisWorkbook.Sheets("YourSheetName") ' Replace with your actual sheet name
    lC4 = targetSheet.Cells(155, targetSheet.Columns.Count).End(xlToLeft).Column
    
  • The worksheet is protected
    If the sheet holding row 155 is protected (even without a password), your code can't access cell properties. Temporarily unprotect the sheet before running this line, then re-protect it if needed:

    targetSheet.Unprotect ' Add your password in quotes if required: targetSheet.Unprotect "YourPassword"
    lC4 = targetSheet.Cells(155, targetSheet.Columns.Count).End(xlToLeft).Column
    targetSheet.Protect ' Re-apply protection with your original settings if needed
    
  • Row 155 is completely empty
    If every cell in row 155 is blank, End(xlToLeft) from the last column will land on A155, but in some edge cases (like odd sheet formatting), this can still trigger an error. Add a check to handle empty rows gracefully:

    If WorksheetFunction.CountA(targetSheet.Rows(155)) = 0 Then
        lC4 = 0 ' Or set a default value that fits your code's logic
    Else
        lC4 = targetSheet.Cells(155, targetSheet.Columns.Count).End(xlToLeft).Column
    End If
    
  • Merged cells are disrupting the range
    Merged cells in row 155 can break the End(xlToLeft) behavior. If possible, unmerge cells in that row—this is usually the cleanest fix. If you can't unmerge, you'll need to adjust your code to account for merged ranges (though that gets more complex).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:07:56