Using column count to end of range

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
114
I have two dates and i am trying to create a dynamic date range based on those two dates.

in cell E5 is my starting date and E7 is my ending date. I have a formula to count the number of months between the dates in Y8="IFERROR(DATEDIF(E5,E7,"m"),0)"

I cannot get the end of range to copy my Edate formula. Or if there is a way to avoid using Y8's fromula that would be ideal.


Code:
Sub Update_Actuals()

Dim job As Worksheet
Dim column As Range
Dim columncount As Long




Set job = ThisWorkbook.Sheets("Job Cost Query")


job.Range("AF13").Formula = "=E5"


column = job.Range("Y8").Value


Set columncount = column + 14


Date = job.Range("AG13").Formula = "=EDATE(AF13,1)"
Date.Copy
Set daterange = job.Range(Cells(34.13), Cells(columncount, 13)).Paste


End Sub

Thank you ahead of time!
 
You are a god, thank you sir!!

Try this

Code:
Sub Update_Actuals()
  Dim i As Long, lc As Long, lr As Long
  lc = Cells(13, Columns.Count).End(xlToLeft).Column  'last column
  lr = Range("AG" & Rows.Count).End(xlUp).Row         'last row
  If lc < Columns("AG").Column Then lc = Columns("AG").Column
  If lr < 13 Then lr = 13
  Range(Cells(13, "AG"), Cells([COLOR=#0000ff]lr, lc[/COLOR])).ClearContents
  For i = 0 To DateDiff("m", Range("E5"), Range("E7"))
    Cells(13, Columns("AG").Column + i) = WorksheetFunction.EDate(Range("E5"), i)
    Range("Z31:Z78").Copy Cells([COLOR=#ff0000]14[/COLOR], Columns("AG").Column + i)
  Next
End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Thank you for your help on this DanteAmor.

After
Code:
[COLOR=#333333][I] Range("Z31:Z78").Copy Cells([/I][/COLOR][COLOR=#ff0000][I]14[/I][/COLOR][COLOR=#333333][I], Columns("AG").Column + i)[/I][/COLOR]
How would I sum this new paste range?
I would like to put the sum in Cell AK7.
 
Last edited:
Upvote 0
I'm not understanding.
Try

Code:
Range("Z31:Z78").Copy Cells([COLOR=#0000ff]7[/COLOR], Columns("[COLOR=#0000ff]AK[/COLOR]").Column + i)
 
Upvote 0
Code:
[COLOR=#333333][I]Sub Update_Actuals()[/I][/COLOR]  Dim i As Long, lc As Long, lr As Long
  lc = Cells(13, Columns.Count).End(xlToLeft).Column  'last column
  lr = Range("AG" & Rows.Count).End(xlUp).Row         'last row
  If lc < Columns("AG").Column Then lc = Columns("AG").Column
  If lr < 13 Then lr = 13
  Range(Cells(13, "AG"), Cells([COLOR=#0000ff]lr, lc[/COLOR])).ClearContents
  For i = 0 To DateDiff("m", Range("E5"), Range("E7"))
    Cells(13, Columns("AG").Column + i) = WorksheetFunction.EDate(Range("E5"), i)
    Range("Z31:Z78").Copy Cells([COLOR=#ff0000]14[/COLOR], Columns("AG").Column + i)
  Next [COLOR=#333333][I]End Sub[/I][/COLOR]

One more try, I'm sorry.

Using the macro you created which builds a schedule using two dates and then copys formulas under each column.

I would like to create a sum formula with a range that includes all the formulas that were copied under each column.
 
Upvote 0
You can explain with an example or an image how you want that sum.
 
Upvote 0
You can explain with an example or an image how you want that sum.


Code:
[COLOR=#333333][FONT=Verdana][I]Sub Update_Actuals()[/I][/FONT][/COLOR][FONT=Verdana] Dim i As Long, lc As Long, lr As Long
[/FONT]  
lc = Cells(13, Columns.Count).End(xlToLeft).Column  'last column
  lr = Range("AG" & Rows.Count).End(xlUp).Row         'last row
  If lc < Columns("AG").Column Then lc = Columns("AG").Column
  If lr < 13 Then lr = 13
  Range(Cells(13, "AG"), Cells([COLOR=#0000ff]lr, lc[/COLOR])).ClearContents
  For i = 0 To DateDiff("m", Range("E5"), Range("E7"))
    Cells(13, Columns("AG").Column + i) = WorksheetFunction.EDate(Range("E5"), i)
 [COLOR=#ff0000]   [B]Range("Z31:Z78").Copy Cells(14, Columns("AG").Column + i) [/B][/COLOR]
  Next [COLOR=#333333][FONT=Verdana][I]End Sub[/I][/FONT][/COLOR]

Starting Date: 1/1/2019 (E5)
End Date 3/1/2019 (E7)

using the above dates the Macro above would paste a range of AF14:AH61.

I want to put =SUM(AF14:AH61) in cell AK8.
 
Upvote 0
Try this

Code:
Sub Update_Actuals()
  Dim i As Long, lc As Long, lr As Long
  lc = Cells(13, Columns.Count).End(xlToLeft).Column  'last column
  lr = Range("AG" & Rows.Count).End(xlUp).Row         'last row
  If lc < Columns("AG").Column Then lc = Columns("AG").Column
  If lr < 13 Then lr = 13
  Range(Cells(13, "AG"), Cells(lr, lc)).ClearContents
  For i = 0 To DateDiff("m", Range("E5"), Range("E7"))
    Cells(13, Columns("AG").Column + i) = WorksheetFunction.EDate(Range("E5"), i)
    Range("Z31:Z78").Copy Cells(14, Columns("AG").Column + i)
  Next
[COLOR=#0000ff]  Range("AK7") = WorksheetFunction.Sum(Range("AG14").Resize(63, i))[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,486
Messages
6,113,932
Members
448,533
Latest member
thietbibeboiwasaco

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