Mikeymike_W
Board Regular
- Joined
- Feb 25, 2016
- Messages
- 171
Hi all,
I was given a great piece of code the other day to help pull a range of data from several worksheets in a fixed location. This works great but now my boss has asked me to do something similar for another spreadsheet but this time the the source where I want to pull data from is changeable.
Is there a way to alter the code below so that it opens a file browser for the user to select the file to pull the data from?
If possible I don't actually want to open this file just for excel to know the path where the data needs to come from.
Many thanks,
Mike
I was given a great piece of code the other day to help pull a range of data from several worksheets in a fixed location. This works great but now my boss has asked me to do something similar for another spreadsheet but this time the the source where I want to pull data from is changeable.
Is there a way to alter the code below so that it opens a file browser for the user to select the file to pull the data from?
If possible I don't actually want to open this file just for excel to know the path where the data needs to come from.
Code:
Sub ConsolidateWbks()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim Pth As String
Dim MstSht As Worksheet
Dim fname As String
Dim Rng As Range
Dim vFile As Variant
Pth = "C:\Users\MWa\Desktop\VBA Test\"
Set MstSht = ThisWorkbook.Sheets("Test1")
fname = Dir(Pth & "*xls*")
Do While Len(fname) > 0
Workbooks.Open (Pth & fname)
With Workbooks(fname)
.Sheets("Global FACT").Unprotect "watters"
Set Rng = MstSht.Range("D" & Rows.Count).End(xlUp).Offset(1)
Rng.Resize(, 35).Value = Application.Transpose(.Sheets("Global FACT").Range("B2:B36").Value)
Rng.Offset(, 35).Value = .Sheets("Global FACT").Range("C36").Value
Rng.Offset(, 36).Value = .Sheets("Global FACT").Range("B37").Value
Rng.Offset(, 37).Value = .Sheets("Global FACT").Range("B38").Value
Rng.Offset(, 38).Value = .Sheets("Global FACT").Range("B39").Value
Rng.Offset(, 39).Value = .Sheets("Global FACT").Range("B40").Value
Rng.Offset(, -2).Value = .Sheets("CRF").Range("C12").Value
Rng.Offset(, -3).Value = .Sheets("CRF").Range("C16").Value
Rng.Offset(, -1).Value = .Sheets("CRF").Range("G4").Value
.Sheets("Global FACT").Protect "watters"
Application.DisplayAlerts = False
.Close , False
Application.DisplayAlerts = True
End With
fname = Dir
Loop
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Many thanks,
Mike