Importing a text file with VBA

darkwolf45

New Member
Joined
Oct 2, 2009
Messages
33
I am actually redoing this from a file I did a couple years ago. I recorded the macro, gave the file to a coworker who needed it and thought I was done with it. Well, he actually lost the file and no one has it anymore. SO, i recorded what I could again, and at a sticking point, being that I do not know VBA. Here si the code:

Sub formatDHSdata()
'
' formatDHSdata Macro
' Macro recorded 4/7/2011 by Brian Rule
'
'
Workbooks.OpenText Filename:= _
"S:\SO\FIN\REIMB\Data Analysis\MA DSH Audit\2008\FFS fils received from DHS\Abbott\1760446256_FFSDATA_I_F_0812_20110324_102939_DH.dat" _
, Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(2, 1), Array(11, 1), Array(21, 1), Array(25, 1), Array(26, 1), Array(28, _
1), Array(30, 1), Array(38, 1), Array(55, 1), Array(67, 1), Array(68, 1), Array(85, 1), _
Array(95, 1), Array(105, 1), Array(115, 1), Array(135, 1), Array(141, 1), Array(154, 1), _
Array(167, 1), Array(176, 1), Array(189, 1), Array(200, 1), Array(211, 1), Array(224, 1)) _
, TrailingMinusNumbers:=True
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.SmallScroll Down:=-18
ActiveWindow.LargeScroll ToRight:=-1
End Sub


The problem is that there are over 140 files that I need to open and modify, not just the single one at this location. How do I change the command to simply take me to the open dialog box to select the file I want?
Thanks for any isight you can offer,
Brian
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try something like this...

Code:
Sub formatDHSdata()

Dim strFileName As String

' Default drive
ChDrive "S:"

' Default Directory
ChDir "S:\SO\FIN\REIMB\Data Analysis\MA DSH Audit\2008\FFS fils received from DHS\Abbott\"

' Prompt for file
strFileName = Application.GetOpenFilename(FileFilter:="Data Files ,*.dat," & _
                                                      "Text Files ,*.txt," & _
                                                      "All Files ,*.*", _
                                          FilterIndex:=1, _
                                          Title:="Open Data File", _
                                          MultiSelect:=False)

If strFileName = "False" Then Exit Sub  ' if User canceled then exit

' Open selected file
Workbooks.OpenText Filename:=strFileName, _
Origin:=437, StartRow:=1, DataType:=xlFixedWidth, TrailingMinusNumbers:=True


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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