Try to use this code:
Sub Extract_Media_Data()
Dim wb As Workbook
Dim ws As Worksheet
Dim sh2 As Worksheet
Dim cell As Range
Dim ColumnC As Range
Dim lastRow As Long
Dim sh2Row As Long
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
lastRow = Cells(Rows.Count, "c").End(xlUp).Row
Set ColumnC = ws.Range("C6:C" & lastRow)
sh2Row = 1
'create new worksheet
If ThisWorkbook.Sheets.Count = 1 Then
Set sh2 = Sheets.Add
sh2.Move after:=Sheets(Sheets.Count)
sh2.Name = "Sheet2"
Else
Set sh2 = ThisWorkbook.Sheets("sheet2")
End If
'set title for column C and D
wb.Worksheets("Sheet2").Range("C1") = "Low Yat"
wb.Worksheets("Sheet2").Range("D1") = "Digital Mall"
'from cell C1 to C1000
For Each cell In ColumnC
'if the total quantity is zero then do nothing
If cell.Value <> 0 Then
'copy offset(-1, 0) to Sheet2 A2,A3,A4 and so on
sh2Row = sh2Row + 1
sh2.Cells(sh2Row, "a") = ws.Cells(cell.Row - 1, "b")
'copy offset(-1, 1) to Sheet2 B2,B3,B4 and so on
sh2.Cells(sh2Row, "b") = ws.Cells(cell.Row - 1, "d")
'copy offset(0, 1) to Sheet2 C2,C3,C4 and so on
sh2.Cells(sh2Row, "c") = ws.Cells(cell.Row, "d")
'1copy offset(0, 2) to Sheet2 D2,D3,D4 and so on
sh2.Cells(sh2Row, "d") = ws.Cells(cell.Row, "e")
Else
End If
Next cell
'sort according to column B
sh2.Activate
Cells.Select
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("B:B") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("A1:K123")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Regards,
Antonio
Bookmarks