Dear All,
I have written a macro and having hard time running nested Loops properly. Please see the Code below:
The aim is to copy data from a Data worksheet based on filters (one filter value is taken from Template and column number corresponds to this) and paste value to Template in the correct column and row. Row values are different i.e. not a simple count, need (9, 10, 14, 15, 85, 86 etc).
Because my nested loop does not work I have given x (need 9, 10, 14, 15, 85, 86 etc) and z (need 12, 13, 14, 15) numbers. My other code runs perfectly and copies data looping through columns etc. Please let me know what changes I could make to run nested loops rather than copy and paste the same code several times.
Thanks,
Shah.
I have written a macro and having hard time running nested Loops properly. Please see the Code below:
The aim is to copy data from a Data worksheet based on filters (one filter value is taken from Template and column number corresponds to this) and paste value to Template in the correct column and row. Row values are different i.e. not a simple count, need (9, 10, 14, 15, 85, 86 etc).
Because my nested loop does not work I have given x (need 9, 10, 14, 15, 85, 86 etc) and z (need 12, 13, 14, 15) numbers. My other code runs perfectly and copies data looping through columns etc. Please let me know what changes I could make to run nested loops rather than copy and paste the same code several times.
Code:
Option Explicit
Sub LoopOne()
Dim sh As Worksheet 'Macro worksheet
Dim wb As Workbook 'Template
Dim ToSht As Worksheet 'Current Month Data sheet on Template
Dim var As Variant 'Sheet Name
Dim FrmSht As Variant
Dim j As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim lw As Long
Dim Num As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Open Template and data files
Set sh = Sheet2 ' Macro worksheet
Set wb = Workbooks.Open(sh.[E2]) ' Open Template workbook
Set ToSht = Worksheets(sh.[C2].Value) ' Current month data sheet on Template
j = 3 'row number on macro sheet to pick up the file path
Set wb = Workbooks.Open(sh.Range("E" & j)) 'File Path & Name for data file
var = sh.Range("C2", sh.Range("C" & Rows.Count).End(xlUp)) ' Select sheet name from Macro Worksheet
Set FrmSht = wb.Sheets(var(j - 1, 1))
Num = 45
For y = 10 To Num 'Column numbers to loop through from Template
lw = FrmSht.Range("A" & Rows.Count).End(xlUp).Row
FrmSht.Range("A6:R" & lw).AutoFilter Field:=1, Criteria1:=ToSht.Cells(2, y).Value 'Filter on datasheet
FrmSht.Range("A6:R" & lw).AutoFilter Field:=3, Criteria1:="TOTAL INVESTMENTS" 'Filter on datasheet
x = 9 'row number on the Template
z = 12 'column number on the datasheet
ToSht.Cells(x, y) = Application.WorksheetFunction.Sum(FrmSht.Range(Cells(5, z), Cells(500, z)).SpecialCells(xlCellTypeVisible)) 'sum column after filter is applied
ToSht.Cells(x, y) = ToSht.Cells(x, y).Value - ToSht.Cells(12, y).Value
FrmSht.AutoFilterMode = False
Next
wb.Close 'Close the workbook
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Thanks,
Shah.