Dynamic File Reference in VBA not working

Utterly Bamboozled

New Member
Joined
Jul 7, 2010
Messages
39
Hi all,

Can't work out why the file reference below (myFile = "C:\Users\WP\Documents\" & Range("A1").Value & ".txt") results in Excel constantly freezing.

The file exists and the myFile string is correct...

Any ideas?

Code:
Function BarStringInfo() As String
Dim myFile As String
Dim text As String
Dim textline As String
Dim posLat As Integer
Dim posLong As Integer


[B]myFile = "C:\Users\WP\Documents\" & Range("A1").Value & ".txt"[/B]
Open myFile For Input As #1


Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
Loop
Close #1
BarStringInfo = text
End Function

Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Arafat

Board Regular
Joined
May 3, 2011
Messages
61
Hi,The code is working fine. please check the file names or check the code from where you are calling this function.
 

Utterly Bamboozled

New Member
Joined
Jul 7, 2010
Messages
39
Hi,The code is working fine. please check the file names or check the code from where you are calling this function.

Thanks Arafat,

I restarted the project in a new workbook to try to isolate the issue. Unfortunately it seems through adding additional complexity to the workbook it becomes volatile. Could be the combination of user-defined functions I guess... For reference I'm also using the below function to split a long string into its component parts:

Code:
Public Function SplitString(ByVal Text1 As String, WordPos As Long) As String
    ' EXCEL VBA SplitString FUNCTION
   
    Dim CommaPos As Long
    Dim LeftBorder, RightBorder As Long
    Dim WordCounter As Long
    Dim tempstring As String
    Dim TestString As String
    Dim pos As Long
    
    pos = WordPos
    CommaPos = 0
    WordCounter = pos
    
    'LeftBorder
    If InStr(1, Text1, ",") <> 0 Then
        While WordCounter - 1 <> 0
            CommaPos = InStr(CommaPos + 1, Text1, ",")
            pos = pos - 1
            WordCounter = WordCounter - 1
        Wend
        LeftBorder = CommaPos + 1
        If InStr(LeftBorder + 1, Text1, ",") <> 0 Then
            RightBorder = InStr(LeftBorder + 1, Text1, ",")
        Else
            RightBorder = 9999
        End If
        
    Else
        LeftBorder = 1
        RightBorder = 9999
    End If
    
    SplitString = Mid(Text1, LeftBorder, (RightBorder - LeftBorder))
  
End Function

Using these two functions seems to be fine, but then when I add some Excel-defined functions the workbook becomes unstable. The Excel-functions in question are LEN() and SUBSTITUTE(). Specifically:

Code:
=SUM(LEN($A$3)-LEN(SUBSTITUTE($A$3,$F$7,"")))/LEN($F$7)

Any suggestions would be great.
Thanks
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,254
Messages
5,769,055
Members
425,513
Latest member
9375021758

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