jordanburch
Active Member
- Joined
- Jun 10, 2016
- Messages
- 443
- Office Version
- 2016
Hey guys
I have the below code. it loops through and imports the active sheet, but I want it to import the sheet with the partial name of "Clear". It only import the active sheet when it opens the file. Can anyone help?
Sub LoopThroughFiles()
Dim MyObj As Object
Dim MySource As Object
Dim file As Variant
Dim wbThis As Workbook 'workbook where the data is to be pasted, aka Master file
Dim wbTarget As Workbook 'workbook from where the data is to be copied from, aka Overnights file
Dim LastRow As Long
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Application.DisplayAlerts = False
'set to the current active workbook (the source book, the Master!)
Set wbThis = ActiveWorkbook
Set sht1 = wbThis.Sheets("Cleared")
Folder = "C:\Users\jordan.burch.ctr\Desktop\Cert Statements\"
Fname = Dir(Folder)
While (Fname <> "")
Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
wbTarget.Activate
Dim ws As Worksheet
Dim ClearedSheet As String
ClearedSheet = ""
For Each ws In ActiveWorkbook.Worksheets
If InStr(1, ws.Name, "Clear", vbTextCompare) Then
ClearedSheet = ws.Name
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
Range("a2:aw40000").Copy
Exit For
End If
Next
If ClearedSheet <> "" Then
' Sheets("Cleared - Cleared to").Activate
' With ActiveSheet
' If .AutoFilterMode Then
' If .FilterMode Then
' .ShowAllData
' End If
' Else
' If .FilterMode Then
' .ShowAllData
' End If
' End If
' End With
Range("a2:aw40000").Copy
wbThis.Activate
'Just add this line:
LastRow = sht1.Range("b1").End(xlDown).Row + 1
'And alter this one as follows:
sht1.Range("a" & Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial
Fname = Dir
'close the overnight's file
wbTarget.Close
End If
Wend
Application.DisplayAlerts = True
End Sub
Thanks,
Jordan
I have the below code. it loops through and imports the active sheet, but I want it to import the sheet with the partial name of "Clear". It only import the active sheet when it opens the file. Can anyone help?
Sub LoopThroughFiles()
Dim MyObj As Object
Dim MySource As Object
Dim file As Variant
Dim wbThis As Workbook 'workbook where the data is to be pasted, aka Master file
Dim wbTarget As Workbook 'workbook from where the data is to be copied from, aka Overnights file
Dim LastRow As Long
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Application.DisplayAlerts = False
'set to the current active workbook (the source book, the Master!)
Set wbThis = ActiveWorkbook
Set sht1 = wbThis.Sheets("Cleared")
Folder = "C:\Users\jordan.burch.ctr\Desktop\Cert Statements\"
Fname = Dir(Folder)
While (Fname <> "")
Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
wbTarget.Activate
Dim ws As Worksheet
Dim ClearedSheet As String
ClearedSheet = ""
For Each ws In ActiveWorkbook.Worksheets
If InStr(1, ws.Name, "Clear", vbTextCompare) Then
ClearedSheet = ws.Name
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
Range("a2:aw40000").Copy
Exit For
End If
Next
If ClearedSheet <> "" Then
' Sheets("Cleared - Cleared to").Activate
' With ActiveSheet
' If .AutoFilterMode Then
' If .FilterMode Then
' .ShowAllData
' End If
' Else
' If .FilterMode Then
' .ShowAllData
' End If
' End If
' End With
Range("a2:aw40000").Copy
wbThis.Activate
'Just add this line:
LastRow = sht1.Range("b1").End(xlDown).Row + 1
'And alter this one as follows:
sht1.Range("a" & Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial
Fname = Dir
'close the overnight's file
wbTarget.Close
End If
Wend
Application.DisplayAlerts = True
End Sub
Thanks,
Jordan