Change/Amend VBA Code To Run Fast

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,516
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I have a set of data which contains shipment records. I have to hide the rows where Column O
shows the status of the Shipment.

I am using the below mentioned code to hide rows based on Column O

PHP:
Private Sub CommandButton1_Click()

For a = 3 To 500

If Worksheets("RO").Cells(a, 15).Value = "Shipped" Then
Worksheets("RO").Rows(a).Hidden = False
End If
If Worksheets("RO").Cells(a, 15).Value = "In Progress" Then
Worksheets("RO").Rows(a).Hidden = True
End If
Next

End Sub

PHP:
Private Sub CommandButton2_Click()

For a = 3 To 500

If Worksheets("RO").Cells(a, 15).Value = "Shipped" Then
Worksheets("RO").Rows(a).Hidden = True
End If
If Worksheets("RO").Cells(a, 15).Value = "In Progress" Then
Worksheets("RO").Rows(a).Hidden = False
End If
Next

End Sub

PHP:
Private Sub CommandButton3_Click()

For a = 3 To 500

If Worksheets("RO").Cells(a, 15).Value = "Shipped" Then
Worksheets("RO").Rows(a).Hidden = False
End If
If Worksheets("RO").Cells(a, 15).Value = "In Progress" Then
Worksheets("RO").Rows(a).Hidden = False
End If
Next

End Sub


Is there any other way out (code) that can provide the desired result a bit fast or can we
change the code to make it work fast

Any help would be appreciated

Regards,

Humayun
 
Forgot to mention: If the dot before Value is not the problem, please post the full error message.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Thanks Dave,

I tried the code you provided

Taking more or less same time :(
 
Upvote 0
Forgot to mention: If the dot before Value is not the problem, please post the full error message.

Now i tied it with dot and without dot.... This time no message is there... i mean nothing is happening ...
in both cases (with / without dot) its the same
 
Upvote 0
Now i tied it with dot and without dot.... This time no message is there... i mean nothing is happening ...
in both cases (with / without dot) its the same
The dot must be there!

Are you sure the code hasn't just run so fast you didn't notice anything happening?

Remove the line of code near the bottom that says
Code:
.ClearContents
& run the code again. have a look at column Z and see if there are any 9's or x's visible.

Have you changed anything else in the code?
 
Last edited:
Upvote 0
Yes Dave,

But would like one thing to share which i experienced with Peter's code and that is when i copied the code for the first time and when i got back to the sheet where the command buttons are. What i noticed is when i pressed the command button 2 and 3 which were running the codes mentioned in post # 1. they were working really fast (strange......) as peter mentioned. but when i pressed the 1st command button which had the peter code - then it gave the debugging message and suddenly everything stops.

After that as i mentioned in post # 13 that i tried the code with / without dots then the code dose not do anything.
 
Upvote 0
But would like one thing to share which i experienced with Peter's code and that is when i copied the code for the first time and when i got back to the sheet where the command buttons are. What i noticed is when i pressed the command button 2 and 3 which were running the codes mentioned in post # 1. they were working really fast (strange......) as peter mentioned. but when i pressed the 1st command button which had the peter code - then it gave the debugging message and suddenly everything stops.

After that as i mentioned in post # 13 that i tried the code with / without dots then the code dose not do anything.
It sounds a bit like you may not have connected the Command buttons to the codes correctly.

As another test, instead of using the command button with my code, find my code in the vba window & click in it anywhere then press F5. Now go back to your 'RO' sheet and see if the rows have been hidden/unhidden.
 
Last edited:
Upvote 0
The dot must be there!

yes it is there

Are you sure the code hasn't just run so fast you didn't notice anything happening?

No brother its not like that.... i am noticing and nothing is happening.

Remove the line of code near the bottom that saysCode:
.ClearContents

Ok... Removed

& run the code again. have a look at column Z and see if there are any 9's or x's visible.

I ran the code again. There are no 9's or x's in column Z

Have you changed anything else in the code?

No i have not changed anything else in the code
 
Upvote 0
It sounds a bit like you may not have connected the Command buttons to the codes correctly.

As another test, instead of using the command button with my code, find my code in the vba window & click in it anywhere then press F5. Now go back to your 'RO' sheet and see if the rows have been hidden/unhidden.


Tried this also... nothing happens... Tried the F5 with my other 2 codes which are connected with command buttons 2 & 3. they work fine.

Also made a new button # 4 and attached your code to that also... Still nothing happens
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,346
Members
452,638
Latest member
Oluwabukunmi

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