How To Add Range Of Data With Dynamic Criteria With VBA Onto A Different Sheet

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
From the image below, I want to achieve any of the filtered data at the right depending on which month I reference.

NEW_IMAGE.jpg


I am thinking of copying all data for 1s under the column C (month)
Then I delete all date rows (from col A) from the copied data.
After that, I will also delete all DAILY TOTALS rows (Under col A) from the copied data.

Once I am done with the above deletions, my copied data should contain just the items or products.
My next move would be to remove duplicates so that no item appears more than once in the copied data.

From here, using a sumif function against the range the data was copied from used get me result.

But my issue here is that I don't know how to start writing the script to point me to the right direction.

Can someone please help me out? I have tried all what I can but I can't seem to figure out the way out yet.

Ps: I want the output to be on a different sheet.

Thanks in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
See if this OK with you
VBA Code:
Sub test()
    Dim myarea, a
    Dim i As Long
    Dim lr As Long
    Set myarea = Range("c5:c" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(2, 1).Areas
    For Each r In myarea
        a = r.Offset(, -2).Resize(r.Count + 1, 3)
        With CreateObject("scripting.dictionary")
            For i = 1 To UBound(a)
                If UCase(a(i, 1)) <> UCase("Daily Total") And a(i, 1) <> "" Then
                    If Not .exists(a(i, 1)) Then
                        .Add a(i, 1), a(i, 2)
                    End If
                End If
            Next
            Set res = Sheets("Sheet2")
            lr = res.Cells(Rows.Count, 5).End(xlUp).Row
            lr = IIf(lr = 1, 1, lr + 2)
            res.Cells(lr, 5) = MonthName(a(1, 3)) & " TOTAL"
            res.Cells(lr + 1, 5).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
            res.Cells(lr + .Count + 1, 5) = "TOTAL": res.Cells(lr + .Count + 1, 6) = WorksheetFunction.Sum(.items)
        End With
    Next
End Sub
 
Upvote 0
here is some vba that will do it for you:
VBA Code:
Sub test2()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 3))
Range(Cells(1, 5), Cells(2 * lastrow, 6)) = ""
outarr = Range(Cells(1, 5), Cells(2 * lastrow, 6))
indi = 0
mon = 0
sumt = 0
For i = 2 To lastrow
 If inarr(i, 1) <> "DAILY TOTALS" And inarr(i, 2) <> "" Then
  ' copy the row
  If inarr(i, 3) <> mon Then
    If mon <> 0 Then
      outarr(indi, 1) = "Total"
      outarr(indi, 2) = sumt
      indi = indi + 1
      sumt = 0
    End If
    indi = indi + 1
    outarr(indi, 1) = MonthName(inarr(i, 3)) & "  TOTALS"
    mon = inarr(i, 3)
    indi = indi + 1
  End If
  For j = 1 To 2
   outarr(indi, j) = inarr(i, j)
  Next j
  sumt = sumt + inarr(i, 2)
  indi = indi + 1
 End If
Next i
      outarr(indi, 1) = "Total"
      outarr(indi, 2) = sumt
      indi = indi + 1

Range(Cells(1, 5), Cells(2 * lastrow, 6)) = outarr

End Sub
 
Upvote 0
See if this OK with you
VBA Code:
Sub test()
    Dim myarea, a
    Dim i As Long
    Dim lr As Long
    Set myarea = Range("c5:c" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(2, 1).Areas
    For Each r In myarea
        a = r.Offset(, -2).Resize(r.Count + 1, 3)
        With CreateObject("scripting.dictionary")
            For i = 1 To UBound(a)
                If UCase(a(i, 1)) <> UCase("Daily Total") And a(i, 1) <> "" Then
                    If Not .exists(a(i, 1)) Then
                        .Add a(i, 1), a(i, 2)
                    End If
                End If
            Next
            Set res = Sheets("Sheet2")
            lr = res.Cells(Rows.Count, 5).End(xlUp).Row
            lr = IIf(lr = 1, 1, lr + 2)
            res.Cells(lr, 5) = MonthName(a(1, 3)) & " TOTAL"
            res.Cells(lr + 1, 5).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
            res.Cells(lr + .Count + 1, 5) = "TOTAL": res.Cells(lr + .Count + 1, 6) = WorksheetFunction.Sum(.items)
        End With
    Next
End Sub
I get the error "object variable or With block variable not set".
 
Upvote 0
here is some vba that will do it for you:
VBA Code:
Sub test2()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 3))
Range(Cells(1, 5), Cells(2 * lastrow, 6)) = ""
outarr = Range(Cells(1, 5), Cells(2 * lastrow, 6))
indi = 0
mon = 0
sumt = 0
For i = 2 To lastrow
If inarr(i, 1) <> "DAILY TOTALS" And inarr(i, 2) <> "" Then
  ' copy the row
  If inarr(i, 3) <> mon Then
    If mon <> 0 Then
      outarr(indi, 1) = "Total"
      outarr(indi, 2) = sumt
      indi = indi + 1
      sumt = 0
    End If
    indi = indi + 1
    outarr(indi, 1) = MonthName(inarr(i, 3)) & "  TOTALS"
    mon = inarr(i, 3)
    indi = indi + 1
  End If
  For j = 1 To 2
   outarr(indi, j) = inarr(i, j)
  Next j
  sumt = sumt + inarr(i, 2)
  indi = indi + 1
End If
Next i
      outarr(indi, 1) = "Total"
      outarr(indi, 2) = sumt
      indi = indi + 1

Range(Cells(1, 5), Cells(2 * lastrow, 6)) = outarr

End Sub
I get the error "subscript out of range"
 
Upvote 0
I get the error "object variable or With block variable not set".
oops - sorry about the first error message. That came from me trying to qualify the Active sheet name.

after i maintained the code just as you wrote it, the new error message is:

run-time error 1004
Application-defined or object-defined error.
 
Upvote 0
Have you selected debug when the error occurs
an alternative, select the first line of the subroutine then press function key 8 , this will step into the sub, keep repeating the Function key 8 through the sub until you get to the error.
or when you get to a loop put a breakpoint at the "next i " command and press function key 5
 
Upvote 0
Have you selected debug when the error occurs
an alternative, select the first line of the subroutine then press function key 8 , this will step into the sub, keep repeating the Function key 8 through the sub until you get to the error.
or when you get to a loop put a breakpoint at the "next i " command and press function key 5
bug_iamge.jpg

The error came in inside the loop when i = 3 but i could not identify the exact line on which it occurred.

The
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,196
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top