If value not in column excel vba

Just to modify scott’s answer to make it a function:

Function FindFirstInRange(FindString As String, RngIn As Range, Optional UseCase As Boolean = True, Optional UseWhole As Boolean = True) As Variant

    Dim LookAtWhat As Integer

    If UseWhole Then LookAtWhat = xlWhole Else LookAtWhat = xlPart

    With RngIn
        Set FindFirstInRange = .Find(What:=FindString, _
                                     After:=.Cells(.Cells.Count), _
                                     LookIn:=xlValues, _
                                     LookAt:=LookAtWhat, _
                                     SearchOrder:=xlByRows, _
                                     SearchDirection:=xlNext, _
                                     MatchCase:=UseCase)

        If FindFirstInRange Is Nothing Then FindFirstInRange = False

    End With

End Function

This returns FALSE if the value isn’t found, and if it’s found, it returns the range.

You can optionally tell it to be case-sensitive, and/or to allow partial-word matches.

I took out the TRIM because you can add that beforehand if you want to.

An example:

MsgBox FindFirstInRange(StringToFind, Range("2:2"), TRUE, FALSE).Address

That does a case-sensitive, partial-word search on the 2nd row and displays a box with the address. The following is the same search, but a whole-word search that is not case-sensitive:

MsgBox FindFirstInRange(StringToFind, Range("2:2")).Address

You can easily tweak this function to your liking or change it from a Variant to to a boolean, or whatever, to speed it up a little.

Do note that VBA’s Find is sometimes slower than other methods like brute-force looping or Match, so don’t assume that it’s the fastest just because it’s native to VBA. It’s more complicated and flexible, which also can make it not always as efficient. And it has some funny quirks to look out for, like the «Object variable or with block variable not set» error.

I went a different direction…
Problem you had was the i in your code was the value of the cell, so when you put it into the rngReconcile.Cells(i, 1).Value call, you were putting in the text instead of the row number.

Next problem you were going to hit was that you were only checking each row for row… I believe you need a solution to check each value in column A with each value in column K.

I think I cleaned it up enough for you to use… I did reduce it just to use one sheet to get it working, so if this is what you want, I will let you put it back to multi workbooks… Hopefully you can follow it.

Dim last_cell As Integer
rngWatch_last_cell = Sheets(1).Range("A65536").End(xlUp).Row
rngReconcile_last_cell = Sheets(1).Range("K65536").End(xlUp).Row


Dim rngWatch_var As Integer, rngReconcile_var As Integer
rngWatch_var = 1
rngReconcile_var = 1

Dim i As Variant
Dim found As Integer

For rngWatch_var = 1 To rngWatch_last_cell
    Dim c As Variant
    c = rngWatch.Cells(rngWatch_var, 1).Value
    found = 0
    For rngReconcile_var = 1 To rngReconcile_last_cell
        If c = rngReconcile.Cells(rngReconcile_var, 1).Value Then
           found = 1
        End If
    Next rngReconcile_var

    If found = 0 Then
        MsgBox c & " not found in list"
        ' add to another column
    End If

Next rngWatch_var

  • #1

hi,
i’m new to vba. i kept searching on how to do something that seems so simple.
any help on how to start would be great!
so, i have an user enter textbox C and an user enter textbox numeric value in D.
All i want the code to do is to look in column A for that C date, if the date exist exit, if not then add that date in column A and add the D value to the cell next to the matching A date.
is that at all possible?

Excel Facts

Round to nearest half hour?

Use =MROUND(A2,»0:30″) to round to nearest half hour. Use =CEILING(A2,»0:30″) to round to next half hour.

AlphaFrog

AlphaFrog

MrExcel MVP

Joined
Sep 2, 2009
Messages
16,465


  • #2

Try something like this…

Code:

[color=darkblue]Sub[/color] Add_Date_and_Value()


    [color=darkblue]Dim[/color] d [color=darkblue]As[/color] Date
    [color=darkblue]Dim[/color] num [color=darkblue]As[/color] [color=darkblue]Double[/color]
    
    [color=green]'Prompt for a date[/color]
    d = Application.InputBox("Enter a date.", "Date Entry", Format(Date, "mm/dd/yyyy"), Type:=1)
    [color=darkblue]If[/color] d = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'User canceled[/color]
    
    [color=green]'Test if the date exists in column A[/color]
    [color=darkblue]If[/color] WorksheetFunction.CountIf(Range("A:A"), d) [color=darkblue]Then[/color]
        MsgBox "The date already exists in column A. ", , "Date Exists"
    [color=darkblue]Else[/color]
        [color=green]'Date doesn't exist[/color]
        [color=green]' Prompt for a number[/color]
        num = Application.InputBox("Enter a value.", "Value Entry", Type:=1)
        [color=darkblue]If[/color] num = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'User canceled[/color]
        [color=darkblue]With[/color] Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Format(d, "mm/dd/yyyy")    [color=green]'Put date in the next empty row in column A[/color]
            .Offset(, 1).Value = num            [color=green]'Put the value in column B[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]


End [color=darkblue]Sub[/color]

  • #3

Sorry for the late reply… but thank you so much!

Similar threads

EXPLANATION

This tutorial shows how to test if a range does not contain a specific value and return a specified value if the formula tests true or false, by using an Excel formula and VBA.

This tutorial provides one Excel method that can be applied to test if a range does not contain a specific value and return a specified value by using an Excel IF and COUNTIF functions. In this example, if the Excel COUNTIF function returns a value of 0, meaning the range does not have cells with a value of 505, the test is TRUE and the formula will return a «Not in Range» value. Alternatively, if the Excel COUNTIF function returns a value of greater than 0, meaning the range has cells with a value of 505, the test is FALSE and the formula will return a «In Range» value.

This tutorial provides one VBA method that can be applied to test if a range does not contain a specific value and return a specified value and return a specified value.

FORMULA
=IF(COUNTIF(range, value)=0, value_if_true, value_if_false)

ARGUMENTS
range: The range of cells you want to count from.
value: he value that is used to determine which of the cells should be counted, from a specified range, if the cells’ value is equal to this value.
value_if_true: Value to be returned if the range does not contains the specific value.
value_if_false: Value to be returned if the range contains the specific value.

try this:

If Application.WorksheetFunction.CountIf(RangeToSearchIn, ValueToSearchFor) = 0 Then
Debug.Print "none"
End If

If you want to do this without VBA, you can use a combination of IF, ISERROR, and MATCH.

So if all values are in column A, enter this formula in column B:

=IF(ISERROR(MATCH(12345,A:A,0)),"Not Found","Value found on row " & MATCH(12345,A:A,0))

This will look for the value «12345» (which can also be a cell reference). If the value isn’t found, MATCH returns «#N/A» and ISERROR tries to catch that.

If you want to use VBA, the quickest way is to use a FOR loop:

Sub FindMatchingValue()
    Dim i as Integer, intValueToFind as integer
    intValueToFind = 12345
    For i = 1 to 500    ' Revise the 500 to include all of your values
        If Cells(i,1).Value = intValueToFind then 
            MsgBox("Found value on row " & i)
            Exit Sub
        End If
    Next i

    ' This MsgBox will only show if the loop completes with no success
    MsgBox("Value not found in the range!")  
End Sub

You can use Worksheet Functions in VBA, but they’re picky and sometimes throw nonsensical errors. The FOR loop is pretty foolproof.

The find method of a range is faster than using a for loop to loop through all the cells manually.

here is an example of using the find method in vba

Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
    With Sheets("Sheet1").Range("A:A") 'searches all of column A
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then
            Application.Goto Rng, True 'value found
        Else
            MsgBox "Nothing found" 'value not found
        End If
    End With
End If
End Sub

Simplest is to use Match

If Not IsError(Application.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
    ' String is in range

Like this post? Please share to your friends:
  • If value not equal excel
  • If value like excel
  • If value is not zero excel
  • If value is not in range excel vba
  • If trust is just a word