Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 78
Default Insert filename via a browse to cell

I would like to click in column A. Let's say cell A3 and browse to a file on
the network and then click on the file and have the filename automatically
inserted in the cell. Maybe even the path - but it just has to be the name.
Then if I move to cell A4, would like the same functionality. Or if you have
a different method. We have a process that creates files, some have errors
and I want to document the file and it's error -- however the filename is
huge.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,069
Default Insert filename via a browse to cell

Paste the following code in the code module for the sheet where you want his
to work (double-clicking any cell in column A will display a File Open
dialog. The name of the file you select will be entered into the cell).

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
Dim Fyle As String
If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
Fyle$ = Application.GetOpenFilename("All Files (*.*), *.*")
'If no file was selected (Cancel clicked), stop the macro.
If Fyle$ = "False" Then Exit Sub
'To include the path, just assign Fyle$ to Target.Value
Target.Value = InStrR(Fyle$, "\")
End Sub

Private Function InStrR(CheckThis As String, ForThis As String) As String
'Searches a string for a specified character from the END to the BEGINNING.
'If found, returns the rest of the string, starting one character to the
right
'of ForThis.
'Declare local variables.
Dim xx As Integer, TmpStr1 As String
'Make sure we are only searching for a single character.
If Len(ForThis$) < 1 Then
MsgBox "InStrR only searches for a single character", _
vbExclamation, "ERROR"
InStrR$ = vbNullString
End If
'Walk backwards through CheckThis$ one character at a time.
For xx% = Len(CheckThis$) To 1 Step -1
Select Case Mid(CheckThis$, xx%, 1)
'If encounter ForThis character, stop & return TmpStr1$.
Case ForThis$
InStrR$ = Trim(TmpStr1$)
Exit Function
'If encounter any other character, add it to the FRONT of TmpStr1$ (reverses
'the order).
Case Else
TmpStr1$ = Mid(CheckThis$, xx%, 1) & TmpStr1$
End Select
Next xx%
End Function

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Hope this helps,

Hutch

"Rookie_User" wrote:

I would like to click in column A. Let's say cell A3 and browse to a file on
the network and then click on the file and have the filename automatically
inserted in the cell. Maybe even the path - but it just has to be the name.
Then if I move to cell A4, would like the same functionality. Or if you have
a different method. We have a process that creates files, some have errors
and I want to document the file and it's error -- however the filename is
huge.

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 78
Default Insert filename via a browse to cell

That worked PERFECTLY!!! thank you so much for your time and efforts.

"Tom Hutchins" wrote:

Paste the following code in the code module for the sheet where you want his
to work (double-clicking any cell in column A will display a File Open
dialog. The name of the file you select will be entered into the cell).

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
Dim Fyle As String
If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
Fyle$ = Application.GetOpenFilename("All Files (*.*), *.*")
'If no file was selected (Cancel clicked), stop the macro.
If Fyle$ = "False" Then Exit Sub
'To include the path, just assign Fyle$ to Target.Value
Target.Value = InStrR(Fyle$, "\")
End Sub

Private Function InStrR(CheckThis As String, ForThis As String) As String
'Searches a string for a specified character from the END to the BEGINNING.
'If found, returns the rest of the string, starting one character to the
right
'of ForThis.
'Declare local variables.
Dim xx As Integer, TmpStr1 As String
'Make sure we are only searching for a single character.
If Len(ForThis$) < 1 Then
MsgBox "InStrR only searches for a single character", _
vbExclamation, "ERROR"
InStrR$ = vbNullString
End If
'Walk backwards through CheckThis$ one character at a time.
For xx% = Len(CheckThis$) To 1 Step -1
Select Case Mid(CheckThis$, xx%, 1)
'If encounter ForThis character, stop & return TmpStr1$.
Case ForThis$
InStrR$ = Trim(TmpStr1$)
Exit Function
'If encounter any other character, add it to the FRONT of TmpStr1$ (reverses
'the order).
Case Else
TmpStr1$ = Mid(CheckThis$, xx%, 1) & TmpStr1$
End Select
Next xx%
End Function

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Hope this helps,

Hutch

"Rookie_User" wrote:

I would like to click in column A. Let's say cell A3 and browse to a file on
the network and then click on the file and have the filename automatically
inserted in the cell. Maybe even the path - but it just has to be the name.
Then if I move to cell A4, would like the same functionality. Or if you have
a different method. We have a process that creates files, some have errors
and I want to document the file and it's error -- however the filename is
huge.

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
Browse For a File or Path and enter in a cell D. Jones Excel Discussion (Misc queries) 5 November 9th 07 04:07 PM
How do I insert the filename of the worksheet into a cell? joeker Excel Discussion (Misc queries) 2 April 10th 06 01:33 PM
How do I insert the filename to a specific cell of the worksheet? Rozoga Excel Discussion (Misc queries) 1 February 10th 06 05:43 PM
How do I insert a partial filename? Admin Excel Discussion (Misc queries) 1 December 1st 05 01:49 AM
Insert value of a cell as a filename Ralph Howarth Excel Worksheet Functions 0 January 18th 05 12:03 AM


All times are GMT +1. The time now is 07:40 PM.

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"