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
1,879
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.
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
925
Office Version
  1. 2013
Platform
  1. Windows
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
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,658
Office Version
  1. 2010
Platform
  1. Windows
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows
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".
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

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"
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows
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.
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,658
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

which line does it occur on
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,658
Office Version
  1. 2010
Platform
  1. Windows
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,962
Messages
5,599,065
Members
414,281
Latest member
Engjamal2021

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
Top