Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "If cell contains" in VBA excel

Tags:

excel

vba

I'm trying to write a macro where if there is a cell with the word "TOTAL" then it will input a dash in the cell below it. For example:

enter image description here

In the case above, I would want a dash in cell F7 (note: there could be any number of columns, so it will always be row 7 but not always column F).

I'm currently using this code, but it's not working and I can't figure out why.

Dim celltxt As String
Range("C6").Select
Selection.End(xlToRight).Select
celltxt = Selection.Text
If InStr(1, celltext, "TOTAL") > 0 Then
Range("C7").Select
Selection.End(xlToRight).Select
Selection.Value = "-"
End If

Help would be appreciated. Hopefully I'm not doing something stupid.

like image 930
Moogle Avatar asked Dec 11 '14 15:12

Moogle


People also ask

How do you check if a cell contains a value in VBA?

The ISTEXT function test if the selected cell contains text. If it does then the function will return a TRUE value. The IF function is then used to return a specified value if the ISTEXT function returns a value of TRUE, which in this example is "Contains Text".

How do I use contains in Excel VBA?

Introduction - VBA InStrThe VBA InStr function is one of the most useful string manipulation functions around. You can use it to test if a string, cell or range contains the substring you want to find. If it does, “InStr” will return the position in the text string where your substring begins.

How do you check if a cell contains a value in Excel?

The Excel ISNUMBER function returns TRUE when a cell contains a number, and FALSE if not. You can use ISNUMBER to check that a cell contains a numeric value, or that the result of another function is a number. The Excel FIND function returns the position (as a number) of one text string inside another.

How to check if a cell contains a string in Excel?

If cell contains one of many text strings, then return a value Select the output cell, and use the following formula: =IF (OR (ISNUMBER (SEARCH ("string1", cell)), ISNUMBER (SEARCH... For our example, the cell we want to check is A2. We’re looking for either “tshirt” or “hoodie”, and the return ...

What happens if the cell contains text in Excel?

If the cell contains text the COUNTIF function will return a value of 1, alternatively it will return a value of 0. The IF function is then used to return a specified value if the COUNTIF function returns a value greater than 0, which in this example is "Contains Text".

How to check if strings contain numbers with VBA?

Steps to check if strings contain numbers with VBA are given below. Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window. In the code window, copy the following code and paste it.

How to test if a cell contains text using Excel ISNUMBER?

If a cell contains text with the Excel ISNUMBER function using VBA Output Range: Select the output range by changing the cell reference ("C5") in the VBA code. Cell to Test: Select the cell that you want to test by changing the cell reference ("B5") in the VBA code.


3 Answers

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Not Intersect(Target, Range("C6:ZZ6")) Is Nothing Then

    If InStr(UCase(Target.Value), "TOTAL") > 0 Then
        Target.Offset(1, 0) = "-"
    End If

End If

End Sub

This will allow you to add columns dynamically and automatically insert a dash underneath any columns in the C row after 6 containing case insensitive "Total". Note: If you go past ZZ6, you will need to change the code, but this should get you where you need to go.

like image 142
NameIsPete Avatar answered Oct 16 '22 12:10

NameIsPete


This will loop through all cells in a given range that you define ("RANGE TO SEARCH") and add dashes at the cell below using the Offset() method. As a best practice in VBA, you should never use the Select method.

Sub AddDashes()

Dim SrchRng As Range, cel As Range

Set SrchRng = Range("RANGE TO SEARCH")

For Each cel In SrchRng
    If InStr(1, cel.Value, "TOTAL") > 0 Then
        cel.Offset(1, 0).Value = "-"
    End If
Next cel

End Sub
like image 43
Chrismas007 Avatar answered Oct 16 '22 12:10

Chrismas007


This does the same, enhanced with CONTAINS:

Function SingleCellExtract(LookupValue As String, LookupRange As Range, ColumnNumber As Integer, Char As String)
Dim I As Long
Dim xRet As String
For I = 1 To LookupRange.Columns(1).Cells.Count
     If InStr(1, LookupRange.Cells(I, 1), LookupValue) > 0 Then
        If xRet = "" Then
            xRet = LookupRange.Cells(I, ColumnNumber) & Char
        Else
            xRet = xRet & "" & LookupRange.Cells(I, ColumnNumber) & Char
        End If
    End If
Next
SingleCellExtract = Left(xRet, Len(xRet) - 1)
End Function
like image 1
Michinio Avatar answered Oct 16 '22 13:10

Michinio