Screen out values which didn't fit two conditions - vba

i wanna write a VBA programme that screen out values which didnt satisfy the specified values. However, I kinda stuck in the object-defined error of line 4 (If...Then). Would somebody pls help me out. Many thanks!!!!!!!!
Sub Macro1()
If Cells(A, 1) <> "none" Or Cells(A, 1) <> 0 Then
Cells(A, 2) = "checked"
Else
Cells(A, 2) = "Not checked"
End If
End Sub

You have your rows and columns reversed. Try:
Sub Macro1()
If Cells(1, "A") <> "none" Or Cells(1, "A") <> 0 Then
Cells(2, "A") = "checked"
Else
Cells(2, "A") = "Not checked"
End If
End Sub

A cell is referenced by a row and column or address. So use
Cells(1,1)
or
Cells("A1")

Use this:
Sub Macro1()
If Cells(1, 1) <> "none" Or Cells(1, 1) <> 0 Then
Cells(1, 2) = "checked"
Else
Cells(1, 2) = "Not checked"
End If
End Sub
Or you this:
Sub Macro2()
If Range("A1") <> "none" Or Range("A1") <> 0 Then
Range("B1") = "checked"
Else
Range("B1") = "Not checked"
End If
End Sub

You need to be careful to:
0) respect the language's syntax/semantics: is kind of obvious for a human, but what would mean A for VBA?
1) be sure what cell values are you accessing: i.e. which worksheet those cells belong to;
2) ynot compare apples with oranges: if something is a string, comparing it with an integer value gives you type mismatch errors. You need to be sure before comparing what type are you comparing.
3) follow the rules of Boolean logic, not the the ones common language logic: to say that something shouldn't be "none" or 0.0 doesn't translate logically into your If condition.
So, this is a proposal for a more robust macro:
Public Sub Macro1()
Dim in_value As Variant
Dim out_value As String
' Adjust the name of the worksheet to your needs'
With Worksheets("Sheet1")
' Read data '
in_value = .Range("A1").Value
' Check what type '
Select Case TypeName(in_value)
Case "Empty"
Let out_value = "Not checked"
Case "String"
If LCase(in_value) <> "none" Then
Let out_value = "checked"
Else
Let out_value = "Not checked"
End If
Case "Integer", "Long", "Single", "Double"
If in_value <> 0 Then
Let out_value = "checked"
Else
Let out_value = "Not checked"
End If
Case Else
Let out_value = "checked"
End Select
' Write data '
Let .Range("A2").Value = out_value
End With
End Sub

Related

VBA excel, if Cells(counter,1)="text" then

I'm trying to execute an if statement that only activates if a certain cell contains a specific text. The cell in question needs to be dynamically altered based on a integer that will change, so far ive tried multiple methods but nothing seems to work.
If Cells(counter, 1).text = "text" then
If Cells(counter, 1).value = "text" then
If Range(Cells(counter, 1)).text = "text then
If Range(Cells(counter, 1)).value = "text then
This seems like a simple procedure, does someone have a solution?
Thanks, Sporre
Edit:
Private Sub CheckBox_Change()
If CheckBox.Value = True Then
'do stuff
End If
ElseIf CheckBox.Value = False Then
If Cells(1, counter).Value = "text1" Or Cells(1, counter).Value =
"text2" Or Cells(1, counter).Value = "text3" Then
'do stuff
End If
End If
End Sub
This is where i get the error message "Application-definded or Object-defined error".
Edit 2:
The problem was I tried to call for the counter in several different subs and it was not a public integer. Thanks for your help!
This one will work
if (Trim(ThisWorkbook.Worksheets("Sheet1").Cells(counter, 1).Value)="text") Then
You cannot use End If and then follow up with and ElseIf. The first one ends the If statement entirely, meaning that the you would have to begin a new one. Based on you edit, I think your code should look something like this:
Private Sub CheckBox_Change()
If CheckBox.Value = True Then
'do stuff
ElseIf CheckBox.Value = False Then
If (Counter < 1) Then
'Show an error if the counter is less than 1
MsgBox "Error: Counter less than 1", vbCritical
ElseIf Cells(1, counter).Value = "text1" Or Cells(1, counter).Value = "text2" Or Cells(1, counter).Value = "text3" Then
'do stuff
End If
End If
End Sub
This should work:
If Worksheets("Name of your worksheet").Cells(counter, 1).Value)="text" Then
'execute some code
End if

How to write a VB script to find multiple keywords within all cells and highlight each keyword?

I'm currently having a list of keywords (e.g. CFO, CTO, interim manager, etc.) and I want to have a macro assigned to a button that can search all cells in Column E of Sheet 1 which contain these keywords then give back the result as well as highlight the keyword in the cell.
Each keyword is in a separate cell in Column A of Sheet 2.
If there is one keyword in the list, it will search for one but if there are more, it will search for combination.
Here is the screenshot to illustrate what I've describe above
I have found something over the Internet with suggestion to use AutoFilter but I can only use it to perform a search for one keyword. This is what I've tried:
Sub EmailFilter()
Application.ScreenUpdating = False
With Worksheets("Sheet1").Columns("E:E")
.AutoFilter Field:=1, Criteria1:= _
"=*" & Worksheets("Sheet2").Range("A2:A10") & "*", Operator:=xlAnd
End With
Application.ScreenUpdating = True
End Sub
Thanks in advance.
The below code will color all the matches with the same color(I have chosen blue). You can write this macro in a module and then create a Form Control Button and assign the macro to the button.
Sub macro()
Dim a As Integer, x As String, mystring As String
a = 2
Sheets("Sheet2").Activate
Cells(a, 1).Activate
Do While ActiveCell.Value <> ""
x = ActiveCell.Value
p = Len(x)
Application.GoTo Sheet1.Range("E2")
Do While ActiveCell.Value <> ""
mystring = ActiveCell.Value
If InStr(mystring, x) > 0 Then
Position = InStr(1, mystring, x)
If Position > 0 Then
ActiveCell.Characters(Position, p).Font.Color = RGB(255, 0, 0)
End If
End If
ActiveCell.Offset(1, 0).Activate
Loop
a = a + 1
Application.GoTo Sheet2.Cells(a, 1)
Loop
End Sub
Let me know if you have any other specific requirements so that the code can be altered. I hope this helps.

VBA: Over-writing cell with multiple IF conditions

I would like to write a VBA macro to give a specific value when two conditions are met, but I can't seem to figure out a way to do so, and searching doesn't help me with my specific issue.
Here's a quick summary of the problem:
I want to over-write the value in a specific cell if the values in two other cells match something specific.
I have a working code for the same report that over-writes a value if one other value matches something specific, and here is the code for that:
Sub test_overwrite()
Dim msheet As Worksheet
Set msheet = ActiveSheet
'Overwrites for test and reactivated cells
For i = 2 To msheet.UsedRange.Rows.Count
Select Case msheet.Cells(i, 7)
Case Is = "test"
msheet.Cells(i, 5) = "test"
Case Is = "reactivated"
msheet.Cells(i, 5) = "reactivated"
End Select
Next i
End Sub
Basically, what I would like to add to this code is the ability to overwrite a cell in (i,7) if the value of (i,5) matches "expired" and the value of (i,6) matches "#N/A".
How would I do so?
Edit 1:
Here's something I have just tried, but I get an 'Object Required' error
Sub subs_test_new_tests()
'Mark certain fields that are 'expired' as 'test'
Dim msheet As Worksheet
Dim state As String
Dim match As String
Dim status As String
Set msheet = ActiveSheet
For i = 2 To msheet.UsedRange.Rows.Count
Set state = msheet.Cells(i, 5)
Set match = msheet.Cells(i, 6)
Set status = msheet.Cells(i, 7)
If state = "expired" And match = "#N/A" Then
status = "test"
End If
Next i
End Sub
Sub subs_test_new_tests()
With ActiveSheet
For i = 2 To .UsedRange.Rows.Count
If .Cells(i, 5) = "expired" Then
If .Cells(i, 6) = "#N/A" Or Application.IsNA(.Cells(i, 6)) Then
.Cells(i, 7) = "test"
End If
End If
Next i
End With
End Sub
Try this:
Sub test_overwrite()
Dim msheet As Worksheet
Set msheet = ActiveSheet
With msheet
'Overwrites for test and reactivated cells
For i = 2 To .UsedRange.Rows.Count
Select Case Trim(.Cells(i, 7).Text)
Case Is = "test"
Trim(.Cells(i, 5).Text) = "test"
Case Is = "reactivated"
Trim(.Cells(i, 5).Text) = "reactivated"
End Select
if Trim(.Cells(i, 5).Text) = "expired" And ISERROR(.Cells(i, 6)) then .Cells(i, 7) = "test"
Next i
End With
End Sub

Excel VBA Macro: Searching For Blank From Selection

I am trying to search through a selection from a table, find a value and then return a specific result.
The conditions I am trying are:
IF 'Name' = blank, return "N / A".
IF 'Result' = blank, OR "N/A", return "N / A".
IF 'Count' = 0, return "No", ELSE "Yes".
The code I have tried so far is as follows:
Sub DoStuffIfNotEmpty()
Set M = Selection
If Not IsEmpty(M) Then
MsgBox "I'm not empty!"
Else
MsgBox "Empty Value"
End If
End Sub
Also for reference, here is the test table I have created:
Reference Image
Test() sets up the worksheet and cells to examine -- if you want to work down a list of cells you can do it here -- and calls DoStuffIfNotEmpty, which examines the Name, Result and Count columns in order. Its not even close to being elegant but there you go...
Sub Test()
Dim cWorksheet As Worksheet
Dim CRange As Range
Set cWorksheet = ActiveWorkbook.Sheets("Sheet1")
Set CRange = cWorksheet.Range("A2:C2")
MsgBox DoStuffIfNotEmpty(cWorksheet, CRange), vbOKOnly
End Sub
Function DoStuffIfNotEmpty(CurrWorksheet As Worksheet, CurrRange As Range) As String
CurrWorksheet.Select
CurrRange.Select
Set m = Selection
If m.Cells(1, 1) = "" Or IsNull(m.Cells(1, 1)) Then
retmsg = "N/A"
Else
If m.Cells(1, 2) = "" Or IsNull(m.Cells(1, 2)) Or m.Cells(1, 2) = "N/A" Then
retmsg = "N/A"
Else
If m.Cells(1, 3) = 0 Then
retmsg = "No"
Else
retmsg = "Yes"
End If
End If
End If
DoStuffIfNotEmpty = retmsg
End Function

Using count, counta, countblank functions

I've tried count, counta, and countblank functions for this code, but it doesn't work. My code is:
Sheet1.Activate
If WorksheetFunction.CountBlank(Range(Cells(3, 3), Cells(50, 3))) > 0 Then
MsgBox "First Enter Data!"
Else
...
I want excel to do some calculations if all of the cells in range C3 to C50 are containing a number, and return the msgbox if they aren't.
All the other codes are true. I've checked them several times.
The problem is that even when all of those cells have numbers, the msgbox appears. I've tried many ways, but it keeps going wrong.
Please help me. Thanks a lot.
Edit:
1) if your numbers stored as text, use following code (it change cells format to "number format"):
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
With rng
.NumberFormat = "0.00"
.Value = .Value
If WorksheetFunction.Count(.Cells) <> .Cells.Count Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
End With
2) You can also use this one:
Dim c As Range
Dim isAllNumbers As Boolean
isAllNumbers = True
For Each c In ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
If Not IsNumeric(c) Or c = "" Then
isAllNumbers = False
Exit For
End If
Next
If Not isAllNumbers Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
You may also want to read this: How to avoid using Select/Active statements