+ Reply to Thread
Results 1 to 4 of 4

OpenText into an Existing Worksheet

  1. #1
    Registered User
    Join Date
    09-22-2006
    Posts
    11

    Question OpenText into an Existing Worksheet

    I would like to Import a Text file into a current Workbook with the worksheet name GLFBCALO. The macro below creates a new workbook instead of imported the data to existing sheet, GLFBCALO. Is there a way to use the OpenText method to import data into an existing worksheet?:


    Public Sub ImportOKdata()
    Dim MyFile As String
    Dim ColumnsDesired
    Dim DataTypeArray
    Dim ColumnArray(0 To 11, 1 To 2)
    Dim x

    Sheets("GLFBCALO").Select
    Cells.Select
    Selection.Clear
    'fill the column and data type info
    'Data Type 1 = general 2 = text, 9 skip
    ColumnsDesired = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    DataTypeArray = Array(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1)

    'populate the array for fieldinfo
    For x = LBound(ColumnsDesired) To UBound(ColumnsDesired)
    ColumnArray(x, 1) = ColumnsDesired(x)
    ColumnArray(x, 2) = DataTypeArray(x)
    Next x
    ' Get file
    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Filters.Add "Text files", "*.txt"
    If .Show = True Then
    MyFile = .SelectedItems(1)
    'Import data
    For x = LBound(ColumnsDesired) To UBound(ColumnsDesired)
    ColumnArray(x, 1) = ColumnsDesired(x)
    ColumnArray(x, 2) = DataTypeArray(x)
    Next x
    Workbooks.OpenText Filename:=MyFile, _
    DataType:=xlDelimited, Tab:=True, FieldInfo:=ColumnArray
    End If
    End With
    End Sub

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Not 100% sure ... but folllowing could work :

    Workbooks.Item.ActiveSheet.OpenText

    HTH
    Cheers
    Carim

  3. #3
    Registered User
    Join Date
    09-22-2006
    Posts
    11

    Thanks

    Thank you very much

  4. #4
    Registered User
    Join Date
    03-27-2014
    Location
    Hungary
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: OpenText into an Existing Worksheet

    Hi Everyone

    I want to do a samething like you. But when I try to use "Workbooks.Item.ActiveSheet.OpenText" code in VBA, than Excel 2007 said "argument is not optional".
    I tried to use google for find my answer but I didn't find useful answer. :S

    But When I use Workbooks.OpenText my code is work fine, but I want to open my text in that sheet when my button is in it.

    Here is my code: (this is not work)

    Function OpenTextFile()

    Dim strFilePath As String
    strFilePath = ActiveWorkbook.Path & "\DATA.asc"

    Workbooks.Item.ActiveSheet.OpenText Filename:=strFilePath, _
    DataType:=xlDelimited, _
    StartRow:=1, _
    TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, _
    Tab:=True, _
    Semicolon:=False, _
    Comma:=False, _
    Space:=True, _
    Other:=False, _
    TrailingMinusNumbers:=True, _
    DecimalSeparator:=","


    End Function

    Do you have any idea what is the correct item? or how can I open it in a current sheet?

    Thanks your answers!
    Last edited by BotosJeti; 03-27-2014 at 07:51 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1