【问题标题】:Excel VBA out of memory error with simple line of codeExcel VBA内存不足错误与简单的代码行
【发布时间】:2021-06-17 01:30:30
【问题描述】:

使用 excel VBA 获取内存不足错误,使用简单的代码行直到现在一直有效。

    Dim lRow, lCol, rowCount, totalRows As Long
Dim tblRng, vLookup, vLookup2, vLookup3 As Range
Dim wb As Workbook
Dim ws As Worksheet
Dim branch, position, branchName, seniorLu, region, branchNum As Variant

Range("B2").Cut Range("C2")
Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

lRow = Cells(Rows.Count, 1).End(xlUp).Row '<---- this line is the error
lCol = Cells(1, Columns.Count).End(xlToLeft).Column

不知道为什么这行看似简单的代码会抛出内存不足错误。到现在为止,它每周都工作一年多。

非常感谢任何想法

【问题讨论】:

  • Dim lRow As Long, lCol As Long, rowCount As Long, totalRows As Long。您需要为每个变量重复类型。虽然我很惊讶你会因为隐含的Variant 而出现内存不足错误。可能还有其他事情发生。
  • 以防万一:您是否将其关闭并再次打开(又名:您是否重新启动了计算机)?
  • @Pᴇʜ 是的,我已经重新启动了我的主计算机两次,甚至在另一台计算机上尝试过。奇怪的是它现在才开始发生。
  • 您有正在运行的加载项吗?如果是这样,请尝试禁用它们(文件 > 选项 > 加载项 > 管理 COM 加载项)

标签: excel vba


【解决方案1】:

只是深入了解一下,但我建议正确声明变量并正确指定 RangeCellsRowsColumns 对象的工作簿/工作表,看看是否有帮助:

Dim lRow As Long, lCol As Long, rowCount As Long, totalRows As Long
Dim tblRng As Range, vLookup As Range, vLookup2 As Range, vLookup3 As Range
Dim wb As Workbook
Dim ws As Worksheet
Dim branch, position, branchName, seniorLu, region, branchNum As Variant

Set wb = ThisWorkbook ' ‹-- define your workbook
Set ws = wb.Worksheets("Sheet1") ' ‹-- define your sheet

' use that ws as worksheet
ws.Range("B2").Cut ws.Range("C2")
ws.Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 
lCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

始终确保没有没有完全指定的工作簿/工作表的 RangeCellsRowsColumns 对象。

【讨论】:

    【解决方案2】:

    为每个变量重复变量类型:

    Dim lRow As Long, lCol As Long, rowCount As Long, totalRows As Long
    

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多