CELL("filename") doesn't work on shortcut workbook link?

K

Kevin

Hi,
I got a shortcut link of master workbook and I"m trying to find the location
of shortcut linked file.

E.G.
1) C:/master file.xls
2) C:/MyFolder/shortcut to master file.xls

Now when I add this formula to shortcut file =Cell("filename") it shows
C:/master file.xls but I'm interested in C:/MyFolder/shortcut to master
file.xls .

Is it possible?

Thanks
Kevin
 
L

Luke M

No. The shortcut in My Folder is simply that, a shortcut to the actual file
which resides on the C drive. Nothing is actually being opened from the
MyFolder.
 
K

Kevin

So is there any workaround to solve this problem?
What I'm trying to do is:

1) I'm trying to read the contents of a folder and place them in a workbook.
The vba code I use works fine but it only reads the filenames of native
folder, where the master file.xls is residing.

2) So generally I copy (not shortcut) this master file.xls to the required
directory and open from there to make it working.

Problem is this master file is quite big and filling up h/disk for no
reason. I thought must have smarter way to overcome this problem.

Any pointer will be useful.

Thx
Kevin
 
R

Roger Govier

Hi Kevin

Why not just change directory within your code with the ChDir command

ChDir "C:\MyWork\Accounts"

--
Regards
Roger Govier

Kevin said:
So is there any workaround to solve this problem?
What I'm trying to do is:

1) I'm trying to read the contents of a folder and place them in a
workbook.
The vba code I use works fine but it only reads the filenames of native
folder, where the master file.xls is residing.

2) So generally I copy (not shortcut) this master file.xls to the required
directory and open from there to make it working.

Problem is this master file is quite big and filling up h/disk for no
reason. I thought must have smarter way to overcome this problem.

Any pointer will be useful.

Thx
Kevin




__________ Information from ESET Smart Security, version of virus
signature database 4826 (20100202) __________

The message was checked by ESET Smart Security.

http://www.eset.com

__________ Information from ESET Smart Security, version of virus signature database 4826 (20100202) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
K

Kevin

Roger,
Actually directory name is changing all the time.
e.g.
ChDir "C:\MyWork\Customer1
ChDir "C:\MyWork\Customer2 .....etc.

So whenever I need to generate new Quote, I drag my master file to new
customer's folder and continue.

Cheers,
Kevin
 
R

Roger Govier

Hi Kevin

So why are you moving the Master file?
If the code within it is changing folder before listing the files, it does
not matter at all where the master file exists.

--
Regards
Roger Govier

Kevin said:
Roger,
Actually directory name is changing all the time.
e.g.
ChDir "C:\MyWork\Customer1
ChDir "C:\MyWork\Customer2 .....etc.

So whenever I need to generate new Quote, I drag my master file to new
customer's folder and continue.

Cheers,
Kevin




__________ Information from ESET Smart Security, version of virus
signature database 4828 (20100202) __________

The message was checked by ESET Smart Security.

http://www.eset.com

__________ Information from ESET Smart Security, version of virus signature database 4828 (20100202) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
K

Kevin

Roger Govier said:
Hi Kevin

So why are you moving the Master file?
If the code within it is changing folder before listing the files, it does
not matter at all where the master file exists.

Hi Roger,
I've copied my code below if that helps. I'm not sure how it will read
contents of native folder if hardcoded ChDir ?
Cheers,
Kevin.
-------------

Sub Auto_Open()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This procedure will list, in alphabetical order, all the txt
' files that are in the same folder as the Active Workbook.
' If the active workbook has never been saved (Path = vbNullString)
' the user will be prompted to save the workbook before
' listing the files. The workbook should be saved to the same
' folder in which the text files to list reside.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim DestRng As Range ' starting point to list files
Dim FolderName As String ' the folder containing the text files
Dim FileName As Variant ' will contain the name of each text file
Dim SaveDir As String ' we'll save the default drive and folder
' to restore at the end of the procedure
Dim FirstCell As Range ' saves the first cell reference
Dim LastCell As Range ' the last cell in the file list
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Change the destination range variable DestRng to the
' cell in which you want the file list to start.
Set DestRng = ActiveWorkbook.Worksheets("MAIN SETUP PAGE").Range("AB7")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' save the first cell to use in sorting later
Set FirstCell = DestRng
' save the current drive and folder to restore at end of procedure.
SaveDir = CurDir
' if ActiveWorkbook.Path = vbNullString, the Active Workbook
' has never been saved. Prompt the user to save the workbook.
If ActiveWorkbook.Path = vbNullString Then
FileName = Application.GetSaveAsFilename( _
filefilter:="Excel Files (*.xls),*.xls", _
Title:="Save the workbook to the same folder" & _
"that contains the text files to list.")
If FileName = False Then
' user clicked cancel and didn't select a SaveAs filename.
' get out now.
Exit Sub
Else
' user selected a SaveAs Filename. Turn off DisplayAlerts
' so the user won't get a "Do you want to replace..." message.
Application.DisplayAlerts = False
' save the workbook
ActiveWorkbook.SaveAs FileName:=FileName
' turn alerts back on
Application.DisplayAlerts = True
End If

End If
' get the folder name of the active workbook
FolderName = ActiveWorkbook.Path
' change the default drive and directory to that named by FolderName
ChDrive FolderName
ChDir FolderName
' get the first text file name
FileName = Dir("*.*")
' loop until FileName = vbNullString. The Dir() command, with nothing
' within the parentheses) will return the next text file name or
' vbNullString when there are no more text files. You cannot rely
' on Dir() to return the file names in any particular order. We'll
' sort the filenames at the end of the procedure.
Do Until FileName = vbNullString
' put the full filename in the destination range
DestRng.Value = FolderName & Application.PathSeparator & FileName
' move the destination range down one row
Set DestRng = DestRng(2, 1)
' get the next text file name from the Dir() function. If there
' are no more text files, Dir() will return vbNullString
FileName = Dir()

Loop
' restore the saved default drive and path info
ChDrive SaveDir
ChDir SaveDir
' DestRng will refer to the blank cell after the end of the file list.
' Set LastCell one row above DestRng, so LastCell points to the end
' of the file list.
Set LastCell = DestRng(0, 1)
' now, sort the filenames, from FirstCell to DestRng. Use DestRng.Parent
' to ensure we're pointing to the proper worksheet.
DestRng.Parent.Range(FirstCell, LastCell).Sort key1:=FirstCell, _
order1:=xlAscending, header:=xlNo

End Sub
 
R

Roger Govier

Hi Kevin

The code as it stands is et to only read the contents of the folder where
the workbook with the code is stored.
If you added the code created by Ken Puls as a function
http://www.vbaexpress.com/kb/getarticle.php?kb_id=284
BrowseforFolder, then you could amend your code to let the user choose the
folder for listing

Change

' get the folder name of the active workbook
FolderName = ActiveWorkbook.Path

to

' choose the folder to be used for listing the files
FolderName = BrowseForFolder

The code for BrowseforFolder is

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
' written by Ken Puls
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level

Dim ShellApp As Object

'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.Self.Path
On Error GoTo 0

'Destroy the Shell Application
Set ShellApp = Nothing

'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False

End Function


You might also want to consider clearing your Destrange before you start, as
otherwise any files already listed will not be overwritten if the list from
the new folder is less than that last created.
--
Regards
Roger Govier



__________ Information from ESET Smart Security, version of virus signature database 4832 (20100203) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
K

Kevin

Hi Roger,
Thanks a lot, it works like a charm.
There are couple of very basic issues, could you pls resolve those as well.
Sorry I'm only good in copy/pasting.

1) How can I change default folder so that popup window opens in following
folder E:/2010/customers/

2) I got a following macro which I use to save the file. But now It saves in
the root folder instead of saving in specific folder which we set above with
BrowseForFolder function.

Sub SaveAsCell()
Dim strName As String

On Error GoTo InvalidName
strName = Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

Exit Sub
InvalidName: MsgBox "The text: " & strName & _
" is not a valid file name.", vbCritical, " "
End Sub

Thanks,
Kevin
 
R

Roger Govier

Hi Kevin

FolderName = BrowseForFolder("E:\2010\Customers\")

(Note all paths have a backslash \ not a forward slash / as you have been
typing)

for the Saving, I think you will need to select folder again by inserting
the line of code in your sub

Sub SaveAsCell()
Dim strName As String, FolderName as string
FolderName = BrowseForFolder("E:\2010\Customers\")

On Error GoTo InvalidName
strName = FolderName & Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

Exit Sub
InvalidName: MsgBox "The text: " & strName & _
" is not a valid file name.", vbCritical, " "
End Sub

--
Regards
Roger Govier

Kevin said:
Hi Roger,
Thanks a lot, it works like a charm.
There are couple of very basic issues, could you pls resolve those as
well.
Sorry I'm only good in copy/pasting.

1) How can I change default folder so that popup window opens in following
folder E:/2010/customers/

2) I got a following macro which I use to save the file. But now It saves
in
the root folder instead of saving in specific folder which we set above
with
BrowseForFolder function.

Sub SaveAsCell()
Dim strName As String

On Error GoTo InvalidName
strName = Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

Exit Sub
InvalidName: MsgBox "The text: " & strName & _
" is not a valid file name.", vbCritical, " "
End Sub

Thanks,
Kevin



__________ Information from ESET Smart Security, version of virus
signature database 4836 (20100204) __________

The message was checked by ESET Smart Security.

http://www.eset.com

__________ Information from ESET Smart Security, version of virus signature database 4836 (20100204) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
K

Kevin

Hi Roger,
FolderName = BrowseForFolder("E:\2010\Customers\") works now, I's using
forward slashes. Is this popup window will come up everytime when I open new
renamed workbook?

Secondly that SaveAsCell macro still doesn't work

FolderName = BrowseForFolder("E:\2010\Customers\")
it just opens popup window again.

On Error GoTo InvalidName
strName = FolderName & Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

FYI in Cell "B1" I got a formula which reads the drive letter + concatenate
text
Formula in Cell B1
=LEFT(CELL("filename",A1),FIND("[",CELL("filename",A1),1)-1) + some other
text which I use to save the workbook.

Is this stuffing up something?

As per my understanding:
Sub Auto_Open() functions knows the Foldername where we are now that's why I
can read the files in that specific folder
But
Sub SaveAsCell() macro doesn't know the current directory where we are. So
need to pick some code from Sub Auto_Open() ??

Thanks once again Roger,
Cheers,
Kevin
 
R

Roger Govier

Hi Kevin

The Browse for folder is giving you the opportunity to choose where you want
to save the file.
If your folder name in cell B1 does not begin with a backslash, then change
the line of code to be

strName = FolderName & "\" & Sheet1.Range("B1")

--
Regards
Roger Govier

Kevin said:
Hi Roger,
FolderName = BrowseForFolder("E:\2010\Customers\") works now, I's using
forward slashes. Is this popup window will come up everytime when I open
new
renamed workbook?

Secondly that SaveAsCell macro still doesn't work

FolderName = BrowseForFolder("E:\2010\Customers\")
it just opens popup window again.

On Error GoTo InvalidName
strName = FolderName & Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

FYI in Cell "B1" I got a formula which reads the drive letter +
concatenate
text
Formula in Cell B1
=LEFT(CELL("filename",A1),FIND("[",CELL("filename",A1),1)-1) + some other
text which I use to save the workbook.

Is this stuffing up something?

As per my understanding:
Sub Auto_Open() functions knows the Foldername where we are now that's why
I
can read the files in that specific folder
But
Sub SaveAsCell() macro doesn't know the current directory where we are.
So
need to pick some code from Sub Auto_Open() ??

Thanks once again Roger,
Cheers,
Kevin

Roger Govier said:
Hi Kevin

FolderName = BrowseForFolder("E:\2010\Customers\")

(Note all paths have a backslash \ not a forward slash / as you have been
typing)

for the Saving, I think you will need to select folder again by inserting
the line of code in your sub

Sub SaveAsCell()
Dim strName As String, FolderName as string
FolderName = BrowseForFolder("E:\2010\Customers\")

On Error GoTo InvalidName
strName = FolderName & Sheet1.Range("B1")
ActiveWorkbook.SaveAs strName

Exit Sub
InvalidName: MsgBox "The text: " & strName & _
" is not a valid file name.", vbCritical, " "
End Sub

__________ Information from ESET Smart Security, version of virus
signature database 4839 (20100205) __________

The message was checked by ESET Smart Security.

http://www.eset.com

__________ Information from ESET Smart Security, version of virus signature database 4839 (20100205) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
K

Kevin

Hi Roger,
I had to tweak a bit but eventually it's working. Thank you very much for
your help, I got more than what I asked for. Glad to sign off ...
Cheers,
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top