Help! VBA to loop through a list containing data references to update forecast sheets automatically

RMCHE5280

New Member
Joined
Apr 5, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have about ten forecast sheets that need to be updated weekly. Updating the plots is very time-consuming, so I decided to try my hand at a macro that would loop through them to update them automatically. Eventually, I will have the macro delete the old forecast sheets and replace them with the autogenerated sheets. However, I need help figuring out how to generate the forecast sheets automatically using VBA and a loop. While I am not new to VBA, it's also something that I only use a few times a year. For this reason, I'm certain there's a more concise method than the one I have been attempting. Can someone point me in the right direction?

Also, the data in the "hourly results" sheet is really about 10,000 rows long, if that matters, but I can create the forecast sheets manually without trouble... it just takes an hour or so to format everything, which I can also do using VBA if I can only get this part working. Thank you again for your help!

Here is my VBA code:

VBA Code:
Sub ForecastGen()

Dim Pgm As Worksheet
Set Pgm = ActiveWorkbook.Sheets("Programming")

Dim DtaRng As Worksheet
Set DtaRng = ActiveWorkbook.Sheets("Hourly Results")

Dim DateLocation As Range
Set DateLocation = Pgm.Range("G2")
MsgBox DateLocation

Dim Location(1 To 10) As Range
Dim k As Integer

For k = 1 To 10
Set Location(k) = Pgm.Cells(k + 1, 6)
MsgBox Location(k)

    ActiveWorkbook.CreateForecastSheet Timeline:=Sheets(DtaRng).Range(DateLocation _
        ), Values:=Sheets(DtaRng).Range(Location), ForecastEnd:= _
        "4/1/2024  12:00:00 AM", ConfInt:=0.95, Seasonality:=1, ChartType:= _
        xlForecastChartTypeLine, Aggregation:=xlForecastAggregationAverage, _
        DataCompletion:=xlForecastDataCompletionInterpolate, ShowStatsTable:=False
       
Next k
   
End Sub

Mini-sheet upload did not work for me, so I am trying to get my data in the rudimentary way.

Here is an example of the information in the "Programming" sheet:

IDPlot NameColumnStartEndDataRangeDateRange
1​
Plot 1B
2​
200​
B2:B200A2:A200
2​
Plot 2C
2​
200​
C2:C200A2:A200
3​
Plot 3D
2​
200​
D2:D200A2:A200
4​
Plot 4E
2​
200​
E2:E200A2:A200
5​
Plot 5F
2​
200​
F2:F200A2:A200
6​
Plot 6G
2​
200​
G2:G200A2:A200
7​
Plot 7H
2​
200​
H2:H200A2:A200
8​
Plot 8I
2​
200​
I2:I200A2:A200
9​
Plot 9J
2​
200​
J2:J200A2:A200
10​
Plot 10K
2​
200​
K2:K200A2:A200

Here is an example of the data in the "Hourly Results" sheet:

DatePlot 1Plot 2Plot 3Plot 4Plot 5Plot 6Plot 7Plot 8Plot 9Plot 10
1/1/24 0:00​
2.042377​
2.04958​
2.050379​
2.047442​
2.047692​
2.054683​
2.053042​
2.033847​
2.039204​
2.208346​
1/1/24 1:00​
2.042376​
2.049579​
2.05038​
2.047443​
2.047692​
2.054683​
2.053045​
2.033847​
2.039204​
2.208343​
1/1/24 2:00​
2.042376​
2.049578​
2.050381​
2.047444​
2.047693​
2.054684​
2.053047​
2.033846​
2.039204​
2.208338​
1/1/24 3:00​
2.042376​
2.049577​
2.050382​
2.047444​
2.047693​
2.054684​
2.053049​
2.033846​
2.039203​
2.208334​
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try the following...

VBA Code:
    ActiveWorkbook.CreateForecastSheet _
        Timeline:=DtaRng.Range(DateLocation.Value), _
        Values:=DtaRng.Range(Location(k)), _
        ForecastEnd:="4/1/2024  12:00:00 AM", _
        ConfInt:=0.95, _
        Seasonality:=1, _
        ChartType:=xlForecastChartTypeLine, _
        Aggregation:=xlForecastAggregationAverage, _
        DataCompletion:=xlForecastDataCompletionInterpolate, _
        ShowStatsTable:=False

By the way, there's no need to use a "Programming" sheet. You can achieve the same results without it. Or is there some reason for using it?

Hope this helps!
 
Upvote 0
Domenic: Thank you for your help. I'll try your code shortly. My thought with the programming sheet is that the dataset size is dynamic—it's continually growing. Also, the data in the "hourly results" table contains some blank cells. When I attempted to specify the last active cell using VBA in my earlier attempts, the blank cells would throw it off, and the entire dataset would not be included in the forecast sheet. Just for my own education, what more eloquent solution should I have used in place of the "programming" sheet? Thanks again!
 
Upvote 0
A dynamic range could easily be dealt with. However, as you already know, those blank cells will cause an error.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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