Need help to change If-then to Array

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
The following code works as desired, but will be SLOW when there are more columns present (the elements are merely examples - my actual worksheets have 40+ columns and can have hundreds of rows).

I feel like this could be an array formula, but I'm just learning about those. Could someone help me?

What it does: checks to assure that the beginning columns in target worksheet match the headers needed - if e.g. the second column doesn't match, the code inserts a column and gives it the header needed.
Code:
Sub CHECK_COLUMNS()
        If UCase(Cells(1, 1).Value) <> "CONCATENATE" Then
            Columns(1).Insert
            Cells(1, 1).Value = "Concatenate"
        End If
        If UCase(Cells(1, 2).Value) <> "REPORT ITEM NO" Then
            Columns(2).Insert
            Cells(1, 2).Value = "Report Item No"
        End If
        If UCase(Cells(1, 3).Value) <> "AREA CODE" Then
            Columns(3).Insert
            Cells(1, 3).Value = "Area Code"
        End If
        If UCase(Cells(1, 4).Value) <> "PHONE START" Then
            Columns(4).Insert
            Cells(1, 4).Value = "Phone Start"
        End If
        If UCase(Cells(1, 5).Value) <> "PHONE END" Then
            Columns(5).Insert
            Cells(1, 5).Value = "Phone End"
        End If
        If UCase(Cells(1, 6).Value) <> "NAME" Then
            Columns(6).Insert
            Cells(1, 6).Value = "Name"
        End If
End Sub
Referencing this:
http://www.mrexcel.com/forum/excel-...atch-array-report-insert-missing-columns.html
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Would it be better to go the other way? Start with the full column headers and then paste the information where it belongs?
 
Upvote 0
When the headers are missing, there is no data there to paste. That's ok, but I need the columns themselves to exist (even if blank) in order to run other code on the sheet.
 
Upvote 0
Anybody??? I've gotten this far (see below code), but I have NO IDEA how to actually compare the two arrays.
I think the code could react in one of two ways:
Option 1: Header I not found. Add value to "missing" temporary array. Go through the rest and if e.g. 3 headers missing, add 3 columns. Then re-compare to ArrayA to re-order the columns correctly.
Option 2: Header I not found. Insert column with Header I at that time. Look for next I.

Here's how far I got:
Code:
Sub checkarray()
Dim ArrayA As Variant
Dim L As Long
Dim I As Integer


ArrayA = Array("Concatenate", "Area Code", "Phone Start", "Phone End", "Name", "Reqd Qty")
Dim ReqdCol As Integer
    
    Cells.Find(What:="Reqd Qty", LookIn:=xlFormulas, LookAt:=xlWhole).Select
    ReqdCol = Columns(ActiveCell.Address)
    
'////////Dim Array1(0, ReqdCol - 1)
Dim Array1 As Variant
Array1 = Range(Cells(2, 1), Cells(2, ReqdCol)).Value   '//rng to be examined=A2:ReqdQty_cell
For I = 1 To UBound(Array1, 2) 'columns
        '///**Add code here to compare Array1 to ArrayA
        '///**code to insert column when an "I" is not found
Next I


End Sub
:confused:
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,268
Members
448,558
Latest member
aivin

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