VBA: Script reacting different as a whole than standalone

Failed84

New Member
Joined
Apr 26, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm having some issues within my script that I don't seem to be able to be debug. The issue is the following;

Within my script I have to make a selection between higher and lower than 12. I started with a script looking like this:
VBA Code:
'for the first row is a header, start at the second.
   For BooBoo = 2 To LastCell
   
'Select all rows that have entries that are older the 12 (so hide all others)
   If Cells(BooBoo, "BP").Value < 12 Then
       Rows(BooBoo).EntireRow.Hidden = True
   End If
Next

'Delete all hidden rows
   For iCntr = ContentRow To 1 Step -1
       If Rows(iCntr).Hidden = True Then Rows(iCntr).EntireRow.Delete
Next

When testing this code by itself - there is nothing wrong with it. When I have it added in with the rest of the code strange things start to happen like the age 11 is being added to my 12+ list. I feel it was because the code was moving too fast So I tried to slow it by using the code:

VBA Code:
    Application.Wait (Now + TimeValue("0:00:02"))

I've even upped it to 5 seconds.

When I couldn't get it to work I tried to add a workaround by adding the following:

VBA Code:
'for the first row is a header, start at the second.
   For Tx = 2 To TheEnd

'Select all rows that have postal 6021 and are older the 12 (so hide all others)
   If Cells(Tx, "BP").Value < 12 Then
       Cells(Tx, "BQ").Value = "Younger"
   Else
       Cells(Tx, "BQ").Value = "Older"
   End If
Next
 
'for the first row is a header, start at the second.
   For BooBoo = 2 To LastCell
   
   ActiveWindow.ScrollRow = 2
   
'Select all rows that have postal 6021 and are older the 12 (so hide all others)
   If Cells(BooBoo, "BQ").Value = "Older" Then
       Rows(BooBoo).EntireRow.Hidden = True
   End If
Next

Now I had a new problem. Standalone the code is doing what it is supposed to, but in the larger whole only the first 8 will receive an older or younger, the rest stays blank.

Let me point out that I've tried several lines that would help cutting performance and making the script flow better... I've tried the ones in this list:
VBA Code:
'Turn some parameters off to improve performance
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    screenUpdateState = Application.ScreenUpdating
    statusBarState = Application.DisplayStatusBar
    calcState = Application.Calculation
    eventsState = Application.EnableEvents
    displayPageBreakState = ActiveSheet.DisplayPageBreaks
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    DisplayPageBreaks = False

Hopefully someone is able to help me with this problem.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It sounds like you're tying yourself up in knots.

Try starting again, but this time record code that:

Selects the entire data range
Applies a filter
Filters column BP for >=12 (saves looping over the data range)

Then review and adjust the resulting code
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top