Hi everyone,
I have an existing macro which works pretty well. I currently run through all workbooks in a folder, identifies worksheet names which match worksheet names in the master workbook and copies the data from the external workbook worksheet to the master workbook worksheet. What I'm trying to do is adjust the macro to do the following:
1. run through each workbook in a folder
2. if the workbook name equals a cell value in ThisWorkbook.Sheet1.range("B2:B50") then copy Worksheet("ABC") entire used range into Workbooks(2) with the worksheet that matches the external workbook name.
3. move to the next cell in the range and repeat as above
So for example, lets cell B2 in ThisWorkbook.Sheet1.range("B2:B50") = "Stuff"
The macro will find workbook.name = "Stuff" and copy the used range in Workbooks("Stuff").Worksheets("ABC")
The master will paste this used range into Workbooks(2).Worksheets("Stuff")
Then move onto the cell B3 in ThisWorkBook.Sheet1.range("B2:B50")
Below is the macro as I have it. It doesn't work with the adjustments I've tried to make:
Option Explicit
Sub Update_Summary()
Dim wb As Workbook
Dim ws As Worksheet
Dim myPath As String, myFile As String, myExtension As String
Dim FldrPicker As FileDialog
Dim FilePicker As FileDialog
Dim rng As Range
Dim cell As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
MsgBox ("Please select Summary File")
myFile = Application.GetOpenFilename
Workbooks.Open (myFile), UpdateLinks:=0
Workbooks(1).Worksheets("Data").Range("D2").Value = ActiveWorkbook.Path
Workbooks(1).Worksheets("Data").Range("D3").Value = ActiveWorkbook.Name
rng = ThisWorkbook.Sheet1.Range("B2:B50")
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
myExtension = "*.xl*"
myFile = Dir(myPath & myExtension)
Do While myFile <> ""
Set wb = Workbooks.Open(Filename:=myPath & myFile)
For Each cell In rng
On Error Resume Next
If ExistBook(wb.Name) Then
If wb(ws.Name) = "Rollup" Then
wb.Sheets("ABC").UsedRange.Copy
Workbooks(2).Sheets(cell.Name).Range("A1").PasteSpecial xlValues
End If
End If
On Error GoTo 0
wb.Close savechanges:=False
Next cell
DoEvents
myFile = Dir
Loop
MsgBox "Task Complete!"
ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Function ExistBook(sWs$, Optional sWb As Workbook) As Boolean
If sWb Is Nothing Then Set sWb = ActiveWorkbook
On Error Resume Next
ExistBook = IsObject(sWb.Sheets(sWs))
End Function
Any ideas?
Thanks!!
Bookmarks