VBA Procedure Won't Complete

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

I received a file that I need to reformat to upload to a database. The current file is setup where one row represents one client, with twelve values for financial projections for each month of next year. I need this formatted with the same attributes, except i need one row for each monthly projection. I set up this procedure to copy Columns A through J, then to to copy the month/yr combo (eg -- 202301 for January) into column K, and then the value for that month in column L. So I have 1248 rows, and I would expect this program to easily create 14,976.

But my program only successfully completes about 50 inner loops before getting hung up. I tried to leave my computer running, but it just froze. I am not sure if there is a memory leak somewhere, or if this is just an inefficient procedure. Are there some simple changes I can make to get this to finish?

Any input is appreciated!

Below is an example of part of the table ... I want to repeat the strings in State, County and Market, then copy the 2023XY label, then copy the value that was under the 2023XY Column to the cell beside the new paste.

StateCountyMarket202301202302
NCBuncombeAsheville500.00250.00

Here is what I want it to look like:
StateCountyMarketMonth_YR_NumBudget
NCBuncombeAsheville202301500.00
NCBuncombeAsheville202302250.00

VBA Code:
Public Sub Auto_Budget_Prep_1()
'Macro to assist in preparing 2023 Budget for upload


'Error Handling
On Error GoTo 0


    'Declare Variables
    Dim macroWB As Workbook
    Dim srcWS1 As Worksheet
    Dim srcWS2 As Worksheet
               
    'Turn off notifications
    With Application
        .ScreenUpdating = False
'                .ScreenUpdating = True  --using comments to test this portion and see if the code was running
               
        .DisplayAlerts = False
    End With
   
    'Bind Objects
        Set macroWB = ThisWorkbook
            Set srcWS1 = macroWB.Worksheets("stage")
            Set srcWS2 = macroWB.Worksheets("budget")

                'COPY EACH ROW B:F 12 TIMES --- 1X FOR EACH Month
               

            'Find last row of each destination / summary file worksheet
            Dim NumRows As Long: NumRows = srcWS1.Range("A1", srcWS1.Range("A1").End(xlDown)).Rows.Count
     
            'need to create a month_yr_num stamp
            'Dim month7 As String: month7 = "202301"
            'Dim month8 As String: month8 = "202302"
            'Dim month9 As String: month9 = "202303"
            'Dim month10 As String: month10 = "202304"
            'Dim month11 As String: month11 = "202305"
            'Dim month12 As String: month12 = "202306"
            'Dim month13 As String: month13 = "202307"
            'Dim month14 As String: month14 = "202308"
            'Dim month15 As String: month15 = "202309"
            'Dim month16 As String: month16 = "202310"
            'Dim month17 As String: month17 = "202311"
            'Dim month18 As String: month18 = "202312"
     
     
           
            Dim i, j As Integer
            'declare k for row counter on srcsheet2
            'this variable is used to count rows for budget sheet ws2, it is not to be destroyed by loop
            j = 2
           
                'loop through each row stage sheet
                For i = 2 To 20
                 'For i = 2 To NumRows
                   
                    'this variable to be used for 12 columns / months
                    Dim k As Integer
                   
                        'inner loop will run once for each month of year
                        For k = 11 To 22
                        'first copy B:F to budget sheet
                        srcWS1.Range("A" & i & ":J" & i).Copy
                        srcWS2.Range("A" & j & ":J" & j).PasteSpecial xlPasteValues
                       
                        'now copy month_yr_num stamp col 11
                        srcWS1.Cells(1, k).Copy
                        srcWS2.Cells(j, 11).PasteSpecial xlPasteValues
                       
                        'now copy political budget value col 12
                        srcWS1.Cells(i, k).Copy
                        srcWS2.Cells(j, 12).PasteSpecial xlPasteValues
                       
                        'increment j
                        j = j + 1

                        'increment k
                        Next k
               
                'increment i
                Next i
      
      
        'Close out variables
        'Set j = Nothing
        'Set i = Nothing
        'Set NumRows = Nothing
        Set srcWS1 = Nothing
        Set srcWS2 = Nothing
        Set macroWB = Nothing

'Turn on notifications
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

'Message
MsgBox "Done!"

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
in Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = Table.UnpivotOtherColumns(Source, {"State", "County", "Market"}, "Month_YR_Num", "Budget")
in
    Result

Book1
ABCDEFGHIJKL
1StateCountyMarket202301202302StateCountyMarketMonth_YR_NumBudget
2NCBuncombeAsheville500250NCBuncombeAsheville202301500
3NCBuncombeAsheville202302250
4
5
Sheet2
 
Upvote 0
Hi AlexB123,

you might need to adapt the column letters as you have shown data without information about the columns:

MrE_1225491_1616912_vba procedure wont_221227.xlsm
ABCDEFGHIJKLMNO
1StateCountyMarket202301202302202303202304202305202306202307202308202309202310202311202312
2NCBuncombeAsheville645,00620,00215,00262,00378,00638,00361,00488,00306,00720,00203,00593,00
3CASan BernadinoFort Irwin357,00540,00738,00353,00693,00630,00741,00485,00661,00712,00628,00656,00
stage


MrE_1225491_1616912_vba procedure wont_221227.xlsm
ABCDE
1StateCountyMarketMonth_YR_NumBudget
2NCBuncombeAsheville202301645,00
3NCBuncombeAsheville202302620,00
4NCBuncombeAsheville202303215,00
5NCBuncombeAsheville202304262,00
6NCBuncombeAsheville202305378,00
7NCBuncombeAsheville202306638,00
8NCBuncombeAsheville202307361,00
9NCBuncombeAsheville202308488,00
10NCBuncombeAsheville202309306,00
11NCBuncombeAsheville202310720,00
12NCBuncombeAsheville202311203,00
13NCBuncombeAsheville202312593,00
14CASan BernadinoFort Irwin202301357,00
15CASan BernadinoFort Irwin202302540,00
16CASan BernadinoFort Irwin202303738,00
17CASan BernadinoFort Irwin202304353,00
18CASan BernadinoFort Irwin202305693,00
19CASan BernadinoFort Irwin202306630,00
20CASan BernadinoFort Irwin202307741,00
21CASan BernadinoFort Irwin202308485,00
22CASan BernadinoFort Irwin202309661,00
23CASan BernadinoFort Irwin202310712,00
24CASan BernadinoFort Irwin202311628,00
25CASan BernadinoFort Irwin202312656,00
budget


VBA Code:
Public Sub MrE_1225491_1616912()
' https://www.mrexcel.com/board/threads/vba-procedure-wont-complete.1225491/

'Macro to assist in preparing 2023 Budget for upload

'Declare Variables
Dim wsStage As Worksheet
Dim wsBudg As Worksheet
Dim NumRows As Long
Dim lngCounter As Long

'Turn off notifications
With Application
  .ScreenUpdating = False
  .DisplayAlerts = False
End With

'Error Handling
On Error GoTo 0

'Bind Objects
Set wsStage = ThisWorkbook.Worksheets("stage")
Set wsBudg = ThisWorkbook.Worksheets("budget")

With wsBudg
  .UsedRange.ClearContents
  .Range("A1").Resize(1, 5).Value = Array("State", "County", "Market", "Month_YR_Num", "Budget")
End With

'loop through each row stage sheet
For lngCounter = 2 To wsStage.Range("A" & wsStage.Rows.Count).End(xlUp).Row
  With wsBudg.Range("A" & wsBudg.Cells(wsBudg.Rows.Count, 1).End(xlUp).Offset(1, 0).Row)
    .Resize(12, 1).Value = wsStage.Cells(lngCounter, "A").Value
    .Offset(0, 1).Resize(12, 1).Value = wsStage.Cells(lngCounter, "B").Value
    .Offset(0, 2).Resize(12, 1).Value = wsStage.Cells(lngCounter, "C").Value
    .Offset(0, 3).Resize(12, 1).Value = WorksheetFunction.Transpose(wsStage.Cells(1, "D").Resize(1, 12).Value)
    With .Offset(0, 4).Resize(12, 1)
      .Value = WorksheetFunction.Transpose(wsStage.Cells(lngCounter, "D").Resize(1, 12).Value)
      .NumberFormat = "#,##0.00"
    End With
  End With
Next lngCounter

'Close out variables
Set wsStage = Nothing
Set wsBudg = Nothing

'Turn on notifications
With Application
  .ScreenUpdating = True
  .DisplayAlerts = True
End With

'Message
MsgBox "Done!"
End Sub

Ciao,
Holger
 
Upvote 0
Solution
Another alterantive:
VBA Code:
Sub test()
Dim outarr()
Lastcol = 15 ' I assume the last month is in column O (15)
With Worksheets("stage")
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
inarr = .Range(.Cells(1, 1), .Cells(lastrow, Lastcol))
End With
ReDim outarr(1 To lastrow * 12, 1 To 5)
indi = 1
For i = 2 To lastrow
 For k = 4 To 15
    For j = 1 To 3
     outarr(indi, j) = inarr(i, j)
    Next j
    outarr(indi, 5) = inarr(i, k)
    indi = indi + 1
 Next k
Next i
With Worksheets("budget")
 .Range(.Cells(1, 1), .Cells(lastrow * 12, 5)) = outarr
End With
End Sub
This uses varaint arrays so will be super fast
 
Upvote 0
Here another macro for you to consider:

The data in the "stage" sheet begins and in cell A1. Results in cell A2 on sheet "budget"

VBA Code:
Sub Auto_Budget_Prep_2()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, m As Long
  
  Set sh1 = Sheets("stage")     'source
  Set sh2 = Sheets("budget")    'destination
  
  a = sh1.Range("A1:V" & sh1.Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1) * 12, 1 To 12)
  
  For i = 2 To UBound(a, 1)
    For j = 11 To 22
      m = m + 1
      For k = 1 To 10
        b(m, k) = a(i, k)
      Next
      b(m, 11) = a(1, j)    'YearMonth
      b(m, 12) = a(i, j)    'Budget
    Next
  Next
  
  Sheets("budget").Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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