Validate if a Prefix in sheet2 has a matching value in sheet1

vinmeg

I have the following issue: In one workbook I have multiple sheets.

On Sheet 2 in column "D" starting on row 2, Is a list of 300+ prefixes of 4 digits long e.g. XFTZ, GHTU, ZAQS etc.

On Sheet 1 in column "R" starting on row 3, Is a list of 1000+ values that can have the following values e.g.: AAAA1234556 and ZAQS12565865. The first value AAAA...... is allowed, where the second value ZAQS..... Should throw an error message when running the VBA code.

The list of values in both sheets can grow over time, so I would like to avoid hard coding of records. I would expect best solution here is to use something like this:

LastRowNr = Cells(Rows.Count, 1).End(xlUp).Row
CMArg

Try something like the following, replacing Sheet1 with the name in which the actual data is located

Option Explicit

Private Sub searchPrefix()
    Dim RangeInArray() As Variant
    Dim LastRow1 As Long
    Dim LastRow2 As Long
    Dim tmpSrch As String
    Dim i As Long

    LastRow1 = Worksheets("Sheet1").Cells(Rows.Count, 18).End(xlUp).Row
    LastRow2 = Worksheets("PREFIXES").Cells(Rows.Count, 4).End(xlUp).Row
    RangeInArray = Application.Transpose(Worksheets("PREFIXES").Range("D1:D" & LastRow2).Value)

    For i = 3 To LastRow1
        If Len(Worksheets("Sheet1").Cells(i, 18).Value) >= 3 Then
            tmpSrch = Left(Worksheets("Sheet1").Cells(i, 18).Value, 4) '18: column R
            If IsInArray(tmpSrch, RangeInArray) Then
                Worksheets("Sheet1").Cells(i, 18).Interior.ColorIndex = xlNone
                Worksheets("Sheet1").Cells(i, 18).Font.ColorIndex = 0
                Worksheets("Sheet1").Cells(i, 18).Font.Bold = False
            Else
                Worksheets("Sheet1").Cells(i, 18).Interior.Color = RGB(252, 134, 75)
                Worksheets("Sheet1").Cells(i, 18).Font.Color = RGB(181, 24, 7)
                Worksheets("Sheet1").Cells(i, 18).Font.Bold = True
            End If
        End If
    Next
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel: how to sum values in sheet2 based on whether a value in sheet1 matches value in sheet2

From Dev

excel compare current time value from sheet1 to time value range from sheet2

From Dev

How to iterate through rows in sheet1 given cell value in sheet2 and replace row in sheet1 with row in sheet 2?

From Dev

Excel: Trying to consolidate weighted average data from Sheet1 and summarize to Sheet2 when Sheet 1 has variable number of rows

From Dev

How to run a macro in sheet1 from sheet2

From Dev

Getting the last fillled column in a sheet1 and copying the data in sheet2 to sheet1

From Dev

Copying cells from sheet1 to sheet2 if it does not exist in sheet2

From Dev

Run A Macro on Each Sheet Except Sheet1 and Sheet2

From Dev

I need to match Sheet1 Column A to Sheet2 Column A if they match then copy Sheet2 Column B to Sheet1 Column I

From Dev

Concatenate 2 cells from Sheet1 to 1 cell Sheet2

From Dev

Copy rows to sheet2 in value in range is met

From Dev

Copying data based on the headers from Sheet1 to Sheet2

From Dev

Refer data from sheet2 to sheet1 dynamically based on date

From Dev

Create columns in sheet2 depending on number of rows in sheet1 in excel

From Dev

Copying columns in Excel from sheet1 to sheet2 without having to cut and paste

From Dev

VBA - Check a range in sheet1 for a variable and create a list on sheet2 of each occurence of the variable

From Dev

Copying active row of Sheet1 to Sheet2 based on cell condition & avoid duplicates

From Dev

search all rows and columns in sheet1 for string, copy entire row to sheet2 if found

From Dev

Excel Online - Replicate date from sheet1 columnA to sheet2 columnB

From Dev

How to reflect sheet1's those rows only which contains positive values, into sheet2?

From Dev

Copying data based on the headers from Sheet1 to Sheet2

From Dev

Copy Row if Sheet1 A contains part of Sheet2 C

From Dev

Excel VBA-Find string in sheet2 and copy the this in sheet1

From Dev

Automatically Copy cells from Sheet2 to Custom column on Sheet1 with rule

From Dev

How to update existing data from Sheet1 to Sheet2 using Macro?

From Dev

Excel, find the row for {max(sum(Sheet1(A)+Sheet2(A)+Sheet3(A))} formula without having the sum in any table

From Dev

MS Excel partial match sheet1!A1 with sheet2!A1 return sheet2!B1

From Dev

How To copy data from a range in sheet1 to sheet2 5 times until I reach an empty cell?

From Dev

ORDERING columns in sheet2 based on a column in sheet 1 - excel

Related Related

  1. 1

    Excel: how to sum values in sheet2 based on whether a value in sheet1 matches value in sheet2

  2. 2

    excel compare current time value from sheet1 to time value range from sheet2

  3. 3

    How to iterate through rows in sheet1 given cell value in sheet2 and replace row in sheet1 with row in sheet 2?

  4. 4

    Excel: Trying to consolidate weighted average data from Sheet1 and summarize to Sheet2 when Sheet 1 has variable number of rows

  5. 5

    How to run a macro in sheet1 from sheet2

  6. 6

    Getting the last fillled column in a sheet1 and copying the data in sheet2 to sheet1

  7. 7

    Copying cells from sheet1 to sheet2 if it does not exist in sheet2

  8. 8

    Run A Macro on Each Sheet Except Sheet1 and Sheet2

  9. 9

    I need to match Sheet1 Column A to Sheet2 Column A if they match then copy Sheet2 Column B to Sheet1 Column I

  10. 10

    Concatenate 2 cells from Sheet1 to 1 cell Sheet2

  11. 11

    Copy rows to sheet2 in value in range is met

  12. 12

    Copying data based on the headers from Sheet1 to Sheet2

  13. 13

    Refer data from sheet2 to sheet1 dynamically based on date

  14. 14

    Create columns in sheet2 depending on number of rows in sheet1 in excel

  15. 15

    Copying columns in Excel from sheet1 to sheet2 without having to cut and paste

  16. 16

    VBA - Check a range in sheet1 for a variable and create a list on sheet2 of each occurence of the variable

  17. 17

    Copying active row of Sheet1 to Sheet2 based on cell condition & avoid duplicates

  18. 18

    search all rows and columns in sheet1 for string, copy entire row to sheet2 if found

  19. 19

    Excel Online - Replicate date from sheet1 columnA to sheet2 columnB

  20. 20

    How to reflect sheet1's those rows only which contains positive values, into sheet2?

  21. 21

    Copying data based on the headers from Sheet1 to Sheet2

  22. 22

    Copy Row if Sheet1 A contains part of Sheet2 C

  23. 23

    Excel VBA-Find string in sheet2 and copy the this in sheet1

  24. 24

    Automatically Copy cells from Sheet2 to Custom column on Sheet1 with rule

  25. 25

    How to update existing data from Sheet1 to Sheet2 using Macro?

  26. 26

    Excel, find the row for {max(sum(Sheet1(A)+Sheet2(A)+Sheet3(A))} formula without having the sum in any table

  27. 27

    MS Excel partial match sheet1!A1 with sheet2!A1 return sheet2!B1

  28. 28

    How To copy data from a range in sheet1 to sheet2 5 times until I reach an empty cell?

  29. 29

    ORDERING columns in sheet2 based on a column in sheet 1 - excel

HotTag

Archive