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
Change:
VBA Code:
    .NumberFormat = "@"

to:
VBA Code:
    .Columns("A:A").NumberFormat = "@"
 
Upvote 0
Change:
VBA Code:
    .NumberFormat = "@"

to:
VBA Code:
    .Columns("A:A").NumberFormat = "@"

Thank you and while that works, it only does so if the id number is always in A.

I am looking to see if there is a more general way to address this so when another layout of the report is supplied, it can be ran with the ID number in D, for example and Date in Column A. In my approach, the entire dataset would be changed to a Text format except if the cell contains a "/" or is set as date. There might even be times where a report has an order date and a sold date and could have delivered date among a few others. So looking to have this as a swiss army knife of sorts.
 
Upvote 0
Is the employee badge column always going to be labeled 'Employee Badge' ?
 
Upvote 0
it won't unfortunately. Depending on the system the report generates from it could be Employee ID or it could also be from our accounting system that for some god awful reason, lets the user name the field so not only could it be different but also misspelled. This is why i was trying to change all with an exception instead. Same with the date field. it can be named something else but the "/" will always be present.
 
Upvote 0
How about replacing:
VBA Code:
With r
    .NumberFormat = "@"
    .Value = .Formula
End With

with:
VBA Code:
Dim cel As Range
    For Each cel In r
        If Not IsDate(cel.Value) Then
            cel.NumberFormat = "@"
            cel.Value = cel.Formula
        End If
    Next
 
Upvote 0
That's it. So the line is looking for anything that isn't a date rather than looking for what is a date. ok that works well actually. thank you.
 
Upvote 0
Correct. If a cell is not considered a date then you want to make it a text cell.

Glad to help.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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