We help IT Professionals succeed at work.

Copy unique data and count total

Billa7
Billa7 asked
on

Please help create a macro which is able to copy only unique title from Column D (Data sheet) to Total sheet (Column A). However, at Total sheet need to count how many time and at what date (Column A, Data sheet) the data are appeared in Data sheet. This value need to be captured at Total sheet according to date (B2:AF2).  I have manually copied few data at Total sheet for Experts to get a better view. Hope Experts could help me create this macro.



Count1.xls
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2011
Commented:
Try this - note you will need to unmerge cells A1 and A2 on the Total sheet first:

Sub MakeSummaryTable()
   Dim lngLastRowData As Long
   Dim lngLastRowTotals As Long
   
   ' clear existing data
   Sheets("Total").UsedRange.Offset(2).ClearContents
   
   ' Get unique list
   With Sheets("Data")
      ' get last used row in col D (Titles)
      lngLastRowData = .Cells(.Rows.Count, "D").End(xlUp).Row
      .Range("D1:D" & lngLastRowData).AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=Sheets("Total").Range("A2"), Unique:=True
   End With
   
   With Sheets("Total")
      ' get last used row in col A (Titles)
      lngLastRowTotals = .Cells(.Rows.Count, "A").End(xlUp).Row
      ' sort Titles
      .Range("A2", .Cells(lngLastRowTotals, "A")).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
      
       .Range("B3:AF" & lngLastRowTotals).FormulaR1C1 = _
        "=SUMPRODUCT((DAY(Data!R2C1:R" & lngLastRowData & "C1)=R2C)*(Data!R2C4:R" & lngLastRowData & "C4=RC1))"
   End With
End Sub

Open in new window

Author

Commented:
Hi Rorya,

Thanks for the code. Is that any possible when I run this code with different set of data at Data sheet,
keep the the old data at Total sheet, but update the new eatery at column A and at the same time continues counting counting the total availability of the data at column (B2:AF2 - Total sheet).  
Most Valuable Expert 2011
Top Expert 2011

Commented:
Not sure what you mean - do you mean that you would be entering just a new month's data but still want to keep the previous totals (and add to them where applicable) on the Total sheet? If so, that will require a completely different approach.

Author

Commented:
Hi Rorya,

You're right. Hope you will consider this.
Most Valuable Expert 2011
Top Expert 2011

Commented:
Yes - bit busy at the moment though. If no-one jumps in beforehand, I will try and take a look in the next few days.

Author

Commented:
Hi Rorya,

I found a workaround process to achieve the result without having a new macro. Your suggested code just doing the job. Is that way for us not displaying the formula at the cell? the end result only display the value when we click at the cell.
Most Valuable Expert 2011
Top Expert 2011
Commented:
Do you mean you want the formulas replaced with static values, or that you don't want anything to appear in the cell unless you click on it?

If the former, then change lines 24 and 25 to:

With .Range("B3:AF" & lngLastRowTotals)
   .FormulaR1C1 = _
        "=SUMPRODUCT((DAY(Data!R2C1:R" & lngLastRowData & "C1)=R2C)*(Data!R2C4:R" & lngLastRowData & "C4=RC1))"
   .Value = .Value
End With

Open in new window

Author

Commented:
Excellent!!
Thanks a lot Rorya.