Yes I know : it's not fancy but I saw from time to time always the same subject : how to …
Yes I know : many samples on web whatever in forums or blogs, so I thought of some lazy people requering to do their job !
But recently I saw a blog on this subject, a nice looking webpage but so ugly is the code ‼
I was very surprised some VBA basics was not used as it's easier for beginners.
So I searched on web and all I found was often long codes with useless stuff always trying to fill holes adding spaces
but never using VBA basics inherited in fact from its grand pa' : the BASIC ‼
-- ★ --
First well think about your export text file destination as Excel already has an easy feature : Save As !
If the receiver of this text file can warm a couple of neurons to fit an import procedure in its database for example
then you do not ever need any VBA procedure !
- First enlarge or reduce the columns widths to well fit the fixed length text format.
- In the File menu select Save As then from the File Type drop-down choose Formatted Text (Space delimited) (*.prn) …
After creating the .prn text file you can easily rename it if needed as .txt for example.
And if you really need a
macro code, see the
Worksheets.SaveAsmethod in VBA help or for a starter just
activate the Macro Recorder before operating manually : you will get your own free code, at beginner level …
(You can first copy the sheet to a new workbook, see
Worksheets.Copyin VBA help.)
No need to continue reading this thread anymore ! Yes I know : I make your day, bye !
-- ★ ★ --
You are still here ‼
So that's the second case : the database expects only a specific fixed format the receiver already gave you.
And as you do not succeed with the previous Save As Excel easy feature then you - maybe - need
a VBA code to export data to a fixed length text file : just keep reading this thread !
-- ★ ★ ★ --
Next posts consist on several demonstrations for two basics ways, from very beginner level
to beginner / advanced level and a bonus demonstration as a 'cherry on the cake' code,
the final post for additional information and a workbook attachment with all demonstrations.
Learning by examples and by reading as it's only VBA basics, all is already in VBA help !
As often I merely can't better explain than VBA help so before any question just well read it …
You try but you fail : do not create any post here but as per this forum rules (in particular How to get the Best Results)
just create your own thread in the appropriate VBA Macros section.