VBA Select cells with value only

dexterfly

New Member
Joined
Apr 16, 2014
Messages
4
I am having a lot of issues trying to select the cells with value only.
For some reason, Excel selects all of the cells with value or blank cells that had a formula before and now they are empty. (see image below)

Here is the formula I am using to select the cells with value:
Sub Sheet3_Rectangle1_Click()
' select all the cells in columns D:G
ActiveSheet.Range("d1").CurrentRegion.Select
Selection.Copy

' paste only the values
Range("d1").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

' select all the cells in columns D:G
ActiveSheet.Range("d1").CurrentRegion.Select
Selection.Copy

End Sub



In the image below, when I click on the SELECT ALL button, it selects all the cells with no value that has a formula before.
I WANT TO AVOID IT. I need to select all columns and rows with value. I need to stop at the last row with actual value.
In the example below, it should stop at row 18 instead of row 1000 that had a formula before the copy and paste process.

My goal is to select D1 to G:18. Right now is selecting D1 to G:1000.

I need help fixing this issue. Thanks

sMN1wxb.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Maybe:
Range("D1:G1000).SpecialCells(xlCellTypeConstants).Select
 
Upvote 0
Code:
Dim lr As Long
lr = ActiveSheet.Columns("D:G").Find("*", After:=Range("D1"), xlValues, xlPart, xlByRows, xlPrevious).Row 
With ActiveSheet
.Range("D1", .Cells(Rows.Count, 7).End(xlUp)).SpecialCells(xlCellTypeConstants).Select
End With
 
Upvote 0
@JoeMo It didn't work because it skipped blank cells in the same row. It also selected the empty cells with formulas.

@JLGWhiz I got an error (syntax error) on the second line of your code
 
Upvote 0
My goal is to select D1 to G:18. Right now is selecting D1 to G:1000.

I need help fixing this issue. Thanks

sMN1wxb.png

Give this single line of code a try...

Code:
 Range("D1:G" & Columns("D:G").Find("*", , xlValues, , xlRows, xlPrevious).Row).Select
 
Upvote 0
dexterfly;3780761. @JLGWhiz I got an error (syntax error) on the second line of your code[/QUOTE said:
Just delete the 'After:='
Code:
lr = ActiveSheet.Columns("D:G").Find("*", Range("D1"), xlValues, xlPart, xlByRows, xlPrevious).Row
With ActiveSheet
.Range("D1", .Cells(Rows.Count, 7).End(xlUp)).SpecialCells(xlCellTypeConstants).Select
 
Upvote 0
ONE MORE THING:

How can I add this to a variable?

Code:
Sheets("Body").Range("D1:G" & Columns("D:G").Find("*", , xlValues, , xlRows, xlPrevious).Row).Select
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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