Hi all,
I know there are solutions for this but I just need help incorporating them into my code. The code runs 1 time and does not loop because I have 2 directories open, so the Dir() "loses context". If someone could help me include a solution in my code I would be eternally grateful. Here is a link from stack overflow but I am very new to code so I can't really decipher it: Loop Through two different directories using VBA.
Below is the code I currently have:
I know there are solutions for this but I just need help incorporating them into my code. The code runs 1 time and does not loop because I have 2 directories open, so the Dir() "loses context". If someone could help me include a solution in my code I would be eternally grateful. Here is a link from stack overflow but I am very new to code so I can't really decipher it: Loop Through two different directories using VBA.
Below is the code I currently have:
VBA Code:
Sub HiNiRate()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbText As Workbook
Dim wsDest As Worksheet
Dim FinalDest As Workbook
Dim wsFinal As Worksheet
Dim i As Integer
Dim j As Variant
Dim RightSheet As String
Dim Channel As String
Dim SheetExt As String
Dim LastRowA As Long
Dim LastRowE As Long
Dim LastRowK As Long
Const LoadingDir As String = "C:\Users\CharlesMorton\Documents\Coin Cell Loading Measurements\"
Const TextDir As String = "C:\Users\CharlesMorton\Documents\Rate Data\HiNi\"
ChDir TextDir
SheetExt = Dir("*.xl*")
TextExt = Dir("*.txt*")
i = 1
Do While TextExt <> ""
Set wkbText = Workbooks.Open(TextDir & TextExt)
Set FinalDest = Workbooks.Open(TextDir & SheetExt)
Set wsFinal = FinalDest.Worksheets(1)
LastRowA = FinalDest.Worksheets(1).Cells(wsFinal.Rows.Count, "A").End(xlUp).Offset(1).Row
LastRowE = FinalDest.Worksheets(1).Cells(wsFinal.Rows.Count, "E").End(xlUp).End(xlUp).End(xlUp).Offset(1).Row
LastRowK = FinalDest.Worksheets(1).Cells(wsFinal.Rows.Count, "E").End(xlUp).End(xlUp).End(xlUp).Offset(2).Row
With wkbText
Dim TextName As String
Dim Label As String
TextName = .Sheets(1).Range("B1").Value
Label = Mid(TextName, 1, (Len(TextName) - 6))
Channel = Right(TextName, 5)
CellNumber = Left(Channel, 1)
Pnum = InStr(1, TextName, "(")
SampleNum = Mid(TextName, Pnum, 6)
RightSheet = LoadingDir & "*" & SampleNum & "-" & CellNumber & ".xls"
RightSheetName = Dir(RightSheet)
Set wkbDest = Workbooks.Open(LoadingDir & RightSheetName)
wkbDest.Worksheets(2).Range("A1:F35").Value = .Sheets(1).Range("A1:F35").Value
.Close savechanges:=False
wsFinal.Range("E" & LastRowE & ":" & "K" & LastRowK).Value = wkbDest.Worksheets(1).Range("U54:AA55").Value
wkbDest.Close savechanges:=True
If Int(j) Then
wsFinal.Range("A" & LastRowA).Value = Label
wsFinal.Range("P2:AB11").Select
Selection.Copy
wsFinal.Range("A" & LastRowA + 10).Select
wsFinal.Paste
End If
End With
i = i + 1
TextExt = Dir()
Loop
End Sub
Last edited by a moderator: