Pelle Peloton
New Member
- Joined
- Nov 17, 2021
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Hi! I'm beginner with VBA and struggling to get dynamic "data import" to work properly in a way that the path is taken from a cell value (instead of written to VBA code). I have one master Excel (sheet "WP_Data") to which I want to import data (one cell content) from tens of source-Excels (in each of them I have sheet "Workpaper_main" and a cell that is named as "WP_balance").
The below macro works, but the issue is that I have the "C:\Users\UsernameABC\XXX\2021-Jan\" written in the code. If some other user wants to import data with this macro --> "UsernameABC" in the path will change. Or when I want to search eg. 2021-Feb data -> I need to update the path as well. I have tried a few different ways to get the path taken from a cell in my Master-Excel (sheet "Parameters", cell "D3"), but haven't succeeded and I understood it might be related to Const - when trying to get strPath to be Range("D3").value. In the Excel I have a few parameters the user can update and that gives the proper path to cell D3.
Any advise highly appreciated!
The below macro works, but the issue is that I have the "C:\Users\UsernameABC\XXX\2021-Jan\" written in the code. If some other user wants to import data with this macro --> "UsernameABC" in the path will change. Or when I want to search eg. 2021-Feb data -> I need to update the path as well. I have tried a few different ways to get the path taken from a cell in my Master-Excel (sheet "Parameters", cell "D3"), but haven't succeeded and I understood it might be related to Const - when trying to get strPath to be Range("D3").value. In the Excel I have a few parameters the user can update and that gives the proper path to cell D3.
Any advise highly appreciated!
VBA Code:
Sub import_data()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = thisWorkbook
Dim LastRow As Long
'empty old data on "WP_Data" sheet
wkbDest.Sheets("WP_Data").Activate
Columns("A:B").Select
Selection.ClearContents
Range("A1").Select
'Search the data from all Excels
Const strPath As String = "C:\Users\UsernameABC\XXX\2021-Jan\"
ChDir strPath
strExtension = Dir("*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
.Sheets("Workpaper_main").Range("WP_balance").Copy
'copy the figures from workpapers and paste to this Master-Excel
wkbDest.Sheets("WP_Data").Cells(wkbDest.Sheets("WP_Data").Rows.Count, "B").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
'copy the workpaper names from workpapers and paste to this Master-Excel
wkbDest.Sheets("WP_Data").Activate
fRow = wkbDest.Sheets("WP_Data").Range("B" & Cells(Rows.Count, "B").End(xlUp).Row).Offset(0, 0).Row
lrow = wkbDest.Sheets("WP_Data").Range("B" & Cells(Rows.Count, "B").End(xlUp).Row).Row
wkbDest.Sheets("WP_Data").Range("A" & fRow) = wkbSource.Name
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: