Hi all,
I've modified a macro to pull values from other workbooks' sheets and use it to populate the active workbook's Sheet1. So far, it works with the getting values from other workbooks' sheets part, but the part I'm having trouble with is populating the active workbook's sheet. The latter is mostly working, but the problem is I'm only able to populate the values from the second sheet of an external workbook that contains two sheets. However, the print debug shows the values are being grabbed correctly from both sheets, one after the other.
Right now, the vba populates the active sheet with values from external workbook sheet1, but instead of continuing to the next empty row, it begins populating from the start again, this time with the external workbook's sheet2, overwriting the previous values that were grabbed. Here is my code (please ignore some of the xperimental commented code):
How would I get it to continue populating the values in sequence (next empty row), after the current sequence ends?
Thanks!
I've modified a macro to pull values from other workbooks' sheets and use it to populate the active workbook's Sheet1. So far, it works with the getting values from other workbooks' sheets part, but the part I'm having trouble with is populating the active workbook's sheet. The latter is mostly working, but the problem is I'm only able to populate the values from the second sheet of an external workbook that contains two sheets. However, the print debug shows the values are being grabbed correctly from both sheets, one after the other.
Right now, the vba populates the active sheet with values from external workbook sheet1, but instead of continuing to the next empty row, it begins populating from the start again, this time with the external workbook's sheet2, overwriting the previous values that were grabbed. Here is my code (please ignore some of the xperimental commented code):
VBA Code:
Sub DataPull()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
Dim cellRange As Range
Set cellRange = Range("A1:A5")
Dim Cell As Range
Dim val As String
Dim ii As Integer
Dim j As Integer
fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel files to merge", MultiSelect:=True)
If (vbBoolean <> VarType(fnameList)) Then
If (UBound(fnameList) > 0) Then
countFiles = 0
countSheets = 0
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbkCurBook = ActiveWorkbook
For Each fnameCurFile In fnameList
countFiles = countFiles + 1
Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile)
For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
For j = 1 To 10
'Get values from each individual sheet
For ii = 1 To 5
If wksCurSheet.Cells(j, ii).Value = "" Then
Exit For
End If
Debug.Print wksCurSheet.Cells(j, ii).Value 'working
Cells(j, ii).Value = wksCurSheet.Cells(j, ii).Value
' For Each Cell In Sheets("Sheet1").Range("A1:X10")
' Cell.Offset(j, ii).Value = wksCurSheet.Cells(j, ii).Value
' Next Cell
Next ii
'Debug.Print wksCurSheet.Range("A1").Value 'working
'Debug.Print wksCurSheet.Range("A1:E1").Value
'Debug.Print wksCurSheet.Cells(1, 4).Value 'working
If wksCurSheet.Cells(j, 1).Value = "" Then
Exit For
End If
Next j
Next
wbkSrcBook.Close SaveChanges:=False
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
'MsgBox "Processed " & countFiles & " files" & vbCrLf & "Merged " & countSheets & " worksheets", Title:="Merge Excel files"
End If
Else
'MsgBox "No files selected", Title:="Merge Excel files"
End If
End Sub
How would I get it to continue populating the values in sequence (next empty row), after the current sequence ends?
Thanks!