Using a range of non-empty cells in VBA

bbot23

New Member
Joined
Oct 29, 2010
Messages
37
Hey guys,

I have a simple line of code in VBA which I need to modify:

Workbooks("trend_strat").Worksheets("recordings").Range("E2:E23").Value = Workbooks("trend_strat").Worksheets("recordings").Range("D2:D23").Value

Right now it goes from row 2 to 23 because those are the cells with values. I'd like the range to be from 2 to the last cell with a value above 0. For example if D24:D44 had no value but D45 had a value and cells after that didn't, the range would be D2:D45 and E2:E45.

Is that possible? Thanks guys!!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
It is definitely possible. Here is an messy way that should do it.

Code:
LastRow = Cells(Rows.Count,4).End(xlUp).Row
Set MyRange = Cells(2,4).Resize(LastRow-1,1)
For Each X In MyRange
    If Isnumeric(X.Value) and X.Value > 0 Then
        Set Y = X
    Endif
Next X
Set MyRange = Range(Cells(2,4),Y)
MyRange.Offset(0,1).Value = MyRange.Value
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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