Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Use external file variable in formula

Hi all. I have the below code to open the dialogue box to prompt the user to open a data file (a .csv file with only one sheet - Sheet1). I think I have that file path read into a variable. Now I need to write a formula into the original file against the data in the newly opened data file. The data file path and name will change, so it needs to be variable based on the file the user selects. How can I incorporate the path of the data file into the formula in the master file? Thanks!

Sub Main()

Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Source As Variant

Set Master = ThisWorkbook

Filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"

FilterIndex = 3

Title = "Select the Data Source File"

ChDrive ("C")
ChDir ("C:\")

With Application
Source = .GetOpenFilename(Filter, FilterIndex, Title)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With

If Source = False Then
Exit Sub
End If

Workbooks.Open Source

Ticket.Activate

Set frng = Range("A1")
With frng
'--------------------------------------------------------------------------
'THIS IS THE PART I CANE SEEM TO FIGURE OUT
..Formula = "='" & Source & "Sheet1!B5"
'--------------------------------------------------------------------------
End With
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Use external file variable in formula


You are missing a single apostrophe and the workbook name must be enclosed in brackets.
'---
Dim strName As String
Source = Application.GetOpenFilename()
Workbooks.Open Source
strName = Dir(Source)
Ticket.Activate
Range("A1").Formula = "='[" & strName & "]Sheet1'!B5"
'---
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(List Files XL add-in: finds and lists files/folders with hyperlinks)







"Steve"
wrote in message
news:25810026.365.1331699035421.JavaMail.geo-discussion-forums@pbcru10...
Hi all. I have the below code to open the dialogue box to prompt the user to open a data file (a
..csv file with only one sheet - Sheet1). I think I have that file path read into a variable. Now I
need to write a formula into the original file against the data in the newly opened data file. The
data file path and name will change, so it needs to be variable based on the file the user selects.
How can I incorporate the path of the data file into the formula in the master file? Thanks!

Sub Main()
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Source As Variant

Set Master = ThisWorkbook
Filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
FilterIndex = 3
Title = "Select the Data Source File"
ChDrive ("C")
ChDir ("C:\")
With Application
Source = .GetOpenFilename(Filter, FilterIndex, Title)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
If Source = False Then
Exit Sub
End If
Workbooks.Open Source
Ticket.Activate
Set frng = Range("A1")
With frng
'--------------------------------------------------------------------------
'THIS IS THE PART I CAN'T SEEM TO FIGURE OUT
..Formula = "='" & Source & "Sheet1!B5"
'--------------------------------------------------------------------------
End With


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Use external file variable in formula

Awesome! Thanks Jim!

On Wednesday, March 14, 2012 9:24:04 AM UTC-6, Jim Cone wrote:
You are missing a single apostrophe and the workbook name must be enclosed in brackets.
'---
Dim strName As String
Source = Application.GetOpenFilename()
Workbooks.Open Source
strName = Dir(Source)
Ticket.Activate
Range("A1").Formula = "='[" & strName & "]Sheet1'!B5"
'---
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(List Files XL add-in: finds and lists files/folders with hyperlinks)







"Steve"
wrote in message
news:25810026.365.1331699035421.JavaMail.geo-discussion-forums@pbcru10...
Hi all. I have the below code to open the dialogue box to prompt the user to open a data file (a
.csv file with only one sheet - Sheet1). I think I have that file path read into a variable. Now I
need to write a formula into the original file against the data in the newly opened data file. The
data file path and name will change, so it needs to be variable based on the file the user selects.
How can I incorporate the path of the data file into the formula in the master file? Thanks!

Sub Main()
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Source As Variant

Set Master = ThisWorkbook
Filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
FilterIndex = 3
Title = "Select the Data Source File"
ChDrive ("C")
ChDir ("C:\")
With Application
Source = .GetOpenFilename(Filter, FilterIndex, Title)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
If Source = False Then
Exit Sub
End If
Workbooks.Open Source
Ticket.Activate
Set frng = Range("A1")
With frng
'--------------------------------------------------------------------------
'THIS IS THE PART I CAN'T SEEM TO FIGURE OUT
.Formula = "='" & Source & "Sheet1!B5"
'--------------------------------------------------------------------------
End With


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
External Links in Excel 2007 with variable source file PaHead Excel Worksheet Functions 5 March 12th 10 07:12 PM
variable substitution in a formula linking to external workbook my Excel Discussion (Misc queries) 2 August 29th 07 03:05 PM
Formula to test for existence of external file Al at Novelis Excel Worksheet Functions 2 August 29th 06 01:13 AM
Formula to add cells from external file Borg Excel Programming 1 August 22nd 06 08:31 PM
Linking to an external referance with a variable file name? Kelly[_7_] Excel Programming 1 February 11th 04 10:56 PM


All times are GMT +1. The time now is 06:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"