PC Review


Reply
Thread Tools Rate Thread

Macro question opening external text files to retrieve data

 
 
Steve
Guest
Posts: n/a
 
      28th Mar 2007
Hi,

I have a macro that has been written to open to different text files
(using Excel), retreiving some data and bringing it back into my
workbook, (bits shown below). At the end of my macro, I have a column
that I have to use the Text-to-Columns feature using a comma as the
delimiter. All of this works fine until I go to use the macro on a
different file, then it remembers the comma delimiter from that last
part of the macro and therefore won't work again from the beginning
when opening the two text files to retreive data, which by the way
were defaulting to us a "tab" delimiter. I tried adding some
information to macro to the part where I open the text files, however
my macro kept stopping there. Any help would be certainly
appreciated.



Thanks,

Steve


Dim wkbCurrent As Workbook
Set wkbCurrent = ActiveWorkbook

Workbooks.Open Filename:="\\smdsldb01\jobscan\Alt_Items_List.txt"



Dim wkbCurrent2 As Workbook
Set wkbCurrent2 = ActiveWorkbook

Workbooks.Open Filename:="\\smdsldb01\jobscan
\Drawing_Nbr_List.txt"



Columns("L:L").Select
Selection.TextToColumns Destination:=Range("L1"),
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False,
FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

 
Reply With Quote
 
 
 
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      28th Mar 2007
Best to explicitly disable the unwanted separators and enable the desired
separator. From the Recorder, this splits on the character "c":


Sub Macro1()
Selection.TextToColumns Destination:=Range("C4"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="c", FieldInfo:=Array(Array(1, 1), Array(2, 1)),
TrailingMinusNumbers:=True
End Sub

--
Gary''s Student
gsnu200712


"Steve" wrote:

> Hi,
>
> I have a macro that has been written to open to different text files
> (using Excel), retreiving some data and bringing it back into my
> workbook, (bits shown below). At the end of my macro, I have a column
> that I have to use the Text-to-Columns feature using a comma as the
> delimiter. All of this works fine until I go to use the macro on a
> different file, then it remembers the comma delimiter from that last
> part of the macro and therefore won't work again from the beginning
> when opening the two text files to retreive data, which by the way
> were defaulting to us a "tab" delimiter. I tried adding some
> information to macro to the part where I open the text files, however
> my macro kept stopping there. Any help would be certainly
> appreciated.
>
>
>
> Thanks,
>
> Steve
>
>
> Dim wkbCurrent As Workbook
> Set wkbCurrent = ActiveWorkbook
>
> Workbooks.Open Filename:="\\smdsldb01\jobscan\Alt_Items_List.txt"
>
>
>
> Dim wkbCurrent2 As Workbook
> Set wkbCurrent2 = ActiveWorkbook
>
> Workbooks.Open Filename:="\\smdsldb01\jobscan
> \Drawing_Nbr_List.txt"
>
>
>
> Columns("L:L").Select
> Selection.TextToColumns Destination:=Range("L1"),
> DataType:=xlDelimited, _
> TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
> Tab:=False, _
> Semicolon:=False, Comma:=True, Space:=False, Other:=False,
> FieldInfo _
> :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      28th Mar 2007
Excel likes to help. If you've ever imported a comma delimited text file or run
Data|Text to columns (delimited by a comma), then excel will figure you want to
do this when you try to paste the next time.

But excel's memory is forgotten if you close and reopen excel. Another way is
to do a dummy Data|text to columns.

put asdf in an empty cell
select that cell
data|text to columns
delimited
but uncheck all the options
and finish up
And then clean up that dummy cell

=====
You could add something like this to the bottom of your code:

'add to the top of your code
Dim DummyCell As Range


'your code here

With ActiveSheet
Set DummyCell = .Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1)
With DummyCell
.Value = "asdf"
.Cells.TextToColumns Destination:=.Cells(1), _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(1, 1)
.ClearContents
End With
End With

End Sub

Steve wrote:
>
> Hi,
>
> I have a macro that has been written to open to different text files
> (using Excel), retreiving some data and bringing it back into my
> workbook, (bits shown below). At the end of my macro, I have a column
> that I have to use the Text-to-Columns feature using a comma as the
> delimiter. All of this works fine until I go to use the macro on a
> different file, then it remembers the comma delimiter from that last
> part of the macro and therefore won't work again from the beginning
> when opening the two text files to retreive data, which by the way
> were defaulting to us a "tab" delimiter. I tried adding some
> information to macro to the part where I open the text files, however
> my macro kept stopping there. Any help would be certainly
> appreciated.
>
> Thanks,
>
> Steve
>
> Dim wkbCurrent As Workbook
> Set wkbCurrent = ActiveWorkbook
>
> Workbooks.Open Filename:="\\smdsldb01\jobscan\Alt_Items_List.txt"
>
> Dim wkbCurrent2 As Workbook
> Set wkbCurrent2 = ActiveWorkbook
>
> Workbooks.Open Filename:="\\smdsldb01\jobscan
> \Drawing_Nbr_List.txt"
>
> Columns("L:L").Select
> Selection.TextToColumns Destination:=Range("L1"),
> DataType:=xlDelimited, _
> TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
> Tab:=False, _
> Semicolon:=False, Comma:=True, Space:=False, Other:=False,
> FieldInfo _
> :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True


--

Dave Peterson
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      28th Mar 2007
On Mar 28, 8:20 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> Excel likes to help. If you've ever imported a comma delimited text file or run
> Data|Text to columns (delimited by a comma), then excel will figure you want to
> do this when you try to paste the next time.
>
> But excel's memory is forgotten if you close and reopen excel. Another way is
> to do a dummy Data|text to columns.
>
> put asdf in an empty cell
> select that cell
> data|text to columns
> delimited
> but uncheck all the options
> and finish up
> And then clean up that dummy cell
>
> =====
> You could add something like this to the bottom of your code:
>
> 'add to the top of your code
> Dim DummyCell As Range
>
> 'your code here
>
> With ActiveSheet
> Set DummyCell = .Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1)
> With DummyCell
> .Value = "asdf"
> .Cells.TextToColumns Destination:=.Cells(1), _
> DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
> ConsecutiveDelimiter:=False, Tab:=False, _
> Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
> FieldInfo:=Array(1, 1)
> .ClearContents
> End With
> End With
>
> End Sub
>
>
>
>
>
> Steve wrote:
>
> > Hi,

>
> > I have a macro that has been written to open to different text files
> > (using Excel), retreiving some data and bringing it back into my
> > workbook, (bits shown below). At the end of my macro, I have a column
> > that I have to use the Text-to-Columns feature using a comma as the
> > delimiter. All of this works fine until I go to use the macro on a
> > different file, then it remembers the comma delimiter from that last
> > part of the macro and therefore won't work again from the beginning
> > when opening the two text files to retreive data, which by the way
> > were defaulting to us a "tab" delimiter. I tried adding some
> > information to macro to the part where I open the text files, however
> > my macro kept stopping there. Any help would be certainly
> > appreciated.

>
> > Thanks,

>
> > Steve

>
> > Dim wkbCurrent As Workbook
> > Set wkbCurrent = ActiveWorkbook

>
> > Workbooks.Open Filename:="\\smdsldb01\jobscan\Alt_Items_List.txt"

>
> > Dim wkbCurrent2 As Workbook
> > Set wkbCurrent2 = ActiveWorkbook

>
> > Workbooks.Open Filename:="\\smdsldb01\jobscan
> > \Drawing_Nbr_List.txt"

>
> > Columns("L:L").Select
> > Selection.TextToColumns Destination:=Range("L1"),
> > DataType:=xlDelimited, _
> > TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
> > Tab:=False, _
> > Semicolon:=False, Comma:=True, Space:=False, Other:=False,
> > FieldInfo _
> > :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -


Thanks to both of you for the help.... I ended up using Daves reply in
my spreadsheet and everything worked out fine....regards, Steve

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
External data retrieve timescale Richard Edwards Microsoft Excel Programming 1 27th Apr 2006 02:44 PM
Opening/creating external text files from Access =?Utf-8?B?Uy5NLg==?= Microsoft Access 1 9th Aug 2005 08:50 PM
Import external data-text files but placed in the next column ratherthan next empty row Martin Microsoft Excel Discussion 0 28th Jul 2004 06:47 PM
import external data via macro/vba question Drabbacs Microsoft Excel Programming 1 25th May 2004 12:36 AM
Re: Opening text files using macro Ron de Bruin Microsoft Excel Programming 0 9th Jul 2003 03:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:05 PM.