run macro on non active sheet

jtodd

Board Regular
Joined
Aug 4, 2014
Messages
194
Hi All

I have this bit of code Sub HideOut()
Sheets("RDC_OB").Select
BeginRow = 208
EndRow = 244
ChkCol = 10

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 0 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End With
End Sub

How do I make this run from Sheet "Dash" wile staying on Sheet "Dash"
Sure this is simple but I cannot make it work !
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
[COLOR=#333333]BeginRow = 208[/COLOR][COLOR=#333333]
EndRow = 244[/COLOR]
[COLOR=#333333]ChkCol = 10[/COLOR]

[COLOR=#333333]For RowCnt = BeginRow To EndRow[/COLOR]
[COLOR=#333333]If [/COLOR][COLOR=#333333]Sheets("RDC_OB").[/COLOR][COLOR=#333333]Cells(RowCnt, ChkCol).Value = 0 Then[/COLOR]
[COLOR=#333333]Sheets("RDC_OB").[/COLOR][COLOR=#333333]Cells(RowCnt, ChkCol).EntireRow.Hidden = True[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]Sheets("RDC_OB").[/COLOR][COLOR=#333333]Cells(RowCnt, ChkCol).EntireRow.Hidden = False[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next RowCnt[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub

[/COLOR]


It seems you have posted only part of the code as there is no "with" statement but you have "end with ". So not tampering with your code just appending sheet name in front of cell references.

This should do the trick.

Thanks
Swapnil Shah
 
Last edited:
Upvote 0
Greetings,

Something like:

Rich (BB code):
Option Explicit
  
Sub HideOut()
Dim BeginRow As Long
Dim EndRow As Long
Dim ChkCol As Long
Dim RowCnt As Long


  BeginRow = 208
  EndRow = 244
  ChkCol = 10
  
  With ThisWorkbook.Worksheets("RDC_OB")
    For RowCnt = BeginRow To EndRow
      If .Cells(RowCnt, ChkCol).Value = 0 Then
        .Cells(RowCnt, ChkCol).EntireRow.Hidden = True
      Else
        .Cells(RowCnt, ChkCol).EntireRow.Hidden = False
      End If
    Next RowCnt
  End With


End Sub

Just to mention, empty cells will evaluate to zero.

Hope that helps,

Mark
 
Upvote 0
Hi Sorry the End with should not have been there
I now have this
Sub HideOut()
BeginRow = 208
EndRow = 244
ChkCol = 10

For RowCnt = BeginRow To EndRow
If Sheets("RDC_OB").Cells(RowCnt, ChkCol).Value = 0 Then
Sheets("RDC_OB").Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt

End Sub

When I run this on cell update from worksheet "Dash" runs very slowly , seems like it calculates it one line at a time?
 
Upvote 0
Thanks that works fine but very slowly , seems to do one row at a time.
When I run the macro with the RDC_OB activated it works very fast ?
 
Upvote 0
You can use following code

Code:
Sub HideBlank()
Dim BeginRow As Integer
Dim EndRow As Integer
Dim cell As Range


BeginRow = 208
EndRow = 244
ChkCol = 10


With Sheets("Dash")
    For Each cell In .Range(.Cells(BeginRow, ChkCol), .Cells(EndRow, ChkCol))
        If cell.Value = 0 Then cell.EntireRow.Hidden = True
    Next cell
End With


End Sub

Thanks
Swapnil Shah
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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