conditional range selection VBA

mjrofra

Board Regular
Joined
May 18, 2009
Messages
180
Hi!

This is my problem:

If I have numbers in column A and I need to select data in that range that is greater than 50 with VBA, which is the easiest way to do it.

:confused:
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about recording a macro using AutoFilter for custom, Is Greater Than for 50. Then select the filtered range below the header row, hit F5, then Special > Visible Cells Only > OK.

That will select what you filtered but the issue is what you want to do with those cells after that, where you probably don't even need to select them in the first place.
 
Upvote 0
You shouldn't need to select those cells in order to work with them but try

Code:
Sub Bigger50()
Dim LR As Long, i As Long, r As Range
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("A" & i).Value > 50 Then
        If r Is Nothing Then
            Set r = Range("A" & i)
        Else
            Set r = Union(r, Range("A" & i))
        End If
    End If
Next i
r.Select
End Sub
 
Upvote 0
Thank you very much Tom and VoG.

I think you´re right, I don´t need to select those cells (I supose) what i need to do with those cell is to copy in another range (very simple, but I don´t know how).

Appreciate your help.
 
Upvote 0
Try this: change B1 as needed:

Rich (BB code):
Sub Bigger50()
Dim LR As Long, i As Long, r As Range
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("A" & i).Value > 50 Then
        If r Is Nothing Then
            Set r = Range("A" & i)
        Else
            Set r = Union(r, Range("A" & i))
        End If
    End If
Next i
r.Copy Destination:=Range("B1")
End Sub

However, using a filter is likely to be quicker.
 
Upvote 0
Thank you very mucho VoG, the code works perfect and I can learn a lot with it.

VoG, excuse me, just one more question, if column A is my criteria and I need to copy data from column A to column C to any other range, what do I need to change in the code.
 
Upvote 0
You just need to change B1 (shown in red) to the top of the range that you want to copy to.
 
Upvote 0

Forum statistics

Threads
1,218,391
Messages
6,142,154
Members
450,416
Latest member
Ferrito

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