fixed length field - VBA

missv

New Member
Joined
Apr 16, 2010
Messages
3
i'm working on a VBA code in which i need to define each field as a specific length, and justify (or pad) the empty spaces. each column needs to be defined a specific length and will not change in the future.

i'm thinking i need to define the range as a string, with a fixed length type? does anyone have a snippet of code or some suggestions on how to write this? i can't imagine it would be terribly hard, but i've already spent a few unsuccessful hours already. :confused:

any help is much appreciated!
 
thanks, i've gotten that far. however, i keep getting errors when i try to define the range(A:A) as s.

will this pad spaces as well?
 
Upvote 0
I don't really understand what you want to do. To set the column width

Code:
Columns("A").ColumnWidth = 10

but that won't relate directly to a 10 character fixed-length string.
 
Upvote 0
maybe this will explain a little better. i have 4 columns.

column 1 needs to be fixed width of 10. however, the actual data varies in the length, so i need to pad spaces on the right to get to a fixed width of 10.

column 2 - fixed width of 8.
column 3 - fixed width of 15 - need to pad 0's on the left
column 4 - fixed width of 10 - pad spaces on the left.


i feel like Dim s as String * 10 is correct (and so forth for the other fixed length columns), but i'm getting an error trying to set column A as s.

Last, on column 3, after i set it to fixed width of 15, how would i pad the 0s?
 
Upvote 0
Perhaps you can do something like this

Code:
Range("B1").Value = "XXXXXXXX"
Columns("B").AutoFit
Range("B1").ClearContents
Range("C1").Value = "XXXXXXXXXXXXXXX"
With Columns("C")
    .AutoFit
    .NumberFormat = "000000000000000"
End With
Range("C1").ClearContents
 
Upvote 0

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