我的VBA方法导致Excel崩溃 - 我看不出错误

时间:2018-04-16 10:26:39

标签: excel vba excel-vba

  编辑:我发布myRange后可能发现了一个问题   变量似乎没有做任何事 - 所以我觉得他们是   从我很久以前使用的方法开始,那里决定裁剪

     

我将删除整个myRange变量,看看会发生什么

 Set myRange = ActiveSheet.Range("1:1")
 Set myRange = ActiveSheet.Range("A:A")
     

编辑2:好的,所以将numCols和numRows函数更改为仅使用

numCols = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
numRows = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
     

他们现在返回正确的行号和列号   但现在当我运行selectBlock()时,它给出了运行时错误28"超出堆栈空间"

Hello All,我一直在编写代码以便能够浏览多个工作表并将数据复制到主工作簿

我根据您传递给它的任何文件对此进行编码以处理 - 这很好

我遇到的问题是我所做的函数,它找到了我传递给它的任何工作表的最后一个填充行

Sub test()

selectBlock().Select
End Sub


Function selectBlock() As Range

Dim row As Integer: row = numRows() 'Finds last populated row
Dim col As Integer: col = numCols() 'Finds last populated column

Set selectBlock() = Range("A2:" & Cells(row, col).Address)
'sets this area starting from cell A2 as the Range

End Function

Function numCols() As Integer

Dim myRange As Range
Set myRange = ActiveSheet.Range("1:1") 'Checks first row to see how many populated columns there are
numCols = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

End Function

Function numRows() As Integer
Dim myRange As Range
Set myRange = ActiveSheet.Range("A:A") 'Checks first columns to see how many populated rows there are
numRows = Range("A" & Rows.Count).End(xlUp).row

End Function

当我调用测试Sub时,它会导致Excel挂起然后崩溃而没有错误代码 所以我想我会创建一些非常好处理的循环或严重错误

对此有任何帮助将非常感激

我也能理解我是如何进行的,这是非常愚蠢的 我曾经用Java编写代码,也许我正在使用我从未摆脱过的技术或陷阱 - 我在大多数时候都在VBA自学,因此从未学过VBA的官方编码实践

3 个答案:

答案 0 :(得分:4)

很多事情在这里

  1. 完全符合您的细胞资格
  2. 在处理行和列时使用Long而非Integer
  3. 使用错误处理。这样可以避免Excel崩溃。
  4. 试试这个

    Sub test()
        On Error GoTo Whoa
        selectBlock().Select
        Exit Sub
    Whoa:
        MsgBox Err.Description
    End Sub
    
    Function selectBlock() As Range
        Dim row As Long: row = numRows() 'Finds last populated row
        Dim col As Long: col = numCols() 'Finds last populated column
    
        Set selectBlock = ActiveSheet.Range("A2:" & ActiveSheet.Cells(row, col).Address)
    End Function
    
    Function numCols() As Long
        numCols = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
    End Function
    
    Function numRows() As Long
        numRows = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).row
    End Function
    

答案 1 :(得分:2)

替换

Set selectBlock() = Range("A2:" & Cells(row, col).Address)

Set selectBlock = Range("A2:" & Cells(row, col).Address)

它看起来像递归:P

答案 2 :(得分:1)

有更安全的方法可以找到LastRowLastCol,我更喜欢Find函数。

在我的代码评论中查看更多详细信息。

<强>代码

Sub test()

Dim Rng As Range

Set Rng = selectBlock
Rng.Select '<-- Not sure why you need to Select ?

End Sub

'============================================================

Function selectBlock() As Range

Dim LastRow As Long
Dim LastCol As Long

LastRow = FindLastRow(ActiveSheet) 'Finds last populated row
LastCol = FindLastCol(ActiveSheet) 'Finds last populated column

Set selectBlock = Range(Cells(2, "A"), Cells(LastRow, LastCol))

End Function

'============================================================

Function FindLastCol(Sht As Worksheet) As Long

' This Function finds the last col in a worksheet, and returns the column number
Dim LastCell As Range

With Sht
    Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
                        SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        FindLastCol = LastCell.Column
    Else
        MsgBox "Error! worksheet is empty", vbCritical
        End
    End If
End With

End Function

'============================================================

Function FindLastRow(Sht As Worksheet) As Long

' This Function finds the last row in a worksheet, and returns the row number

Dim LastCell As Range
With Sht
    Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        FindLastRow = LastCell.row
    Else
        MsgBox "Error! worksheet is empty", vbCritical
        End
    End If
End With

End Function