Another Type Mismatch reading text file

WhoCanDo

Board Regular
Joined
Dec 18, 2007
Messages
193
Hi,

I want to read the contents of a file if it exists, otherwise, create that file.

Using the following code, the code will look for the file Nudge.txt in your Documents folder. If it doesn't exist, it will create the file with the lines 0, 0 & 0

However, because I want the variables as Double to be used in other procedures, I added the Public declarations.

This code works without the Public declarations, but not with it.

The code gets stuck on "Line Input #1, yAdjust", which is peculiar because it's Ok with vAdjust & xAdjust. (Compile error: Type mismatch)

The code resides in Forms, but to test it, I am not calling it yet.

Any ideas please ??

VBA Code:
Public vAdjust, xAdjust, yAdjust As Double

Sub GetDefaults()
'
    Pathname = Left(Application.StartupPath, InStr(10, Application.StartupPath, "\") - 1)
    On Error Resume Next
    Open Pathname & "\Documents\Nudge.txt" For Input As #1
    If (InStr(1, "52,53,75,76", Err.Number) > 0) Then
        On Error GoTo 0
        Close #1
        Open Pathname & "\Documents\Nudge.txt" For Output As #1
            Write #1, 0 ' Vertical point of origin nudge
            Write #1, 0 ' Horizontal nudge
            Write #1, 0 ' Vertical nudge
        Close #1
    Else
        Line Input #1, vAdjust
        Line Input #1, xAdjust
        Line Input #1, yAdjust
        Close #1
    End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Because you are using Line Input. The type mismatch is because (as per Microsoft documentation) Line Input "Reads a single line from an open sequential file and assigns it to a String variable."

To fix it you need to read the values as string and then convert to double with CDbl, like so:
VBA Code:
Public vAdjust As Double, xAdjust As Double, yAdjust As Double

Sub GetDefaults()
    Dim strvAdjust As String, strxAdjust As String, stryAdjust As String
'
    Pathname = Left(Application.StartupPath, InStr(10, Application.StartupPath, "\") - 1)
    On Error Resume Next
    Open Pathname & "\Documents\Nudge.txt" For Input As #1
    If (InStr(1, "52,53,75,76", Err.Number) > 0) Then
        On Error GoTo 0
        Close #1
        Open Pathname & "\Documents\Nudge.txt" For Output As #1
            Write #1, 0#  ' Vertical point of origin nudge
            Write #1, 0#  ' Horizontal nudge
            Write #1, 0#  ' Vertical nudge
        Close #1
    Else
        Line Input #1, strvAdjust
        Line Input #1, strxAdjust
        Line Input #1, stryAdjust
        vAdjust = CDbl(strvAdjust)
        xAdjust = CDbl(strxAdjust)
        yAdjust = CDbl(stryAdjust)
        Close #1
    End If
End Sub

As an aside, the line
VBA Code:
Public vAdjust, xAdjust, yAdjust As Double
is not doing what you think it's doing.

As with a Dim statement, you still have to declare the type for each variable. In your case, yAdjust is the only one declared as Double. The other two, because they don't have a qualifier, are set as Variant (which also explains why yAdjust is the only one that failed). You need
VBA Code:
Public vAdjust as Double, xAdjust as Double, yAdjust As Double
 
Upvote 0
Solution
Excellent myall_blues.

Your explanation all makes sense now.

The Public declaration I used was from another site I came across. I use this declaration style with Pascal, but it's obvious now that you explained it, that it's not the same for Excel.

I was guessing it was the declaration that stopped me, but I wasn't sure how to fix it.

Nice work.
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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
Back
Top