Cell Reference as file Path

Raaj Chauhan

New Member
Joined
Aug 1, 2015
Messages
9
Hi All,

Thanks to all. I need a small help. I have a code which is working fine. Whereas each and every time I need to change the reference (path) of the file in VBA Code. Can you please help me, instead of giving the path in VBA code can i keep it in Sheet1 (A4) cell.

Thanks in Advance


Code:
Sub UpdateDrugs()

Dim fpath As String
Dim owb As Workbook
Dim Master As Worksheet
Dim Slave As Worksheet 'the following declares both master and slave as worksheets

' I want to keep this reference in Sheet1 A4 cell
fpath = "D:\ART Work\Kamareddy_MLL Tool_08032018.xlsm" ' Path where your MLL tool file is

Set owb = Application.Workbooks.Open(fpath) 'opens the file path

Set Master = ThisWorkbook.Worksheets("Drug_Master") 'Declares this workbook and sheet as "master"
Set Slave = owb.Worksheets("Data") 'Declares the workbook and sheet you're copying to as "slave"

For j = 1 To 50000 'The master sheet range
    For i = 1 To 50000 'The slave sheet range
        If Trim(Master.Cells(j, 2).Value2) = vbNullString Then Exit For 'if the ART_No is blank it will exit and move on to the next row
        If Master.Cells(j, 2).Value = Slave.Cells(i, 23).Value Then '2 (Column B) in Drugs and 23 (Colum W) in MLL tool
            Slave.Cells(i, 26).Value = Master.Cells(j, 4).Value 'to update Due Date
            Slave.Cells(i, 27).Value = Master.Cells(j, 5).Value ' to update Next Due Date
            Slave.Cells(i, 32).Value = Master.Cells(j, 9).Value 'to update Regimen
            
        End If
    Next
    
Next

MsgBox ("Data Transfer Successful")

With owb
    .Save
    .Close
End With
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Replace:
Code:
fpath = "D:\ART Work\Kamareddy_MLL Tool_08032018.xlsm"
with
Code:
fpath = Range("A4").Value
where A4 contains:
Code:
D:\ART Work\Kamareddy_MLL Tool_08032018.xlsm
without the quotes.
 
Last edited:
Upvote 0
Replace:
Code:
fpath = "D:\ART Work\Kamareddy_MLL Tool_08032018.xlsm"
with
Code:
fpath = Range("A4").Value
where A4 contains:
Code:
D:\ART Work\Kamareddy_MLL Tool_08032018.xlsm
without the quotes.
Thanks mumps for quideing me. It's working fine. As per your suggestion I have added the below code
fpath = ThisWorkbook.Worksheets("Home").Range("A4")

Thanks once again:)
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,703
Members
452,994
Latest member
Janick

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