Using column count to end of range

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
70
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!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,938
Office Version
2007
Platform
Windows
From which initial cell to which final cell do you want to put the formula.
What are the references? That is, which row or column have data and based on that data put the formulas.
 

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
70
From which initial cell to which final cell do you want to put the formula.
What are the references? That is, which row or column have data and based on that data put the formulas.
The final cell would be dynamic based on how many months between the beginning date (E5) and the final date (E7).

Inputs

E5 = 1/1/2019
E7 = 4/1/2019


Output, starting in AG13:
AG13AH13AI13AJ13
1/1/20192/1/20193/1/20194/1/2019

<tbody>
</tbody>

<tbody></tbody>
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,938
Office Version
2007
Platform
Windows
Try this
The formula in Y8 is no longer necessary

Code:
Sub Update_Actuals()
  Dim i As Long
  For i = 0 To DateDiff("m", Range("E5"), Range("E7"))
    Cells(13, Columns("AG").Column + i) = WorksheetFunction.EDate(Range("E5"), i)
  Next
End Sub
 

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
70
DanteAmor Thank you so much! this works great!

1.
I have additional formulas that I save at z31:Z78 that i want to copy under the dates which would be dynamic.

How can i reference the last column of the dates in row AG to allow for me to paste dynamically under the dates in AG?

2. Once I know the last column used I will want to add code at the beginning to remove the prior data before running the macro
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,938
Office Version
2007
Platform
Windows
Try this

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

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
70
This works perfectly!! thank you!

Is there an easy way to incorporate copying the formulas in Z31:Z78 to the if statement? They would be pasted under AG13.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,938
Office Version
2007
Platform
Windows
This works perfectly!! thank you!

Is there an easy way to incorporate copying the formulas in Z31:Z78 to the if statement? They would be pasted under AG13.

You just want to copy the cells in the Z31:Z78 under AG13, also under AH13 and so on?
 

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
70
You just want to copy the cells in the Z31:Z78 under AG13, also under AH13 and so on?
Yes under all the dates that are populated dynamically.

And deleting the formulas at the beginning before running the macros.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,938
Office Version
2007
Platform
Windows
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
 

Forum statistics

Threads
1,078,486
Messages
5,340,622
Members
399,387
Latest member
amrita34

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top