Public Sub Row_Col()
Dim WB1 As Workbook, WB2 As Workbook, EWB As Workbook
Dim WS1 As Worksheet, WS2 As Worksheet, WS3 As Worksheet
Dim Path As String
Path = Environ("USERPROFILE") & "\Desktop\"
File1Name = "WRKBOOK 1.xlsx"
FullName = Path & File1Name
On Error Resume Next
Set EWB = Application.Workbooks(File1Name)
If EWB Is Nothing Then
If Dir(FullName) <> "" Then Kill FullName
Kill FullName
Set WB1 = Workbooks.Add
Else
EWB.Close False
If Dir(FullName) <> "" Then Kill FullName
Kill FullName
Set WB1 = Workbooks.Add
End If
On Error GoTo 0
With WB1
.SaveAs Filename:=FullName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
TxT = "EXAMPLE,EXAMPLE,EXAMPLE,EXAMPLE,EXAMPLE,EXAMPLE,code1,code2,code3,code4" & _
";,,,TYPE,state,ZONE,abs1,resp,ems,DOA" & _
";LINE,DIGIT,NOM,Auto,Auto,Auto,QTY,QTY,QTY,QTY" & _
";,,,,,,,,," & _
";1,11111,Smith,tea,BC,red,2,,9," & _
";2,44444,Drumph,snod,DE,orange,,,1," & _
";3,56789,Chuck,dok,ZA,blue,1,2,3,5"
Set WS1 = .Worksheets("Sheet1")
With WS1
Arr = Split(TxT, ";")
For x = LBound(Arr) To UBound(Arr)
.Range("A" & x + 1 & ":J" & x + 1) = (Split(Arr(x), ","))
Next
Set WS2 = Sheets.Add(After:=.Parent.Worksheets(.Name))
End With
With WS2
ShtName = WS1.Name
RWcLRngAdrs = WS1.Range("G5:J7").Address(True, True)
AggFuctn = "AGGREGATE(15,6,((COLUMN(" & ShtName & "!" & RWcLRngAdrs & ")-COLUMN(" & ShtName & "!$G$5))+((ROW(" & ShtName & "!" & RWcLRngAdrs & ")-ROW(" & ShtName & "!$G$5))*COLUMNS(" & ShtName & "!" & RWcLRngAdrs & ")+1))/--(" & ShtName & "!" & RWcLRngAdrs & "<>0),ROWS($A$2:A2))"
With .Cells(1, 1).Resize(1, 8)
.Value = [{"LINE","DIGIT","NOM","TYPE","STATE","ZONE","DIR","QTY*"}]
With .Interior
.Pattern = xlSolid
.Color = RGB(37, 97, 149)
.TintAndShade = 0
End With
With .Font
.Color = RGB(255, 255, 255)
.Bold = True
End With
End With
For I = 1 To 6
IndxAdrs = ShtName & "!" & .Cells(5, I).Resize(55, 1).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",CEILING(" & Replace(AggFuctn, "$A$2:A2", K) & ",COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))/COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "),1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Next
IndxAdrs = ShtName & "!" & .Cells(2, 7).Resize(1, 4).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",1,MOD(" & Replace(AggFuctn, "$A$2:A2", K) & "-1,COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))+1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
IndxAdrs = ShtName & "!" & .Cells(5, 7).Resize(55, 4).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I + 1).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",CEILING(" & Replace(AggFuctn, "$A$2:A2", K) & ",COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))/COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "),MOD(" & Replace(AggFuctn, "$A$2:A2", .Cells(2, I + 1).Address(True, True) & ":" & .Cells(2, I + 1).Address(0, 0)) & "-1,COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))+1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
''''''''''''''''''
With .Range("G5:J7").Offset(-3, -6).Resize(50, 8)
Cll1 = .Cells(1, 1).Address(0, 1)
.Cells.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2<>"""",MOD(ROW($A2),2)<>0)"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15586489
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
.Tab.Color = RGB(255, 0, 0)
End With
WS1.Activate
.Save
End With
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
File2Name = "WRKBOOK 2.xlsx"
FullName = Path & File2Name
On Error Resume Next
Set EWB = Application.Workbooks(File2Name)
If EWB Is Nothing Then
If Dir(FullName) <> "" Then Kill FullName
Kill FullName
Set WB2 = Workbooks.Add
Else
EWB.Close False
If Dir(FullName) <> "" Then Kill FullName
Kill FullName
Set WB2 = Workbooks.Add
End If
On Error GoTo 0
With WB2
.SaveAs Filename:=FullName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Set WS3 = .Worksheets("Sheet1")
With WS3
ShtName = "'[" & WB1.Name & "]" & WS3.Name & "'"
RWcLRngAdrs = WS1.Range("G5:J7").Address(True, True)
With .Cells(1, 1).Resize(1, 8)
.Value = [{"LINE","DIGIT","NOM","TYPE","STATE","ZONE","DIR","QTY*"}]
With .Interior
.Pattern = xlSolid
.Color = RGB(37, 97, 149)
.TintAndShade = 0
End With
With .Font
.Color = RGB(255, 255, 255)
.Bold = True
End With
End With
AggFuctn = "AGGREGATE(15,6,((COLUMN(" & ShtName & "!" & RWcLRngAdrs & ")-COLUMN(" & ShtName & "!$G$5))+((ROW(" & ShtName & "!" & RWcLRngAdrs & ")-ROW(" & ShtName & "!$G$5))*COLUMNS(" & ShtName & "!" & RWcLRngAdrs & ")+1))/--(" & ShtName & "!" & RWcLRngAdrs & "<>0),ROWS($A$2:A2))"
For I = 1 To 6
IndxAdrs = ShtName & "!" & .Cells(5, I).Resize(55, 1).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",CEILING(" & Replace(AggFuctn, "$A$2:A2", K) & ",COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))/COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "),1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Next
IndxAdrs = ShtName & "!" & .Cells(2, 7).Resize(1, 4).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",1,MOD(" & Replace(AggFuctn, "$A$2:A2", K) & "-1,COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))+1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
IndxAdrs = ShtName & "!" & .Cells(5, 7).Resize(55, 4).Address(True, True)
K = .Cells(2, I).Address(True, True) & ":" & .Cells(2, I).Address(0, 0)
With .Cells(2, I + 1).Resize(50, 1)
.Formula = "=IFERROR(INDEX(" & IndxAdrs & ",CEILING(" & Replace(AggFuctn, "$A$2:A2", K) & ",COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))/COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "),MOD(" & Replace(AggFuctn, "$A$2:A2", .Cells(2, I + 1).Address(True, True) & ":" & .Cells(2, I + 1).Address(0, 0)) & "-1,COLUMNS(" & ShtName & "!" & RWcLRngAdrs & "))+1),"""")"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
'''''''''''''''''''''''''''
With .Range("G5:J7").Offset(-3, -6).Resize(50, 8)
Cll1 = .Cells(1, 1).Address(0, 1)
.Cells.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2<>"""",MOD(ROW($A2),2)<>0)"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15586489
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End With
End With
End Sub