Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
The code below allows me to format a column based on the header value. Because my datasets are so large, performing this cell by cell is not feasible. Could someone assist in "converting" this code for use in an array?
I was going to change it to Function, passing the array I've already read in.
Thanks, y'all!
I was going to change it to Function, passing the array I've already read in.
Thanks, y'all!
Code:
Sub FormatByHeader(Optional ByVal rngUsed As Range)
' ~~ This sub formats worksheet for SSN, dates, and rank
' ~~ Apply custom [number] format based on column header
' http://www.vbaexpress.com/forum/showthread.php?25740-Solved-Excel-2007-Apply-custom-number-format-based-on-column-header
Dim cell As Range, _
cellx As Range, _
rngX As Range, _
row As Range
Dim strFormat As String, _
strDtFormat As String, _
Set wbk = ActiveWorkbook
strDtFormat = "mm/dd/yyyy"
With ActiveSheet
.UsedRange ' ~ Reset used range
Set rngX = Range("A1:" & Cells(1, rngUsed.Columns.Count).address) ' ~~ Set for range of header row | cells function gives the last cell in row 1
[[COLOR="#0000FF"][B]substitute arrHeader for rngX[/B][/COLOR]]
Set arrHeader = Application.Index(arr, 1, 0) ' ~~ Define array Header || https://usefulgyaan.wordpress.com/2013/06/12/vba-trick-of-the-week-slicing-an-array-without-loop-application-index/
For Each cell In rngX
With cell
Select Case True
Case .value Like "SSN"
For Each cellx In Intersect(cell.EntireColumn, rngUsed).Offset(1, 0).Resize(rngUsed.Rows.Count - 1) ' ~~ confines column of corresponding header
cellx = WorksheetFunction.Substitute(cellx, "-", vbNullString) ' remove dashes
If cellx = vbNullString Or cellx.value = "xxxxxxxxx" Then
cellx.value = intPseudoSSN ' ~~ assign pseudo SSN for any blank SSN
cellx.NumberFormat = "@" ' Format as text -- REQUIRED!!
cellx = Application.Text(cellx, "000000000") ' format as text instead of number but retain leading zeros (DESIRED)
intPseudoSSN = intPseudoSSN + 1
Else
cellx.NumberFormat = "@"
If Len(cellx.value) <> 9 Then cellx.Interior.ColorIndex = 41 ' ~~ test if there SSN with more or less than 9 digits
If IsLetter(cellx.value) = True Then cellx.font.Color = 26 ' ~~ test if there are letters in SSN column
cellx = Application.Text(cellx, "000000000")
End If
Next cellx