VBA Paste to Dynamic Column Address

s_simon

New Member
Joined
Sep 12, 2018
Messages
10
Prior to my problem area, I receive two dates from a form which I then determine the difference between them.


Code:
    Dim dayCount As Integer
    dayCount = DateDiff("d", Date_Selection.sDate.Value, Date_Selection.eDate.Value)

On my worksheet I need to take the formula that resides in "J2" and copy it to the column count of daycount
ie. If daycount = 2 , then copy the formula in J2 over to K2 and L2.

My latest attempt was:
Code:
    Worksheets("Master_Week").Range("J2", Cells("J2", Cells(2, Columns.Count).End(xlToLeft).Column + dayCount)).Formula = Range("J2").Formula
Which did not work
I have tried numerous variations of autofill, xltoleft, xltoright, up, down turn around, bang my head on the desk but can not get it right in my head

Hope the above makes sense.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this
Code:
With Worksheets("Master_Week")
    .Cells(2, Columns.Count).End(xlToLeft).Offset(, 1).Resize(, daycount).Formula = Range("J2").Formula
End With
 
Last edited:
Upvote 0
Try this
Code:
With Worksheets("Master_Week")
    .Cells(2, Columns.Count).End(xlToLeft).Offset(, 1).Resize(, daycount).Formula = Range("J2").Formula
End With

Thanks JLGWhiz - it copied the formula over the correct amount of columns , however I am running into a problem on the copy -
Cell J2 =
=IFERROR(IF((I2+1)<='Date Sheet'!$B$2,I2+1,""),"")
and now K2 also = =IFERROR(IF((I2+1)<='Date Sheet'!$B$2,I2+1,""),"") instead of =IFERROR(IF((J2+1)<='Date Sheet'!$B$2,J2+1,""),"")
 
Upvote 0
So I believe that I have what I need :

Worksheets("Master_Week").Activate
Worksheets("Master_Week").Range("J2").Select
Worksheets("Master_Week").Range("J2").Offset(, dayCount).Select
Dim newCol As String
newCol = "J2:" & ActiveCell.Address
newCol = Replace(newCol, "$", "")
Worksheets("Master_Week").Range(newCol).FillRight
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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