Hello,
I was wondering if anyone could help me figure out this code. I have been struggling to figure out what is wrong with my code. My goal is for the macro to read through all the different workbooks in a folder I have. The code seems to work, but then when it loops, it instead decides to just loop the same workbook. Here is the code below and I am also a beginner so any help is appreciated. Thanks for the help!
Sub TransferData()
Dim wbFile As Workbook, wsData As Worksheet, wbDataBase As Workbook, wsDB As Worksheet,
Dim strPath As String, strFile As String
Dim DBPath As String, wbDB As String, DBFile As String
Dim DataPath As String, DBDataPath As String
Dim lngIdx As Long, lngLastRow As Long, lngLastCol As Long, lngLastRowDB As Long, lngLastColDB As Long
Dim lngDstLastRow As Long
Dim rngCopy As Range, rngPaste As Range
Dim colFileNames As Collection
Set colFileNames = New Collection
strPath = "H:\*FolderName*"
strFile = Dir(strPath & "\*xlsm")
DBPath = "H:\*FolderName2*"
DBFile = Dir(DBPath & "\*xlsm")
Do While Len(strFile) > 0
colFileNames.Add (strFile)
strFile = Dir
Loop
For lngIdx = 1 To colFileNames.Count
DataPath = strPath & "" & colFileNames(lngIdx)
Set wbFile = Workbooks.Open(DataPath)
Set wsData = wbFile.Worksheets("Data")
lngLastRow = LastRow(wsData)
lngLastCol = LastRow(wsData)
With wsData
Set rngCopy = .Range(.Cells(3, 2), .Cells(lngLastRow, 6))
End With
DBDataPath = DBPath & DBFile
Set wbDataBase = Workbooks.Open(DBDataPath)
Set wsDB = wbDataBase.Worksheets("DataBase")
lngLastRowDB = LastRow(wsDB)
lngLastColDB = LastCol(wsDB)
If lngIdx > 0 Then
lngDstLastRow = LastRow(wsDB)
Set rngPaste = wsDB.Cells(lngLastRowDB + 1, 2)
End If
rngCopy.Copy
wsDB.Cells(lngLastRowDB + 1, 2).Select
ActiveCell.PasteSpecial
ActiveWorkbook.Close
wsData.Select
ActiveWorkbook.Close
Next
End Sub
I was wondering if anyone could help me figure out this code. I have been struggling to figure out what is wrong with my code. My goal is for the macro to read through all the different workbooks in a folder I have. The code seems to work, but then when it loops, it instead decides to just loop the same workbook. Here is the code below and I am also a beginner so any help is appreciated. Thanks for the help!
Sub TransferData()
Dim wbFile As Workbook, wsData As Worksheet, wbDataBase As Workbook, wsDB As Worksheet,
Dim strPath As String, strFile As String
Dim DBPath As String, wbDB As String, DBFile As String
Dim DataPath As String, DBDataPath As String
Dim lngIdx As Long, lngLastRow As Long, lngLastCol As Long, lngLastRowDB As Long, lngLastColDB As Long
Dim lngDstLastRow As Long
Dim rngCopy As Range, rngPaste As Range
Dim colFileNames As Collection
Set colFileNames = New Collection
strPath = "H:\*FolderName*"
strFile = Dir(strPath & "\*xlsm")
DBPath = "H:\*FolderName2*"
DBFile = Dir(DBPath & "\*xlsm")
Do While Len(strFile) > 0
colFileNames.Add (strFile)
strFile = Dir
Loop
For lngIdx = 1 To colFileNames.Count
DataPath = strPath & "" & colFileNames(lngIdx)
Set wbFile = Workbooks.Open(DataPath)
Set wsData = wbFile.Worksheets("Data")
lngLastRow = LastRow(wsData)
lngLastCol = LastRow(wsData)
With wsData
Set rngCopy = .Range(.Cells(3, 2), .Cells(lngLastRow, 6))
End With
DBDataPath = DBPath & DBFile
Set wbDataBase = Workbooks.Open(DBDataPath)
Set wsDB = wbDataBase.Worksheets("DataBase")
lngLastRowDB = LastRow(wsDB)
lngLastColDB = LastCol(wsDB)
If lngIdx > 0 Then
lngDstLastRow = LastRow(wsDB)
Set rngPaste = wsDB.Cells(lngLastRowDB + 1, 2)
End If
rngCopy.Copy
wsDB.Cells(lngLastRowDB + 1, 2).Select
ActiveCell.PasteSpecial
ActiveWorkbook.Close
wsData.Select
ActiveWorkbook.Close
Next
End Sub