Does a worksheet have to be activated or selected to capture the visible filtered range?

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the bit of code below that is referring to a range of filtered cells on another worksheet. I need to know if there is a way of setting this range without having to select or activate the worksheet called "LogDetails". Right now the value for the "Rng" comes up as nothing and I can't see why it would. If I let the code show the worksheet called "LogDetails" it will select the filtered range on that worksheet. Any help, much appreciated.


Thanks, SS


VBA Code:
    LR = Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("LogDetails").Range("G1:N" & LR).SpecialCells(xlCellTypeVisible).Select
  
    Set Rng = Sheets("LogDetails").Range("G1:N" & LR).Selection.SpecialCells(xlCellTypeVisible).Select
    
    On Error GoTo 0

    If Rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Remove this line completely
VBA Code:
    Sheets("LogDetails").Range("G1:N" & LR).SpecialCells(xlCellTypeVisible).Select

And remove the Select from this line. It's not needed to assign the range, and it will prevent it from working if the sheet is hidden.
Rich (BB code):
    Set Rng = Sheets("LogDetails").Range("G1:N" & LR).Selection.SpecialCells(xlCellTypeVisible).Select
 
Upvote 0
...and remove the Selection in the middle
Rich (BB code):
Set Rng = Sheets("LogDetails").Range("G1:N" & LR).Selection.SpecialCells(xlCellTypeVisible).Select
 
Upvote 1
Solution
...and remove the Selection in the middle
Rich (BB code):
Set Rng = Sheets("LogDetails").Range("G1:N" & LR).Selection.SpecialCells(xlCellTypeVisible).Select
This worked great along with the suggestion to remove the Post #4. Thank you all.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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