Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90244

Using Input box for all variables in countif vba

$
0
0

I have tried to code a macro where a search is conducted for a specific text. A)The column to search in, B)The column where the result should appear and C) The text for which the search is being conducted All are referred by respective input boxes. The input box for columns to be searched and where output is to be placed should only need the column name,(and not range) indicated by letters (text and so string) as value. For example, if a search in column Y is to be done the input box should only need entry of letter "Y". I have tried various permutations, but could not replace Y2:Y&LastRow in the code below, so that it refers to the input from input box for the column to search in.

The code is as follows:-

Sub CountIfAllVariablesFromInputBox()
    Dim LastRow    As Long
    Dim ChkColumn  As String
    'display an input box asking for column
    ChkColumn = InputBox( _
    "Please enter column to check")
    'if no input stop
    ColumnNumber = Columns(ChkColumn).Column
    If Len(ChkColumn) = 0 Then
        MsgBox "No column entered"
        Exit Sub
    End If

    Dim InputColumn    As String
    'display an input box asking for column
    InputColumn = InputBox( _
    "Please enter column to insert results")
    'if no input stop
    If Len(InputColumn) = 0 Then
        MsgBox "No column entered"
        Exit Sub
    End If
'inputbox for text string to search for    
    Dim SuccessKeyWord As String
    SuccessKeyWord = InputBox(Prompt:="Enter KeyWord For Success", _
    Title:="KeyWord For Success", Default:="WOW!!")

    LastRow = Range(ChkColumn & Rows.Count).End(xlUp).Row

    Range(InputColumn & "1").Formula = "=COUNTIF(Range("Y2:Y"&LastRow),"""& SuccessKeyWord & """)"
 End With
End Sub

Googling threw up so many ways to refer to ranges (with cells, cell, variables) that I am overwhelmed, unfortunately I could not get result by any of those.

I would be really thankful of your kind help.

I have posted a screenShot.sometimes I may need to search in column "W" and at others in column "Y". I need that flexibility by using the inputbox.

Screen Shot of the columns

Error after replacing the last line of the code by:-

Range(InputColumn & "1").Formula = "=COUNTIF(Range("""& ChkColumn & 2 & ":"& ChkColumn & """&LastRow),"""& SuccessKeyWord & """)"

Note:-

Search in column W

Result in column AA

Text to search WOW!!


Viewing all articles
Browse latest Browse all 90244


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>