VBA to use cell reference and open another workbook

jas1980

New Member
Joined
Oct 14, 2016
Messages
25
I have the following code in a workbook and it works perfectly, it opens the corresponding csv file, updates information and closes the csv, I am trying to change the address from being written into the code to being retrieved from a cell reference.

Any advice is much appreciated.

Sub GetFile()

Workbooks.Open "C:\Users\jamie\AppData\Roaming\MetaQuotes\Terminal\06980EAAF7FCC460D7BD131105CC8618\MQL4\Files\BSA_Transactions\BSA_Transactions.csv", UpdateLinks:=1
ActiveWorkbook.Close SaveChanges:=True

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

jsb1921

Board Regular
Joined
Aug 14, 2020
Messages
70
Office Version
  1. 2007
Platform
  1. Windows
  2. Web
In A1 put the value "C:\Users\jamie\AppData\Roaming\MetaQuotes\Terminal\06980EAAF7FCC460D7BD131105CC8618\MQL4\Files\BSA_Transactions\"
VBA Code:
dim str1 as string
str1=trim(ltrim(cells(1,"A").value))      '1 is rownumber A is the column, read address from A1
Workbooks.Open  str1 & "BSA_Transactions.csv", UpdateLinks:=1
if it serves the need mark it as a solution.
 
Solution

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
What about
VBA Code:
  Workbooks.Open Filename:=Range("A1")
 

jas1980

New Member
Joined
Oct 14, 2016
Messages
25
In A1 put the value "C:\Users\jamie\AppData\Roaming\MetaQuotes\Terminal\06980EAAF7FCC460D7BD131105CC8618\MQL4\Files\BSA_Transactions\"
VBA Code:
dim str1 as string
str1=trim(ltrim(cells(1,"A").value))      '1 is rownumber A is the column, read address from A1
Workbooks.Open  str1 & "BSA_Transactions.csv", UpdateLinks:=1
if it serves the need mark it as a solution.
Thank you for this it works perfectly, I just removed "BSA_Transactions.csv" so it was all in the same cell

Sub GetFile()

Dim str1 As String
str1 = Trim(LTrim(Cells(2, "P").Value)) '2 is rownumber P is the column, read address from O3

Workbooks.Open str1, UpdateLinks:=1
ActiveWorkbook.Close SaveChanges:=True

End Sub
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
Hi
in my code Range("A1") should hold the path an the file name
"C:\Users\jamie\AppData\Roaming\MetaQuotes\Terminal\06980EAAF7FCC460D7BD131105CC8618\MQL4\Files\BSA_Transactions\BSA_Transactions.csv"
 

Watch MrExcel Video

Forum statistics

Threads
1,130,035
Messages
5,639,668
Members
417,104
Latest member
Nelsini

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
Top