+ Reply to Thread
Results 1 to 3 of 3

VBA macro to check for match

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    VBA macro to check for match

    Hi can anyone help please I have a workbook with a "controls" sheet then the sheets after the controls sheet all have data in them. I need a macro to check if the name in A1 matches exactly with a cell on row 3 on every sheet. Then if any don't match write the sheet and name that doesn't match on the controls sheet in columns A and B

  2. #2
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: VBA macro to check for match

    Hi Roadhouse,

    Here is a program that I think might do what you are asking.

    Sub Controls()
    
    ' Dimension array variables
    Dim shtname() As String
    Dim arrA1() As String
    
    ' Get Number of Columns in sheet
    numcols = Application.Columns.Count
    
    'Verify if Sheet "controls" exists, if not, create it
    On Error Resume Next
    Err.Clear
    Sheets("controls").Activate
    If (Err.Number <> 0) Then
       Sheets.Add before:=Sheets(1)
       ActiveSheet.Name = "controls"
    End If
    On Error GoTo 0
    
    ' Verify "controls" sheet is first in list
    If (Sheets(1).Name <> "controls") Then Sheets("controls").Move before:=Sheets(1)
    
    ' Go through Sheets and check if name in range A1 shows up in Row 3 for each sheet
    ncontrols = 0
    If (Sheets.Count > 1) Then
       For i = 2 To Sheets.Count
          Sheets(i).Activate
          ' Get value of A1
          str1 = Range("A1")
          ' Find Last Colum with Date in row 3
          lastcol = Range(Cells(3, numcols), Cells(3, numcols)).End(xlToLeft).Column
          For j = 1 To lastcol
             If (str1 = Cells(3, j)) Then GoTo 10  'Value of A1 has a match in row 3
          Next j
          ' Add Sheet name and value of A1 to array if A1 has not match in row 3
          ncount = ncount + 1
          ReDim Preserve shtname(1 To ncount) As String
          ReDim Preserve arrA1(1 To ncount) As String
          shtname(ncount) = Sheets(i).Name
          arrA1(ncount) = Range("A1")
    10 'Next i
       Next i
    End If
    
    ' Populate "controls" sheet
    If (ncount > 0) Then
       Sheets("controls").Activate
       Cells.Clear
       For i = 1 To ncount
          Range("A" & i) = shtname(i)
          Range("B" & i) = arrA1(i)
       Next i
       ' Put in Headings
       Rows(1).Insert shift:=xlDown
       Range("A1") = "Sheet Name"
       Range("B1") = "A1 Value"
       Rows(1).Font.Bold = True
       Cells.VerticalAlignment = xlCenter
       Cells.HorizontalAlignment = xlCenter
       Columns("A:B").AutoFit
    End If
    
    End Sub
    Hope it helps,

    Dan

  3. #3
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: VBA macro to check for match

    dan that was brilliant thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to compare check-in vs. check-out data
    By skylark332 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-30-2016, 04:12 PM
  2. [SOLVED] index match with check box
    By burdo77 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-28-2016, 09:10 AM
  3. [SOLVED] Cross Check Columns for Index Match Match
    By Harr in forum Excel Formulas & Functions
    Replies: 25
    Last Post: 12-31-2015, 11:35 AM
  4. [SOLVED] Using Index & Match (or similar) to check two columns for a match
    By dvs in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-30-2015, 07:07 PM
  5. Index Match Match with rank check
    By Jonathan9 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-20-2014, 11:46 AM
  6. Macro to force format and check barcode check digit
    By Code Flunkie in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-02-2009, 10:27 AM
  7. Macro to check for blank cell entry, copy previous value, and check for duplicates
    By xPunxNotDeadx in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-26-2009, 06:33 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1