Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Import Text file into an existing Worksheet

My code opens a new workbook. Instead I need the data to be imported into an
existing worksheet name GLFBCALO. Is there a way I can do this?

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

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200609/1

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Import Text file 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, sh as worksheet

Sheets("GLFBCALO").Select
set sh = Activesheet
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
Activesheet.usedRange.Copy Destination:=sh.Range("A1")
ActiveWorkbook.close SaveChanges:=False
End Sub

--
Regards,
Tom Ogilvy


"Fredriksson via OfficeKB.com" wrote:

My code opens a new workbook. Instead I need the data to be imported into an
existing worksheet name GLFBCALO. Is there a way I can do this?

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

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200609/1


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Import Text file into an existing Worksheet

Thank you very much. I was trying to used a previous example that you replied.
I now know what I did wrong. I forgot to declare sh as a worksheet. Thanks
for your help. I appreciate it

Tom Ogilvy wrote:
Public Sub ImportOKdata()
Dim MyFile As String
Dim ColumnsDesired
Dim DataTypeArray
Dim ColumnArray(0 To 11, 1 To 2)
Dim x, sh as worksheet

Sheets("GLFBCALO").Select
set sh = Activesheet
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
Activesheet.usedRange.Copy Destination:=sh.Range("A1")
ActiveWorkbook.close SaveChanges:=False
End Sub

My code opens a new workbook. Instead I need the data to be imported into an
existing worksheet name GLFBCALO. Is there a way I can do this?

[quoted text clipped - 35 lines]
End With
End Sub


--
Message posted via http://www.officekb.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do a import data from a text file to an excel worksheet madlin Excel Discussion (Misc queries) 4 January 12th 10 04:04 PM
import data from txt file to an existing excel file shaji Excel Discussion (Misc queries) 1 September 12th 09 04:15 PM
how to import more than 1 text file into the same Excel worksheet. SinnetBS Excel Discussion (Misc queries) 1 June 14th 06 03:14 AM
Changing only source file of pre-existing text import QueryTable? EBrowne Excel Programming 3 August 23rd 04 03:31 AM
Help: Impot Text File to existing worksheet tratliff[_2_] Excel Programming 1 August 4th 04 09:31 PM


All times are GMT +1. The time now is 07:21 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"