Skip to content

Instantly share code, notes, and snippets.

@rafareino
Created June 13, 2016 22:15
Show Gist options
  • Save rafareino/b5c9e79479de33e58cdb5ae4ba946fd0 to your computer and use it in GitHub Desktop.
Save rafareino/b5c9e79479de33e58cdb5ae4ba946fd0 to your computer and use it in GitHub Desktop.
This code is here to be accessible to anyone trying to solve this issue on StackOverflow http://stackoverflow.com/q/37789605/955143
Input
FD1 FD2 FD3 FD4
-- -- -- --
2 3 3 5
AACCC FUUUUU
AAHHH FGGGGG
55HHH FVVVVV
55HHH--LVVVVV
PPNNN LVVVVV
PPJJJ--LDDDDD
My code, copy/past into a new module in excel
Sub LoopParser()
Const SizesRow = 3
Const DataStart = "A4"
Dim ToCut
ToCut = 0
Range(DataStart).Select
Do Until IsEmpty(ActiveCell)
ToCut = ActiveSheet.Cells(SizesRow, ActiveCell.Column).Value
Call ParseOneField(ActiveCell.Address, CInt(ToCut))
ActiveCell.Offset(0, 1).Select
Loop
Range(DataStart).Select
End Sub
Sub ParseOneField(TargetCell, DesiredSize As Integer)
Const MaxLayout = 10000#
Range(TargetCell).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.TextToColumns _
Destination:=Range(TargetCell), _
DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, xlTextFormat), _
Array(DesiredSize, xlTextFormat), _
Array(MaxLayout, xlTextFormat) _
), _
TrailingMinusNumbers:=True
End Sub
FD1 FD2 FD3 FD4
-- -- -- --
2 3 3 5
AA CCC FUU UUU
AA HHH FGG GGG
55 HHH FVV VVV
55 HHH --L VVVVV
PP NNN LVV VVV
PP JJJ --L DDDDD
When the expected result would be (preserving blanks in the third field)
FD1 FD2 FD3 FD4
-- -- -- --
2 3 3 5
AA CCC F UUUUU
AA HHH F GGGGG
55 HHH F VVVVV
55 HHH --L VVVVV
PP NNN L VVVVV
PP JJJ --L DDDDD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment