VBA to Create Table and to Remove Duplicates

jessebh2003

Board Regular
Joined
Feb 28, 2020
Messages
71
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have two items for my data where I could use some help.

Item 1
The data needs turned into a table but the number of rows varies (the columns are always the same). I used to macro recorder for these and tweaked them.

First I tried the below but the code locked in the range.
VBA Code:
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$K$13200"), , xlYes).Name = "Table1"
    Range("Table1[#All]").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight13"
    ActiveSheet.ListObjects("Table1").ShowTableStyleRowStripes = False
    Rows("1:1").Select
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0

Then I tried selecting the cells in row 1 for each column and doing Ctrl+Shift+Down but the code locked in the range.
VBA Code:
    Range("A1:K1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$K$13200"), , xlYes).Name = "Table1"

I looked online and watched videos and much of it described writing code to actually create the table from scratch but that didn't seem right. Is there an easier way?

Item 2
In this same table, I need to remove overlapping data. I was given this formula and instructions on how to use it. Here are the instructions:

  1. Paste this formula into column L2, =D2&"~"&B2&"~"&DAY(G2)&MONTH(G2)&YEAR(G2)&"~"&TEXT(TIME(HOUR(G2),0,0),"hh:mm:ss AM/PM")
  2. Sort the spreadsheet by the new column and then by coverage Greatest to Smallest - This is so when we remove duplicates it will keep the record with the largest coverage value.
  3. On the data tab in excel click on remove duplicates and uncheck all columns except the new one we made in step 1 then push ok.

The problem is if the overlapping time crosses into the next hour. Because formula pulls the hour from G2 into L2, when I remove the duplicates, these overlapping entries aren't removed.
  • Jane Doe: The calculated end time (Insert Date + Watch Time) is 11/29/2021 16:00:42, which is greater than the time in G3 (15:34:59).
  • John Miller: The calculated end time (Insert Date + Watch Time) is 03/28/2022 19:47:53, which is greater than the time in G5 (19:06:48).
  • Mary Thomas: The calculated end time (Insert Date + Watch Time) is 03/22/2022 18:48:03, which is greater than the time in G7 (18:04:53).

How can I remove the duplicates including those when the Insert Time crosses into the next hour? Doesn't have to be this formula either; whatever will work best and produce the result I need.

VBA Code:
    Range("L1").Formula2R1C1 = "Duplicate"
    Range("L2").Formula2R1C1 = "=RC[-8]&""~""&RC[-10]&""~""&DAY(RC[-5])&MONTH(RC[-5])&YEAR(RC[-5])&""~""&TEXT(TIME(HOUR(RC[-5]),0,0),""hh:mm:ss AM/PM"")"
    Columns("L:L").EntireColumn.AutoFit
    With ActiveSheet.ListObjects("Table1").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("Table1[Duplicate]"), Order:=xlAscending
        .Header = xlYes
        .Apply
    End With
    ActiveSheet.Range("Table1[#All]").RemoveDuplicates Columns:=12, Header:=xlYes


Copy of Video Tracking Report-original cleaned.xlsx
ABCDEFGHIJKLM
1Tracking IDCase IDCase TitleUser IDFull NameEmailInsert DateLast WatchedCoverageVideo Length (hh:mm:ss)Watch Time (hh:mm:ss)DuplicateCalculated End Date
29597313Unmasking Racial Inequality in Healthcare Video29496Jane DoeJane.Doe@noemail.com11/29/2021 14:58:4011/29/2021 14:58:40100.000%01:02:0201:02:0229496~313~29112021~02:00:00 PM11/29/2021 16:00:42
39596313Unmasking Racial Inequality in Healthcare Video29496Jane DoeJane.Doe@noemail.com11/29/2021 15:34:5911/29/2021 15:34:59100.000%01:02:0201:02:0229496~313~29112021~03:00:00 PM
417638313Unmasking Racial Inequality in Healthcare Video29869John MillerJohn.Miller@noemail.com03/28/2022 18:57:1703/28/2022 18:57:1781.547%01:02:0200:50:3629869~313~2832022~06:00:00 PM03/28/2022 19:47:53
517642313Unmasking Racial Inequality in Healthcare Video29869John MillerJohn.Miller@noemail.com03/28/2022 19:06:4803/28/2022 19:06:4814.827%01:02:0200:09:1229869~313~2832022~07:00:00 PM
616544313Unmasking Racial Inequality in Healthcare Video9104Mary ThomasMary.Thomas@noemail.com03/22/2022 17:54:3803/22/2022 17:54:3886.086%01:02:0200:53:259104~313~2232022~05:00:00 PM03/22/2022 18:48:03
716547313Unmasking Racial Inequality in Healthcare Video9104Mary ThomasMary.Thomas@noemail.com03/22/2022 18:04:5303/22/2022 18:04:5314.424%01:02:0200:08:579104~313~2232022~06:00:00 PM
Sheet1
Cell Formulas
RangeFormula
M2,M6,M4M2=G2+K2
L2:L7L2=D2&"~"&B2&"~"&DAY(G2)&MONTH(G2)&YEAR(G2)&"~"&TEXT(TIME(HOUR(G2),0,0),"hh:mm:ss AM/PM")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
M2Cell Value>$H$3textNO
M4Cell Value>$H$5textNO
M6Cell Value>$H$7textNO


I appreciate any help! Thank you so much.
 

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.

Forum statistics

Threads
1,215,692
Messages
6,126,230
Members
449,303
Latest member
grantrob

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