Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

internal pointer to external file reference

Status
Not open for further replies.

hotdog2go

Mechanical
Jul 8, 2002
4
I need help trying to find a way around this road block.
I want to create a cell, lets say (A1) with a year value, 2000, 2001, 2002 etc.
Later in the file I want to perform a test for a condition, and based on the results, retreive data from an external file of a given name and a tab name of the same value as (A1). There will be many additional tests similiar to this test throughout the sheet.

This file will be a template that we be used for many years, and I do not want to have to go through and change the file name or tab name at every single test cell on the sheet every time the year changes.

Maybe my whole approach here is wrong, if you have some better ideas I would like to hear them.

Thanks
 
Replies continue below

Recommended for you

It should be pretty straightforward, since VBA has lots of text manipulation functions. You would take the value of A1, convert it to text and concatenate with the appropriate directory path and file extension, etc.

TTFN
 
hotdog2go:

You could write a macro (in "ThisWorkbook") to enter the year that the template is opened:

Private Sub Workbook_Open()
OpenDate = Date
Range("A1").Value = Year(OpenDate)
End Sub

Reading the data from another file is easily accomplished with a custom function if you know where in the sheet you want to read it from:

Function GetData(MyYear As String, Cell_Ref As String)
GetData = Workbooks(MyYear).Worksheets(MyYear). _
Range(Cell_Ref).Value
End Function

Note that the sheet you are reading from must be open for this function to work. If it is not open, you can open it automatically when the template is opened if you want (you can even open it hidden if you want).

I'm not sure how you reference the cells in the "Read" sheet, but the function I used above relies on a reference cell "Cell_Ref" that has something like "D1" typed into it. There are other ways to do the same thing, however.

Hope this helps!

jproj
 
IRSTUFF
That is exactly how I approached it but it would not work for me. My test cell looked something like this:
a1=2000
a2=concatenate("g:\somedir\[somefile]",a1,"deptrate")

a2 then looks like this
g:\somedir\[somefile]2000deptrate
OK, good, thats is what I want.

But I want my test cell looked like this:
=IF(c1=1,'g:\somedir\[somefile]2000deptrate'!g17)

How do I get the text string created by a2 in-between the single quotes in the line above?

Jproj, I'm not to familiar with macro's I'll give it a try.

Thanks, both, for your input.

 
If I'm reading that correct, should be another concatenation:

=IF(c1=1,concatenate("'",concatenate("g:\somedir\[somefile]",a1,"deptrate")
,"'!g17")


or something to that effect

TTFN
 
IRstuff

That just dumps the results of the concatenation into the cell, it does not actually go out into the file and pull in the requested cell.

Good try,
Thanks

 
OK, I see what you're doing. The macro looks something like this:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/8/2002 by Eden Y.C. Mei
'
Range("A1").Select
Selection.Copy
Range("A2").Select
Range("A2").Value = "=[test" + Range("A1").Value + ".xls]sheet1!b3"


End Sub

and A1 in this case contains '2002, since that's the cheat for making sure that it's text and not a number. Sorry, I couldn't remember how to get the conversion.

TTFN
 
Oh, ignore the first 3 lines, they're a remnant of the macro recording.

TTFN
 
hotdog2go:

To open the VBA editor, press Alt + F11 while in Excel. Next click on "Insert" and choose "Module". This will open a new module for your macro. Next, just paste the macro below into your module.

Sub GetData()
If Range("C1").Value = 1 Then
Range("A2").Value = "=" & Range("B2").Value
End If
End Sub

This will place a macro named "GetData" into you module. If you close the visual basic editor and go back to excel you can run the macro by pressing Alt + F8 (or by clicking on "Tools" and choosing "Macro" and "Macros...") The macro dialog box will pop up and you should be able to find the macro you just copied (GetData). Click on it and then click on "Run". The macro first checks for cell C1 = 1. If it is = 1 then the macro concantenates the equals sign with the contents of cell B2 and place the equation in cell A2. Cell A2 will then read the value of cell G17 of your read file. If Cell C1 is not = to 1, the macro does nothing.

You may also find it useful to use the "&" sign for concantenation instead of the equation:

="My" & "File" is the same as =CONCATENATE("My","File")

Hope this helps

jproj





 
Thanks, Both of you.
I'll give these things a try today.

RF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor