1

I have a very long and clunky code that works , but often crashes and gives a No Response message. One way of getting around this, I've found, is to declare variables as Integers, rather than Long Integers if I know they won't exceed 32,767.

To ensure that I won't run into problems later on, I want to insert a line of code into the start of the script that will Dim the variables as either Integers or Long Integers depending on the size of the worksheet. What I have so far is:

 If Library.Range("A1").SpecialCells(xlCellTypeLastCell).row > 30000 Then
 Dim i As Long 
 Dim LastRow As Long 
 ElseIf Library.Range("A1").SpecialCells(xlCellTypeLastCell).row <= 30000  Then
 Dim i As Integer
 Dim LastRow As Integer

But this is giving me an error for having duplicate declaration statements. Is there another approach to this? Or am I doing something wrong?

4
  • 3
    What is your code doing!? There is something fundamentally wrong going on if using a bit less memory on a couple of variables is causing it to be unresponsive! May 13, 2016 at 9:46
  • 2
    Also, VBA converts all Integers to Long Integers anyway - so you wouldn't actually see any performance benefit from this. If anything, using a Long is more efficient because the value isn't being cast. May 13, 2016 at 10:06
  • Ah OK, good point. It works to copy and paste a lot of information, re-arranging it based on a series of IF THEN statements. It works perfectly with small datasets, but stalls out sometimes on larger ones (though often still works). May 13, 2016 at 12:41
  • 1
    Try putting the data into an array and work with it in memory instead. If your code is actually working then you can paste it to Code Review to get critique and optimisation tips May 13, 2016 at 12:44

1 Answer 1

0

Why not use a variant instead,

this will demonstrate

Dim a As Variant

a = 100
Debug.Print TypeName(a)

a = 10000000
Debug.Print TypeName(a)
2
  • I'm a bit confused as to how this is having a material effect on OP. However, if you want to use as little memory as possible you surely wouldn't want to declare as Variant.
    – CallumDA
    May 13, 2016 at 9:54
  • I wasn't using as a solution to his memory issues, I was showing that it can be done for him, and its the best place to show/format code. I personally wouldn't use that method, apologies if confusion caused.
    – Nathan_Sav
    May 13, 2016 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.