Help with format tweaking of existing code (date and text)

Serafin54

Board Regular
Joined
Apr 11, 2014
Messages
155
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a code that will look at the current sheet and then combine amounts found under identical headers.

While it works really well, I need to tweak it due to recently discovered issues. For instance, the employee badge might contain leading zeros and when the code runs, the zeros drop. I added in a code at the front end to update the sheet to text but when i do that, the dates obviously also follow and become serial numbers. I have tried to tweak the " Cells(1, 1 + col).Resize(rc, 1).Value = SD.items()(col)" but anything i try causes the code to clear all cells.

Basically, what i'm trying to accomplish is to:
1. Update all cells to text unless there is a "/" or is already formatted as date (whichever is easiest to code)
2. Auto size all columns so that i don't get a scientific reference. Not that i have, but more as precaution.

Thanks for looking.


Before:
Employee BadgeEmployee NameDateApplesOrangesBananasApplesPlums
0012Jon Smith01/01/20221034885090
0014Betty White01/02/2022630763357
1234Axel Foley01/03/202271170612
765Nigel Tufnell01/04/2022625255781
00001Hans Gruber01/05/2022687689479


After:
Employee BadgeEmployee NameDateApplesOrangesBananasPlums
0012Jon Smith4456260348890
0014Betty White4456339307657
1234Axel Foley445646811702
765Nigel Tufnell4456563252581
00001Hans Gruber44566100876879


VBA Code:
Sub Combine_Duplicate_Headers_formatting()

'Sums amounts under duplicate headers
Dim r As Range:         Set r = Range("A1").CurrentRegion
Dim rc As Integer:      rc = r.Rows.Count
Dim AR() As Variant:    AR = r.Value2
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim v As Variant

'changes formats to text prior to combine so that leading zeros aren't dropped if left as general format
With r
    .NumberFormat = "@"
    .Value = .Formula
End With

For i = 1 To UBound(AR, 2)
    If Not SD.exists(AR(1, i)) Then
        SD.Add AR(1, i), Application.Index(AR, 0, i)
    Else
        v = SD(AR(1, i))
        For j = 2 To UBound(v)
            v(j, 1) = v(j, 1) + AR(j, i)
        Next j
        SD(AR(1, i)) = v
    End If
Next i

r.ClearContents

For col = 0 To SD.Count - 1
    Cells(1, 1 + col).Resize(rc, 1).Value = SD.items()(col)
Next col
  For rc = 1 To ActiveSheet.UsedRange.Columns.Count
  
Next
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this for now:

VBA Code:
Sub Combine_Duplicate_Headers_formatting()

'Sums amounts under duplicate headers
Dim r As Range:         Set r = Range("A1").CurrentRegion
Dim rc As Integer:      rc = r.Rows.Count
Dim AR() As Variant:    AR = r.Value2
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim v As Variant
Dim cel As Range

    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    
'changes formats to text prior to combine so that leading zeros aren't dropped if left as general format

    For Each cel In r
        If Not IsDate(cel.Value) Then
            cel.NumberFormat = "@"
            cel.Value = cel.Formula
        End If
    Next


For i = 1 To UBound(AR, 2)
    If Not SD.exists(AR(1, i)) Then
        SD.Add AR(1, i), Application.Index(AR, 0, i)
    Else
        v = SD(AR(1, i))
        For j = 2 To UBound(v)
            v(j, 1) = v(j, 1) + AR(j, i)
        Next j
        SD(AR(1, i)) = v
    End If
Next i

r.ClearContents

For col = 0 To SD.Count - 1
    Cells(1, 1 + col).Resize(rc, 1).Value = SD.items()(col)
Next col
  For rc = 1 To ActiveSheet.UsedRange.Columns.Count
  
Next
    
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True

End Sub

Let us know how long that takes.
 
Upvote 0
Solution
ok this is much faster. I just tested on a 5000 row and took about 10 seconds which considering all that is going on, is a keeper. Thank you! I did the screen updating but didn't think to do the Auto Calc toggle.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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