Check presence of values in a column/range using Excel VBA
Most often a programmer would be given a job of Insert/Update scenarion in EXcel. That is, Insert a new row if a specific value does not exist; if it does then update some. So the process is to check for existence of a specific value
Here is a generic function ;
Sub CheckForExistence()
myVal = "Sample"
myRange = "A:A"
If Check_Val_Existence(myVal, myRange) = True Then
MsgBox "Value exists"
End If
End Sub
This used the Find Method. This method finds specific information in a range, and returns a Range object that represents the first cell where that information is found. Returns Nothing if no match is found.
Function Check_Val_Existence(ByVal sText, ByVal sRange) As Boolean
Dim rFnd As Range
Dim sText As String
Set rFnd = ActiveSheet.Range(sRange).Find(What:=sText, LookAt:=xlPart)
If Not rFnd Is Nothing Then
Check_Val_Existence = True
Else
Check_Val_Existence = False
End If
End Function
Sunday, August 12, 2007
Subscribe to:
Post Comments (Atom)
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.
5 comments:
Code doesn't work, Stext is declared twice
Thanks Pavel
It should have been
Function Check_Val_Existence(ByVal sText, ByVal sRange) As Boolean
Dim rFnd As Range
Set rFnd = ActiveSheet.Range(sRange).Find(What:=sText, LookAt:=xlPart)
If Not rFnd Is Nothing Then
Check_Val_Existence = True
Else
Check_Val_Existence = False
End If
End Function
Hi,
I need a help in Excel. I have created an excel file with 3 input boxes. That means, once I press Button an input box will pop-up asking me to enter the value, and the value I enter will be entered in Column 'B' 'C' and 'D' with respective input boxes. Now I want that before entering the value in respective column, macro should search if there is a duplicate value in that column or not. If any exist it should prompt that a duplicate value already exist in the column.
Could you please help me with this.
Looking forward to hear from you.
Thanks
Puneet Jha
Hi Pavel and Shasur,
Thanks a lot for the help. But still my problem is not solved. For your kind reference I am copying below the code I am using.
It may not be the correct code as I am at a very initial level of learning VB.
Public Sub Submit_Click()
Range("C:C").Find(What:="", LookAt:=xlWhole).Select
ActiveCell.Value = InputBox("Please enter the word you want to enter in column C")
Range("C1:C65536").FindNext
Range("D:D").Find(What:="", LookAt:=xlWhole).Select
Range("D:D").Find(What:="", LookAt:=xlWhole).Select
ActiveCell.Value = InputBox("Please enter the word you want to enter in column D")
Range("D1:D65536").FindNext
Range("E:E").Find(What:="", LookAt:=xlWhole).Select
Range("E:E").Find(What:="", LookAt:=xlWhole).Select
ActiveCell.Value = InputBox("Please enter the word you want to enter in column E")
Range("E1:E65536").FindNext
ActiveWorkbook.Save
End Sub
Now actually I want, after user enters a word he/she wants to enter in column C, D and E, the code should search in the column whether the word entered by user already exist in the column or not. If the word exist, code
should give a message that "The word you want to enter already exist in "" column" after showing this message the code should take user to the cell and select the cell were the word entered by user already exist. And if the word does not exist code should take user to the first empty cell in the column and allow user to enter word in the cell.
Can you please help me with this.
Looking forward to your response.
Warm Regards
Puneet Jha
Hi Punjeet!
There is a MUCH simpler way to acheive what you're doing! Here's a neat little trick to do this:-
It's easier to use column NUMBERS instead of LETTERS. The best way to do this, is to reference them as "Cells" instead of as "Range" coordinates. (meaning, instead of writing:
' Range("C1").Value
you simply write:
' Cells(3, 1).Value
or even better, you can replace these numbers with variables:
' Cells(ColNum, RowNum).Value
Now, the code in your Submit_Click() routine will be much simpler, because the bulk of it will be done in one simple sub-routine, that can be reused witout having to re-write it! And here it is:
Sub AddNonDuplicate(ByVal ColNum
As Integer, ByVal MyValue
As String)
Dim RowNum As Integer
RowNum = 1 'Starting in the
'first row Do
If Cells(CelNum, RowNum).Value = MyValue Then '(If the value of any row in your column is the same as the one you are trying to add, it is a duplicate, and should say so and exit:)MsgBox MyValue & " already exists in this column!"
Exit Sub
End If
RowNum = RowNum + 1 'check the next row down Loop Until Cells(ColNum, RowNum).Value = "" 'stop when you find the first blank
'Now it has checked every non-blank cell, and hasn't found a duplicate, so if it has got this far, it can be added to the blank cell it stopped on: Cells(ColNum, RowNum).Value = MyValue
End Sub 'Now here's how simple your Submit_Click() routine becomes: Public Sub Submit_Click()
Dim InputValue As String 'Variable to hold input value
InputValue = InputBox("Please enter the word you want to enter in column C")
Call AddNonDuplicate(3, InputValue) 'Column C is actually column 3 InputValue = InputBox("Please enter the word you want to enter in column D")
Call AddNonDuplicate(4, InputValue) 'Column D is actually column 4 InputValue = InputBox("Please enter the word you want to enter in column E")
Call AddNonDuplicate(5, InputValue) 'Column E is actually column 5 ActiveWorkbook.Save
End Sub
Hope this helps!
-Belial9. ;)
Post a Comment