Curtisyoung78
New Member
- Joined
- Jun 19, 2017
- Messages
- 25
I have a VBA Code that retrieves info from a path that is in the VBA coding itself. I need to be able to let other users change the path regularly with having to know how VBA works. I would like it to be able to read the path from a cell where the user can paste the path (cell L2) and not have to have the developer tab and chance altering the VBA negatively. Below is the code i have, as you can see it has the path in it. Ive tried a few things but my limited VBA knowledge has come up short. Please help. Thank you in advance.
Sub MergeFiles_v1()
Dim wkb As Workbook
Dim LR As Long
Dim LC As Long
Dim arr() As Variant
Dim filename As String
Const path As String = "T:\Curtis\Pile Data" ' would like to paste to cell L2 and have it read the contents as the path
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
filename = Dir(path & "\*.xls", vbNormal)
If Len(filename) = 0 Then Exit Sub
Do Until filename = vbNullString
If Not filename = ThisWorkbook.Name Then
Set wkb = Workbooks.Open(path & filename)
With wkb
With .Sheets(1)
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
arr = .Cells(2, 1).Resize(LR - 1, LC).Value
End With
.Close False
End With
Set wkb = Nothing
With ActiveWorkbook.Sheets(1)
LR = .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Row
.Cells(LR + 1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
Erase arr
End With
End If
filename = Dir()
Loop
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Sub MergeFiles_v1()
Dim wkb As Workbook
Dim LR As Long
Dim LC As Long
Dim arr() As Variant
Dim filename As String
Const path As String = "T:\Curtis\Pile Data" ' would like to paste to cell L2 and have it read the contents as the path
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
filename = Dir(path & "\*.xls", vbNormal)
If Len(filename) = 0 Then Exit Sub
Do Until filename = vbNullString
If Not filename = ThisWorkbook.Name Then
Set wkb = Workbooks.Open(path & filename)
With wkb
With .Sheets(1)
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
arr = .Cells(2, 1).Resize(LR - 1, LC).Value
End With
.Close False
End With
Set wkb = Nothing
With ActiveWorkbook.Sheets(1)
LR = .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Row
.Cells(LR + 1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
Erase arr
End With
End If
filename = Dir()
Loop
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub