Option Explicit
Sub StrangeFormOfTransposingLikeTazmanianDevil()
' constants
Const ksSourceWS = "Before&After"
Const ksSourceRng = "SourceTable"
Const ksTargetWS = "Before&After"
Const ksTargetRng = "TargetTable"
Const ksTargetNRng = "Target0Table"
Const ksTargetN = "0"
Const kiTargetN = 4
Const ksNumbers = "0123456789-"
' declarations
Dim rngS As Range, rngT As Range, c As Range, rngTN(kiTargetN) As Range
Dim lIndex(kiTargetN) As Long
Dim I As Long, J As Long, K As Integer, A As String
' start
Set rngS = Worksheets(ksSourceWS).Range(ksSourceRng)
Set rngT = Worksheets(ksTargetWS).Range(ksTargetRng)
With rngT
If .Rows.Count > 2 Then Range(.Rows(2), .Rows(.Rows.Count)).ClearContents
End With
For I = 1 To kiTargetN
Set rngTN(I) = Worksheets(ksTargetWS).Range(Replace(ksTargetNRng, ksTargetN, CStr(I)))
lIndex(I) = 1
Next I
' process
With rngS
I = 1
Do Until .Cells(I, 1).Value = ""
Set c = .Cells(I, 1)
If c.Offset(0, 1).Value <> "" Then
J = 0
' case 1: 2 cols, number and anything
If J = 0 Then
If IsNumeric(c.Value) And Not (IsNumeric(c.Offset(0, 1).Value)) Then
J = 1
End If
End If
' case 2: 3 cols, numbers
If J = 0 Then
If IsNumeric(c.Value) And IsNumeric(c.Offset(0, 1).Value) And _
IsNumeric(c.Offset(0, 1).Value) Then
J = 2
End If
End If
' case 3: 2 cols, number with dash and number
If J = 0 Then
For K = 1 To Len(c.Value)
If InStr(ksNumbers, Mid(c.Value, K, 1)) = 0 Then Exit For
Next K
If K > Len(c.Value) Then
J = 3
End If
End If
' case 4: 2 cols, letter&number and number
If J = 0 Then
If Not (IsNumeric(c.Value)) And IsNumeric(c.Offset(0, 1).Value) Then
J = 4
End If
End If
'
If J > 0 Then
lIndex(J) = lIndex(J) + 1
Range(c, c.End(xlToRight)).Copy rngTN(J).Cells(lIndex(J), 1)
End If
End If
I = I + 1
Loop
End With
' end
For I = 1 To kiTargetN
Set rngTN(I) = Nothing
Next I
Set rngT = Nothing
Set rngS = Nothing
Beep
End Sub
Hi, tazz!Give a look at the uploaded file. It uses a few dynamic named ranges for easier referencing:
SourceTable: =DESREF('Before&After'!$BA$2;;;CONTARA('Before&After'!$BA:$BA)-1;3) -----> in english: =OFFSET('Before&After'!$BA$2,,,COUNTA('Before&After'!$BA:$BA)-1,3)
TargetTable: =DESREF('Before&After'!$BF$1;;;MAX(CONTARA('Before&After'!$BF:$BF);CONTARA('Before&After'!$BH:$BH);CONTARA('Before&After'!$BK:$BK);CONTARA('Before&After'!$BM:$BM));9) -----> in english: =OFFSET('Before&After'!$BF$1,,,MAX(COUNTA('Before&After'!$BF:$BF),COUNTA('Before&After'!$BH:$BH),COUNTA('Before&After'!$BK:$BK),COUNTA('Before&After'!$BM:$BM)),9)
Target1Table: =DESREF(TargetTable;;0;CONTARA('Before&After'!$BF:$BF);2) -----> in english: =OFFSET(TargetTable,,0,COUNTA('Before&After'!$BF:$BF),2)
Target2Table: =DESREF(TargetTable;;2;CONTARA('Before&After'!$BH:$BH);3) -----> in english: =OFFSET(TargetTable,,2,COUNTA('Before&After'!$BH:$BH),3)
Target3Table: =DESREF(TargetTable;;5;CONTARA('Before&After'!$BK:$BKF);2) -----> in english: =OFFSET(TargetTable,,5,COUNTA('Before&After'!$BK:$BK),2)
Target4Table: =DESREF(TargetTable;;7;CONTARA('Before&After'!$BM:$BM);2) -----> in english: =OFFSET(TargetTable,,7,COUNTA('Before&After'!$BM:$BM),2)
Const ksSourceWS = "DATA1"
Const ksTargetWS = "DATA1"
Set rngS = Worksheets(ksSourceWS).Range(ksSourceRng)
Set rngT = Worksheets(ksTargetWS).Range(ksTargetRng)
Set rngS = Activesheet.Range(ksSourceRng)
Set rngT = Activesheet.Range(ksTargetRng)
Set rngTN(I) = Worksheets(ksTargetWS).Range(Replace(ksTargetNRng, ksTargetN, CStr(I)))
Set rngTN(I) = Activesheet.Range(Replace(ksTargetNRng, ksTargetN, CStr(I)))