PDA

View Full Version : [SOLVED:] Combining VBA Code--Some Help



rsrasc
11-11-2023, 08:21 AM
Hi all,

Just wondering if the following codes can be combined in one.






Sub Copy_Column_Formulas_025_Budget()


Workbooks("025 FY 2024 Expenses-SSE-Budget.xlsx").Sheets("FY24-Monthly Variance Report").Activate




Dim rng As Range
Dim lastRow As Long
Dim i As Long




On Error GoTo sub_exit


Application.Calculation = xlCalculationManual


Set rng = Range("H2:AE2" & lastRow)
lastRow = Cells(Rows.Count, "F").End(xlUp).Row

For i = 1 To lastRow

If Cells(i, "F").Value = "Budget" Then

Cells(i, "H").Copy
Cells(i, "I").Resize(, 23).PasteSpecial Paste:=xlPasteFormulas
End If



Next


sub_exit:

Application.Calculation = xlCalculationAutomatic
End Sub








Sub Copy_Column_Formulas_025_Change()


Workbooks("025 FY 2024 Expenses-SSE-Budget.xlsx").Sheets("FY24-Monthly Variance Report").Activate




Dim rng As Range
Dim lastRow As Long
Dim i As Long


On Error GoTo sub_exit


Application.Calculation = xlCalculationManual


Set rng = Range("H2:AE2" & lastRow)
lastRow = Cells(Rows.Count, "F").End(xlUp).Row

For i = 1 To lastRow


If Cells(i, "F").Value = "% Change" Then

Cells(i, "H").Copy
Cells(i, "I").Resize(, 23).PasteSpecial Paste:=xlPasteFormulas
End If
Next


sub_exit:

Application.Calculation = xlCalculationAutomatic
End Sub

Thanks!

p45cal
11-11-2023, 09:24 AM
Sub both()
Dim lastRow As Long
Dim i As Long
Workbooks("025 FY 2024 Expenses-SSE-Budget.xlsx").Sheets("FY24-Monthly Variance Report").Activate
On Error GoTo sub_exit
Application.Calculation = xlCalculationManual
lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For i = 1 To lastRow
If Cells(i, "F").Value = "Budget" Or Cells(i, "F").Value = "% Change" Then
Cells(i, "H").Copy
Cells(i, "I").Resize(, 23).PasteSpecial Paste:=xlPasteFormulas
End If
Next
sub_exit:
Application.Calculation = xlCalculationAutomatic
End Sub
There is no point in setting rng; it's never used (besides, it looks dodgy with that 2 in it: Set rng = Range("H2:AE2" & lastRow) and it uses lastRow before it has been assigned anything!)

rsrasc
11-11-2023, 10:01 AM
Thank you p45cal for your prompt response. This will really help me with 25 files.