Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Macro crashes after 32000 rows

Tags:

excel

vba

I have a VBA macro that copies rows from one worksheet into another based upon finding values in cells in 3 columns. The macro works, but crashes when it reaches row 32767. There are no formulas or special formatting in this row. Further, I have taken that row out, but it still crashes on that row number. Is this a limitation in excel? There are some 43000 in the worksheet that is being process

Therefore, I ask what is wrong with my macro and how I can get it reach the end of the worksheet:

Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim wks As Worksheet
On Error GoTo Err_Execute

For Each wks In Worksheets

LSearchRow = 4
LCopyToRow = 4

ThisWorkbook.Worksheets.Add After:=Worksheets(Worksheets.Count)
Set wksCopyTo = ActiveSheet
wks.Rows(3).EntireRow.Copy wksCopyTo.Rows(3)

While Len(wks.Range("A" & CStr(LSearchRow)).Value) > 0

    If wks.Range("AB" & CStr(LSearchRow)).Value = "Yes" And wks.Range("AK" & CStr(LSearchRow)).Value = "Yes" And wks.Range("BB" & CStr(LSearchRow)).Value = "Y" Then

        Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
        Selection.Copy


        wksCopyTo.Select
        wksCopyTo.Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
        wksCopyTo.Paste

        'Move counter to next row
        LCopyToRow = LCopyToRow + 1
        'Go back to Sheet1 to continue searching
        wks.Select
    End If
    LSearchRow = LSearchRow + 1
Wend

Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Next wks
    Exit Sub
Err_Execute:
    MsgBox "An error occurred."

Please help!

like image 456
Andy5 Avatar asked May 11 '12 20:05

Andy5


People also ask

How many rows can VBA handle?

The maximum number of rows is 1,048,576 for every sheet in excel (this might depend on your version).

How big can a macro be?

There is no maximum length limit for a macro in Excel. However, there is a limit of 8,192 characters for a single line of code. Macros are written in the Visual Basic for Applications (VBA) programming language.

How do I fix out of memory in VBA?

To correct this errorClose unnecessary applications, documents and source files. Reduce the number of Public variables. Check available disk space. Increase the available RAM by installing additional memory or reallocating memory.

What is error code 400 in VBA?

In VBA, error 400 is a runtime error which is not so common, but there are a few situations where you could face this error. This error occurs when Microsoft Excel crashes or fails while running a macro. And different reasons can make Excel crash or fails: Deficient in the installation of Microsoft Excel.


2 Answers

The VBA 'Int' type is a signed 16-bit field so it can only hold values from -32768 to +32767. Change those variables to 'Long', which is a signed 32-bit field and can hold values from -2147483648 to +2147483647. Should be enough for Excel. ;)

like image 79
Eight-Bit Guru Avatar answered Sep 19 '22 17:09

Eight-Bit Guru


This sounds like an integer Problem

The Integer and Long data types can both hold positive or negative values. The difference between them is their size: Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647.

But which version are you using? Because:

Traditionally, VBA programmers have used integers to hold small numbers, because they required less memory. In recent versions, however, VBA converts all integer values to type Long, even if they are declared as type Integer. Therefore, there is no longer a performance advantage to using Integer variables; in fact, Long variables might be slightly faster because VBA does not have to convert them.

This Information is directly from MSDN

UPDATE

Please also read the first comment! I was interpreting the MSDN Information the wrong way!

Thats MSDN being misleading: VBA does not itself convert Integer to Long. Under the covers the CPU converts integer to long , does the arithmetic and then converts the resulting long back to integer. So VBA integers still cannot hold numbers larger than 32K – Charles Williams

like image 45
Neysor Avatar answered Sep 17 '22 17:09

Neysor