+ Reply to Thread
Results 1 to 3 of 3

Extracting data from the offset of a cell, to be put into a new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2008
    Posts
    56

    Extracting data from the offset of a cell, to be put into a new sheet

    Hello and thanks for reading this, and thanks in advance for your help for which I truly appreciate.

    I have a sheet of data (pls refer attachment - it's a zip file because excelforum doesn't allow *.xlsm uploads) which needs some processing. I need to scan column C (stock level) for those which are not at zero stock quantity, and take the following data to be arranged into a new worksheet according to rows:

    copy offset(-1, 0) to Sheet2 A2,A3,A4 and so on
    copy offset(-1, 1) to Sheet2 B2,B3,B4 and so on
    copy offset(0, 1) to Sheet2 C2,C3,C4 and so on
    copy offset(0, 2) to Sheet2 D2,D3,D4 and so on

    I'm still quite a noob at VBA programming so I could only come up with the code below (which I'm pretty sure has errors as it is):

    Sub Extract_Media_Data()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim cell As Range
    Dim ColumnC As Range
    
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1")
    Set ColumnC = ws.Range("C1:C1000")
    
    'create new worksheet
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(2).Name = "Sheet2"
    
    'set title for column C and D
    wb.Worksheets("Sheet2").Range("C1") = "Low Yat"
    wb.Worksheets("Sheet2").Range("D1") = "Digital Mall"
    Sheets("Sheet1").Activate
    
    '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
    'copy offset(-1, 1) to Sheet2 B2,B3,B4 and so on
    'copy offset(0, 1) to Sheet2 C2,C3,C4 and so on
    'copy offset(0, 2) to Sheet2 D2,D3,D4 and so on
        Else
        End If
    Next cell
    
    'sort according to column B
    Sheets("Sheet2").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
    I would greatly appreciate it if someone can please help teach me how to fill in the blanks in the IF...THEN section, as well as help me check if the rest of my coding is correct.
    Sheet1 of the attachment is the demo data (arbitrary and is bound to change), and Sheet2 is an example of how the data is to be arranged.

    Thank you very much!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    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

  3. #3
    Registered User
    Join Date
    05-14-2008
    Posts
    56
    hey, sorry for the late reply wasn't in the office for the whole day.

    tried your code, had to edit it a little for it to work properly but it did the job nonetheless. a very big thank you, i learned new code again today

    thanks again!

    ~Michael~

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1