VBA: how to change cell reference

cmclux

New Member
Joined
Mar 24, 2011
Messages
12
I have written the following copy and paste macro which now references to one cell and the copy and paste works. I have a range of data in one row that I would like to use the same macro for but when I change the cell reference to a range the macro does not work.

Private Sub Workbook_Open()
'
' Macro1 Macro
'

'
If Range("D3") <= Range("$B$2") Then
Range("D42").Copy
Range("D42").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End If

End Sub

The macro compares a date in cell D3 with todays date in cell B2 and then copies and replaces values in cell D42. The references to D3 and D42 need to reference to column E, F etc.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try something like this...
Code:
Private Sub Workbook_Open()

    Dim col As Integer
    
    With Sheets("Sheet1")
    
        For col = 4 To 10 ' For each column D to J
            If .Cells(3, col).Value <= Date Then .Cells(42, col).Value = .Cells(42, col).Value
        Next col
    
    End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,703
Members
452,938
Latest member
babeneker

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