I have a question as I am puzzled with how code acts. If I run the below code, sometimes, the input in the 'raw' sheet gets deleted completed. If I re-start xls and run the code using the same (!) input in the raw sheet, it runs just fine. Do you have an idea, what's the reason for it as I am totally clueless? And how could I resolve it?
Many thanks, Eka
Sub dataset_transformation()
Dim irow As Long
Dim icol As Integer
Dim lastRw As Long
On Error Resume Next
'Deleting empty rows
'Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Deleting the temp sheet on the workbook (in case it exists)
Application.DisplayAlerts = False
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "interim" Then
Sheet.Delete
End If
Next Sheet
'Adding the brand new working sheets
Sheets.Add After:=Sheets("raw")
ActiveSheet.Name = "interim"
Sheets("raw").Select
'Loop through rows - Bottom to top
For irow = Cells.SpecialCells(xlLastCell).Row To 2 Step -1
'Loop Through Columns right to left
For icol = Cells.SpecialCells(xlLastCell).Column To 1 Step -1
'If Cell is Bold - Do Nothing
If Cells(irow, icol).Font.FontStyle = "Bold" Then
'If Cell is Normal and Not empty - Do nothing
ElseIf Cells(irow, icol).Font.FontStyle = "Regular" And Not IsEmpty(Cells(irow, icol)) Then
'Otherwise - Delete row
Else
Cells(irow, icol).EntireRow.Delete
'Exit Loop
Exit For
End If
Next icol
Next irow
'Removing the extra space in the amount column
'Range("B1:D" & Cells(Rows.Count, 1).End(xlUp).Row).Replace What:=".", Replacement:=",", SearchOrder:=xlByColumns
Range("B1:D" & Cells(Rows.Count, 1).End(xlUp).Row).Replace What:=" ", Replacement:="", SearchOrder:=xlByColumns
Rows("1:2").Select
Range("A2").Activate
Selection.Delete Shift:=xlUp
'Converting count & sum columns to numbers
Columns("B:B").Select
'Range("B226").Activate
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Columns("D:D").Select
'Range("D226").Activate
Selection.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
'Identifying the intend level
'Dim CurCell As Range
'Set CurCell = ActiveCell
Cells(2, 1).Select
Do While Trim(ActiveCell.Value) <> ""
ActiveCell.Offset(0, 4).Value = ActiveCell.IndentLevel
ActiveCell.Offset(1, 0).Select
Loop
'Copying the Ylan-Yde data to a new sheet
Columns("A:A").Select
Selection.Find("??????? ATLAS ????-???", LookIn:=xlValues).Select
Range(ActiveCell.Address & ":" & Cells(Cells(Rows.Count, "A").End(xlUp).Row, ActiveCell.Column + 4).Address).Select
Selection.Copy
'Pasting the Ylan-Yde data to the new sheet
Sheets("interim").Select
Range("A1").Select
ActiveSheet.Paste
'Creating the column which says whether it is a main shop or Ylan-Yde
'Main shop
Sheets("raw").Select
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("B3").Select
Selection.Copy
Range("A1").Select
ActiveSheet.Paste
Selection.AutoFill Destination:=Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row)
'Ylan-Yde
Sheets("interim").Select
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("B1").Select
Selection.Copy
Range("A1").Select
ActiveSheet.Paste
Selection.AutoFill Destination:=Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row)
'Deleting the Ylan-Yde data from the Atlas data
Sheets("raw").Select
Columns("B:B").Select
Selection.Find("??????? ATLAS ????-???", LookIn:=xlValues).Select
Rows(ActiveCell.Row & ":" & Rows.Count).Delete
'Deleting the total sum row
Sheets("interim").Select
ActiveSheet.Cells(Rows.Count, "A").End(xlUp).EntireRow.Delete
'Calculating the % contribution to total - main shop sheet
Sheets("raw").Select
Range("G3").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/R3C5"
Selection.AutoFill Destination:=Range("G3:G" & Range("A" & Rows.Count).End(xlUp).Row)
ActiveSheet.Columns("G").Copy
ActiveSheet.Columns("G").PasteSpecial xlPasteValues
'Calculating the % contribution to total - Ylan-Yde sheet
Sheets("interim").Select
Range("G1").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/R1C5"
Selection.AutoFill Destination:=Range("G1:G" & Range("A" & Rows.Count).End(xlUp).Row)
ActiveSheet.Columns("G").Copy
ActiveSheet.Columns("G").PasteSpecial xlPasteValues
'Copying the Yland-Yde data back to the main shop data
Range("A1:G" & Cells(Rows.Count, "A").End(xlUp).Row).Select
Selection.Copy
Sheets("raw").Select
lastRw = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lastRw + 1).Select
ActiveSheet.Paste
'Naming the newly created cols
Range("A1").Value = "M"
Range("A2").Value = ""
Range("F1").Value = "L"
Range("F2").Value = ""
Range("G1").Value = "%"
Range("G2").Value = ""
Range("B1").Select
Selection.Copy
Range("A1:A2").Select
Range("A2").Activate
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("B1").Select
Selection.Copy
Range("F1:G2").Select
Range("G2").Activate
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'Changing the format of the % contribution to %
Columns("G:G").Select
Selection.Style = "Percent"
Selection.NumberFormat = "0.00%"
'Adding the blue background
Range(Cells(1, "G"), Cells(Cells(Rows.Count, 1).End(xlUp).Row, "F")).Select
With Selection.Interior
.PatternColorIndex = xlAutomatic
.Color = 16777200
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'Adding the table borders
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ThemeColor = 9
.TintAndShade = 0
.Weight = xlThin
End With
'Deleting the interim sheet
Sheets("interim").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
MsgBox "Whoop, whoop, that's all folks!"
End Sub