简体   繁体   中英

getting overflow error, when I used the function “ActiveSheet.UsedRange.Rows.Count” to find the count in excel VBA

I am getting an overflow error, when I tried to use the function

ActiveSheet.UsedRange.Rows.Count

in VBA Excel

I am going on assumption you have something like the following:

Dim x as Integer

x = ActiveSheet.UsedRange.Rows.Count

To fix simply change it to:

Dim x as Long

x = ActiveSheet.UsedRange.Rows.Count

Or

Dim x&

x = ActiveSheet.UsedRange.Rows.Count

The ampersand (&) type-declaration character represents a Long

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

Because Excel worksheets can have more then 32,767 rows, you cannot ALWAYS fit the number of rows into an integer, and must use a Long.

Have you defined your variable as an integer rather than a long or float? If there are more than 32256 or something rows it's too big to be an integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM