Choosing Text File to Import

jimayers

Board Regular
Joined
Nov 14, 2010
Messages
99
Hello - I am having trouble with the following code. I have read through many posts but cannot seem to get it.
Code:
Sub ImportTxt_File()
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
    With dlgOpen
        .AllowMultiSelect = True
        ''Start in
        .InitialFileName = "Z:\docs\"
        .Show
    End With
    For Each fname In dlgOpen.SelectedItems
        Set myTextFile = Workbooks.Open(fname)
        myTextFile.Sheets(1).Range("A1").CurrentRegion.copy_
            ThisWorkbook.Sheets(1).Range ("B7")
        myTextFile.Close (False)
    Next
End Sub

I get the error "variable not defined."

Thanks for any help - JA
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello - I am having trouble with the following code. I have read through many posts but cannot seem to get it.
Code:
Sub ImportTxt_File()
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
    With dlgOpen
        .AllowMultiSelect = True
        ''Start in
        .InitialFileName = "Z:\docs\"
        .Show
    End With
    For Each [B][COLOR="#FF0000"]fname[/COLOR][/B] In dlgOpen.SelectedItems
        Set [B][COLOR="#FF0000"]myTextFile[/COLOR][/B] = Workbooks.Open(fname)
        myTextFile.Sheets(1).Range("A1").CurrentRegion.copy_
            ThisWorkbook.Sheets(1).Range ("B7")
        myTextFile.Close (False)
    Next
End Sub

I get the error "variable not defined."
It sounds like you have Option Explicit located at the top of your code module (that is a good thing) but that you have not Dim'med the variables I highlighted in red above (although it is possible that myTextFile is not a variable if you renamed the Code Name for the worksheet as myTextFile... unlikely, but possible).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

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