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
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,712
Office Version
  1. 2010
Platform
  1. Windows
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:

Watch MrExcel Video

Forum statistics

Threads
1,123,295
Messages
5,600,785
Members
414,405
Latest member
Zaurb

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