VBA Code to path based on a cells contents.

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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
path equates to the value "T:\Curtis\Pile Data" so
Code:
Range("L2") = path
should put that value in the cell. But why re-invent the wheel, just use the constant 'path' where you need the value. I suppose it could be needed for visual purposes. Or maybe I don't understand the issue.
 
Upvote 0
Glad one of us could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top