Hello! I am working to build a dynamic for loop/if statement but I am at my limits of VBA knowledge and need some help!
Objective: Loop through a dynamic range of asset, liability, and net asset cells that can change in row and column size, copy values that are populated (skipping blanks) and also grab the associated Fund Code value in Column B and account number in Row 4 of source file .
Attached Images: I have uploaded images of my source file and destination file (you can see what my code currently returns in the destination file)
Current State: My code currently works great for fixed cells but I need to expand it to accommodate more columns/rows. It currently loops through column D in the source file, skips the blanks, and returns the associated values in column B and pastes them in columns D and B of the destination file respectively, it also copies and pastes the fixed value in cell A3 and Cell D4 to column A and C in the destination file.
Goal: What I want to do to my code is make the source region dynamic, so it will loop through column D AND all columns and rows (ignoring the 5 header rows) to the right of column D and grab any populated values. I also want to make the acccount_num in source file D4 dynamic so it will grab the account number associated with the column it grabs the $$ value from. A source template that is filled out may have more or less Funds (columns) and Assets/liabilities/net assets (rows) so that's why it needs to be dynamic. I think I need to add a double for loop but not sure.
Let me know if I need to clarify anything!
Here is my code so far:
Objective: Loop through a dynamic range of asset, liability, and net asset cells that can change in row and column size, copy values that are populated (skipping blanks) and also grab the associated Fund Code value in Column B and account number in Row 4 of source file .
Attached Images: I have uploaded images of my source file and destination file (you can see what my code currently returns in the destination file)
Current State: My code currently works great for fixed cells but I need to expand it to accommodate more columns/rows. It currently loops through column D in the source file, skips the blanks, and returns the associated values in column B and pastes them in columns D and B of the destination file respectively, it also copies and pastes the fixed value in cell A3 and Cell D4 to column A and C in the destination file.
Goal: What I want to do to my code is make the source region dynamic, so it will loop through column D AND all columns and rows (ignoring the 5 header rows) to the right of column D and grab any populated values. I also want to make the acccount_num in source file D4 dynamic so it will grab the account number associated with the column it grabs the $$ value from. A source template that is filled out may have more or less Funds (columns) and Assets/liabilities/net assets (rows) so that's why it needs to be dynamic. I think I need to add a double for loop but not sure.
Let me know if I need to clarify anything!
Here is my code so far:
VBA Code:
Public Sub import_FBI_data()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim i As Long
Dim j As Long
Dim lastrow1 As Long
Dim MY_LAST_ROW As Long
Application.ScreenUpdating = False
'Pop up to allow user to select which file they want to use as the source
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen <> False Then
'Set variables for the source and destination workbooks.worksheets
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set wsSource = OpenBook.Worksheets("Template")
Set wsDest = ThisWorkbook.Worksheets("gl_transactions")
'Clear contents of destination range
wsDest.Range("A3:A800").EntireRow.ClearContents
'Identify last row and column of the source workbook and starting point for the double loop
lastrow1 = wsSource.Cells(Rows.Count, 1).End(xlUp).Offset(-3, 0).Row
'Loop through every row and column starting in row 6 column D and copy over any data that isn't blank with the associated values
For i = 6 To lastrow1 Step 1
If Not IsEmpty(Cells(i, "D")) Then
amount = wsSource.Cells(i, 4).Value
fund_code = wsSource.Cells(i, 2).Value
account_num = wsSource.Cells(4, 4).Value
post_code = wsSource.Cells(3, 1).Value
'Identify last row of destination workbook
MY_LAST_ROW = wsDest.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'Identify where FBI data will copy to in destination workbook
With wsDest
wsDest.Cells(MY_LAST_ROW, 4).Value = amount
wsDest.Cells(MY_LAST_ROW, 2).Value = fund_code
wsDest.Cells(MY_LAST_ROW, 3).Value = account_num
wsDest.Cells(MY_LAST_ROW, 1).Value = post_code
End With
End If
Next i
End If
OpenBook.Close False
Application.ScreenUpdating = True
End Sub