Convert to array functionality

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. 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!

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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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