VBA question -ranges

Hiport

Active Member
Joined
May 9, 2008
Messages
455
Is it possible to put two ranges in one line of code?

Code:
  With shtTest
    Intersect(.UsedRange, .Range("A8:H" & Rows.Count)).ClearContents
    Intersect(.UsedRange, .Range("N8:S" & Rows.Count)).ClearContents
  End With
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Hiport

Try:

Code:
With shtTest
    Intersect(.UsedRange, Application.Union(.Range("A8:H" & Rows.Count), .Range("N8:S" & Rows.Count))).ClearContents
End With
 
Upvote 0
One more question, how can i add a msgbox which will return "No items to provision" Exit Sub if the criteria "provisioned" is not found?


Code:
Public Sub copyVisibleCols()
 Dim shtTest1, shtTestAs Worksheet
 
 Set shtTest1 = Sheets("Test1")
 Set shtTest= Sheets("Test")
 
    With shtTest
     Intersect(.UsedRange, Application.Union(.Range("A8:H" & Rows.Count), .Range("N8:S" & Rows.Count))).ClearContents
    End With

    With shtTest
        If .FilterMode Then .ShowAllData
        With .Range("A5").CurrentRegion
        .AutoFilter Field:=1, Criteria1:="Provisioned"
        On Error Resume Next
        .Offset(1, 1).Resize(.Rows.Count - 1, 1).Resize(, 3).SpecialCells(xlCellTypeVisible).Copy
        shtTest1.Range("A8").PasteSpecial xlPasteValues
        
        .Offset(1, 5).Resize(.Rows.Count - 1, 1).Resize(, 1).SpecialCells(xlCellTypeVisible).Copy
        shtTest1.Range("D8").PasteSpecial xlPasteValues
        
        .Offset(1, 12).Resize(.Rows.Count - 1, 1).Resize(, 1).SpecialCells(xlCellTypeVisible).Copy
        shtTest1.Range("E8").PasteSpecial xlPasteValues
 
        End With
      If .FilterMode Then .ShowAllData
   End With
End Sub






Hi Hiport

Try:

Code:
With shtTest
    Intersect(.UsedRange, Application.Union(.Range("A8:H" & Rows.Count), .Range("N8:S" & Rows.Count))).ClearContents
End With
 
Upvote 0
Hi

One way is to look for the criterion in the first column of the range before applying the filter.

Code:
...
        With .Range("A5").CurrentRegion
            If .Columns(1).Find(What:="Provisioned", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) Is Nothing Then
                MsgBox "bye"
                Exit Sub
            End If
...

Another way would be to apply the filter and then check if there are visible rows.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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