Consulting

Results 1 to 8 of 8

Thread: Solved: Check contents of cells in header row

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Solved: Check contents of cells in header row

    I have a macro that resizes/reformats the columns in a spreadsheet, but I'd like to add a "check" to the process to make sure that the columns are where I think they are before I do that.

    I've got the following test code, but get an error. What am I doing wrong?

    [vba]' Verify that headers are in the expected columns
    Dim aryVals
    Dim i As Long
    aryVals = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M")
    For i = 0 To 12
    If LCase(Cells(1, i).Value) <> LCase(aryVals(i)) Then
    MsgBox "Headers do not appear as expected."
    Exit Sub
    End If
    Next i

    [/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can't use Cells(1, i).Value if i = 0
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    So for this type of array, the first value is not zero? I changed the code to "For i = 1 to 13". I don't get an error now, but I do get the message stating the the headers are not as expected. How to I match up the loop with the correct columns?

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Cheryl
    Arrays start at 0, Rows/Colums start at 1. i+1 might be a way to compare values???
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location

    Maybe check to see is a header is missing and...

    the msgbox could tell you which one too.
    [vba]
    Sub dude()
    ' Verify that headers are in the expected columns
    Dim aryVals
    Dim i As Long
    aryVals = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M")
    For i = LBound(aryVals) To UBound(aryVals)
    If Len(Cells(1, i + 1).Value) = 0 Then
    MsgBox "Headers do not appear as expected in >" & _
    " " & Cells(1, i + 1).Address
    Exit Sub
    End If
    Next i

    End Sub
    [/vba]

  6. #6
    if you type Option Base 1 in the declaration section arrays start at 1 not 0. then your program will work.

  7. #7
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    I still can't get it to work. Can someone attach a sample file that checks the header values?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try this.
    [VBA]
    Sub Test()
    ' Verify that headers are in the expected columns
    Dim aryVals
    Dim i As Long
    aryVals = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M")
    For i = 0 To 12
    If LCase(Cells(1, i + 1).Value) <> LCase(aryVals(i)) Then
    MsgBox "Headers do not appear as expected." & _
    vbCr & UCase(Cells(1, i + 1).Value) & ":" & UCase(aryVals(i))
    Exit Sub
    End If
    Next i
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •