ActiveSheet.UsedRange.Select with conditions

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Need Macro to copy specific range on a sheet. It will always be in columns C thru H, but how far down varies and macro needs to determine that. Start row no less than 3, but needs to adjust that lower, based on cell value in column H starting to be greater than zero, i.e. H3, H4 may be zero one time, but H3:H6 may be zero another, macro needs to exclude those rows from copying.

So far I only got:

VBA Code:
ActiveSheet.UsedRange.Select
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

can you use the Autofilter for excluding rows? If not maybe start with something like
VBA Code:
Sub s1611512()
Dim lngStart As Long
Dim lngLast As Long
Dim lngLooper As Long
Dim lngHelp As Long

lngStart = 3
Do While Cells(lngStart, "H").Value <= 0
  lngStart = lngStart + 1
Loop

For lngLooper = 3 To 8
  lngHelp = Cells(Rows.Count, lngLooper).End(xlUp).Row
  If lngHelp > lngLast Then lngLast = lngHelp
Next lngLooper

Range("C" & lngStart & ":H" & lngLast).Select
End Sub
Ciao,
Holger
 
Upvote 0
Solution
Hi,

can you use the Autofilter for excluding rows? If not maybe start with something like
VBA Code:
Sub s1611512()
Dim lngStart As Long
Dim lngLast As Long
Dim lngLooper As Long
Dim lngHelp As Long

lngStart = 3
Do While Cells(lngStart, "H").Value <= 0
  lngStart = lngStart + 1
Loop

For lngLooper = 3 To 8
  lngHelp = Cells(Rows.Count, lngLooper).End(xlUp).Row
  If lngHelp > lngLast Then lngLast = lngHelp
Next lngLooper

Range("C" & lngStart & ":H" & lngLast).Select
End Sub
Ciao,
Holger
Thanks, @HaHoBe

How do you eXclude line(s) from copying if value in H is zero not at the top, but at the bottom?

In other words, idea is to eXclude any lines from copying that have a zero in H column.
 
Upvote 0
Hi 2Took,

any lines without zero either calls for the AutoFilter, the Advanced Filter or VBA Code to loop.

Example for a loop may look like this:
VBA Code:
Sub MrE161221A()
'https://www.mrexcel.com/board/threads/activesheet-usedrange-select-with-conditions.1218319/
'mod from https://www.mrexcel.com/board/threads/activesheet-usedrange-select-with-conditions.1218319/post-5956497
Dim lngLast       As Long
Dim lngLooper     As Long
Dim lngHelp       As Long
Dim lngResize     As Long
Dim rngSelect     As Range

Const cstrCheckCol As String = "H"
Const cstrStartCol As String = "C"

With Sheets("Source")
  lngResize = .Cells(1, cstrCheckCol).Column - .Cells(1, cstrStartCol).Column + 1
  
  For lngLooper = 3 To 8
    lngHelp = .Cells(Rows.Count, lngLooper).End(xlUp).Row
    If lngHelp > lngLast Then lngLast = lngHelp
  Next lngLooper
  
  For lngLooper = 3 To lngLast
    If .Cells(lngLooper, cstrCheckCol).Value > 0 Then
      If rngSelect Is Nothing Then
        Set rngSelect = .Cells(lngLooper, cstrStartCol).Resize(, lngResize)
      Else
        Set rngSelect = Union(rngSelect, .Cells(lngLooper, cstrStartCol).Resize(, lngResize))
      End If
    End If
  Next lngLooper
End With

If Not rngSelect Is Nothing Then
  rngSelect.Copy Sheets("Dest").Range("A1")
  Set rngSelect = Nothing
End If

End Sub

Code for Autofilter may look like this (please mind that CurrentRegion will take the whole surrounding area of the starting cell until an empty column or row is detected, this will include rows above as well):

VBA Code:
Sub MrE1612219_Autofilter()
'Dest will show the header row as well
With Sheets("Source").Range("C2").CurrentRegion
  .AutoFilter Field:=6, Criteria1:=">0", Operator:=xlFilterValues
  .SpecialCells(xlCellTypeVisible).Copy Sheets("Dest").Range("A1")
  .AutoFilter
End With
End Sub

In both samples data is located on Worksheet("Source"), the found range will be copied over to Worksheet("Dest"). Please adjust the names to suit.

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

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