Search code examples
vbaexcelsearchuser-input

Excel VBA - Loop search for value then color row


This macro is supposed to find the value NULL in column "W" and paint the row it found NULL on in a color. It does that fine however if I try to search for a number in the same column(that i know exists there) it doesn't seem to find the value. Any help would be appreciated.

Sub e()

MsgBox "some question?", , "Marvin The Paranoid Android"

Dim fNameAndPath As Variant, wb As Workbook
Dim LastRow As Long, i As Long, sht As Worksheet
Dim myValue As Variant

fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLSX", Title:="Èçáåðåòå ôàéë ñ èìå /Ðåâîëâèíãè/")
If fNameAndPath = False Then Exit Sub

Set wb = Workbooks.Open(fNameAndPath)
Set sht = wb.Worksheets("Sheet1")
X = wb.Name

Windows(X).Activate

'Macro optimization
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
'000000000
ActiveWindow.Zoom = 85
Rows("1:1").Select
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Range("A1:W1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Range("A:E,L:N").EntireColumn.AutoFit
Columns("F:F").ColumnWidth = 6.14
Columns("G:G").ColumnWidth = 6.43
Columns("H:K").ColumnWidth = 5.43
Range("O:R,T:V").ColumnWidth = 4.71
Columns("S:S").ColumnWidth = 14.71
Rows("1:1").RowHeight = 54.54
Range("A1").Select

myValue = InputBox("Give me some input")

LastRow = sht.Cells(sht.Rows.Count, "W").End(xlUp).row

For r = 1 To LastRow
    If Cells(r, Columns("W").Column).Value = myValue Then
        Rows(r).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
Next r

Range("A1").Select
Application.Calculation = xlCalculationAutomatic

wb.Close SaveChanges:=True 'or false

'Reverse macro optimization
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True

MsgBox "Îáðàáîòèõ ôàéë /Ðåâîëâèíãè/...", , "Marvin The Paranoid Android"

End Sub

Solution

  • Autofilter() method of Range object can detect "23" both as number and a text:

    With sht
        With .Range("W1", .Cells(.Rows.Count, "W").End(xlUp)) '<--| consider column "W" values down to its last non empty row
            .AutoFilter field:=1, Criteria1:=myValue '<--| filter column "W" on 'myValue'
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any values match...
                With .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Interior '<--|... consider only filtered values, and apply formatting
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 65535
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End If
        End With
        .ShowAllData '<--| show all rows back...
    End With