对 countif vba 中的所有变量使用输入框
Using Input box for all variables in countif vba
我尝试编写一个宏来搜索特定文本。
A)要搜索的列,
B)结果应该出现的列和
C)进行搜索的文本
所有都由各自的输入框引用。
要搜索的列和要放置输出的输入框 应该只需要列名,(而不是范围) 由字母(文本等字符串)指示作为值。
例如,如果要在 Y 列中进行搜索,则输入框应该只需要输入字母 "Y"。
我尝试了各种排列,但无法替换下面代码中的 Y2:Y&LastRow,因此它 指的是要搜索的列的输入框的输入。
代码如下:-
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
谷歌搜索提出了很多方法来引用范围(包括单元格、单元格、变量),我不知所措,不幸的是我无法通过任何一种方法获得结果。
非常感谢您的帮助。
我已经发布了 screenShot.sometimes 我可能需要在 "W" 列中搜索,在 "Y" 列中搜索其他内容。我需要使用输入框的灵活性。
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 & """)"
或
Range(InputColumn & "1").Formula = "=COUNTIF(Range(""" & ChkColumn & 2 & ":" & ChkColumn &LastRow & """),""" & SuccessKeyWord & """)"
注:-
在 W 列中搜索
AA 列中的结果
要搜索的文字 WOW!!
对您的行代码进行一些修改:
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
On Error Resume Next
ColumnNumber = Columns(ChkColumn).Column
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
Dim InputColumn As String
'display an input box asking for column
On Error Resume Next
InputColumn = InputBox( _
"Please enter column to insert results")
'if no input stop
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'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(""" & ChkColumn & 2 & ":" & ChkColumn &LastRow & """),""" & SuccessKeyWord & """)"
'End With
End Sub
假设您希望用户select列
Sub CountIfAllVariablesFromInputBox()
Dim LastRow As Long, Rng As Range
Dim ChkColumn As Range
Dim InputColumn As Range
Dim SuccessKeyWord As String
'display an input box asking for column
Set ChkColumn = Application.InputBox("Please enter column to check", Type:=8)
'if no input stop
If Len(ChkColumn) = 0 Then
MsgBox "No column entered"
Exit Sub
End If
ColumnNumber = ChkColumn.Column
'display an input box asking for column
Set InputColumn = Application.InputBox( _
"Please enter column to insert results", Type:=8)
'if no input stop
If InputColumn Is Nothing Then Exit Sub
'inputbox for text string to search for
SuccessKeyWord = InputBox(Prompt:="Enter KeyWord For Success", _
Title:="KeyWord For Success", Default:="WOW!!")
LastRow = Cells(Rows.Count, ColumnNumber).End(xlUp).Row
Set Rng = Range(Cells(1, ColumnNumber), Cells(LastRow, ColumnNumber))
Cells(1, InputColumn.Column) = "=COUNTIF(" & Rng.Address & ",""" & SuccessKeyWord & """)"
End Sub
哦,终于搞定了
Sub CountIfAllVariablesFromInputBox()
Dim LastRow As Long
Dim ChkColumn As String
Dim InputColumn As String
Dim SuccessKeyWord As String
Dim rng As Range
'display an input box asking for column
ChkColumn = Application.InputBox("Please enter column to check")
'if no input stop
On Error Resume Next
ColumnNumber = Columns(ChkColumn).Column
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'display an input box asking for column
On Error Resume Next
InputColumn = Application.InputBox( _
"Please enter column to insert results")
'if no input stop
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'inputbox for text string to search for
SuccessKeyWord = Application.InputBox(Prompt:="Enter KeyWord For Success", _
title:="KeyWord For Success", Default:="WOW!!")
LastRow = Range(ChkColumn & Rows.Count).End(xlUp).Row
Set rng = Range(ChkColumn & 2 & ":" & ChkColumn & LastRow)
Range(InputColumn & "1").Value = WorksheetFunction.CountIf(rng, SuccessKeyWord)
End Sub
感谢@JvdV @chrisneilsen @user11982798 @Davesexcel
我尝试编写一个宏来搜索特定文本。 A)要搜索的列, B)结果应该出现的列和 C)进行搜索的文本 所有都由各自的输入框引用。 要搜索的列和要放置输出的输入框 应该只需要列名,(而不是范围) 由字母(文本等字符串)指示作为值。 例如,如果要在 Y 列中进行搜索,则输入框应该只需要输入字母 "Y"。 我尝试了各种排列,但无法替换下面代码中的 Y2:Y&LastRow,因此它 指的是要搜索的列的输入框的输入。
代码如下:-
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
谷歌搜索提出了很多方法来引用范围(包括单元格、单元格、变量),我不知所措,不幸的是我无法通过任何一种方法获得结果。
非常感谢您的帮助。
我已经发布了 screenShot.sometimes 我可能需要在 "W" 列中搜索,在 "Y" 列中搜索其他内容。我需要使用输入框的灵活性。
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 & """)"
或
Range(InputColumn & "1").Formula = "=COUNTIF(Range(""" & ChkColumn & 2 & ":" & ChkColumn &LastRow & """),""" & SuccessKeyWord & """)"
注:-
在 W 列中搜索
AA 列中的结果
要搜索的文字 WOW!!
对您的行代码进行一些修改:
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
On Error Resume Next
ColumnNumber = Columns(ChkColumn).Column
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
Dim InputColumn As String
'display an input box asking for column
On Error Resume Next
InputColumn = InputBox( _
"Please enter column to insert results")
'if no input stop
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'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(""" & ChkColumn & 2 & ":" & ChkColumn &LastRow & """),""" & SuccessKeyWord & """)"
'End With
End Sub
假设您希望用户select列
Sub CountIfAllVariablesFromInputBox()
Dim LastRow As Long, Rng As Range
Dim ChkColumn As Range
Dim InputColumn As Range
Dim SuccessKeyWord As String
'display an input box asking for column
Set ChkColumn = Application.InputBox("Please enter column to check", Type:=8)
'if no input stop
If Len(ChkColumn) = 0 Then
MsgBox "No column entered"
Exit Sub
End If
ColumnNumber = ChkColumn.Column
'display an input box asking for column
Set InputColumn = Application.InputBox( _
"Please enter column to insert results", Type:=8)
'if no input stop
If InputColumn Is Nothing Then Exit Sub
'inputbox for text string to search for
SuccessKeyWord = InputBox(Prompt:="Enter KeyWord For Success", _
Title:="KeyWord For Success", Default:="WOW!!")
LastRow = Cells(Rows.Count, ColumnNumber).End(xlUp).Row
Set Rng = Range(Cells(1, ColumnNumber), Cells(LastRow, ColumnNumber))
Cells(1, InputColumn.Column) = "=COUNTIF(" & Rng.Address & ",""" & SuccessKeyWord & """)"
End Sub
哦,终于搞定了
Sub CountIfAllVariablesFromInputBox()
Dim LastRow As Long
Dim ChkColumn As String
Dim InputColumn As String
Dim SuccessKeyWord As String
Dim rng As Range
'display an input box asking for column
ChkColumn = Application.InputBox("Please enter column to check")
'if no input stop
On Error Resume Next
ColumnNumber = Columns(ChkColumn).Column
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'display an input box asking for column
On Error Resume Next
InputColumn = Application.InputBox( _
"Please enter column to insert results")
'if no input stop
If Err.Description <> "" Then
MsgBox "No column entered or Something Error"
Exit Sub
End If
On Error GoTo 0
'inputbox for text string to search for
SuccessKeyWord = Application.InputBox(Prompt:="Enter KeyWord For Success", _
title:="KeyWord For Success", Default:="WOW!!")
LastRow = Range(ChkColumn & Rows.Count).End(xlUp).Row
Set rng = Range(ChkColumn & 2 & ":" & ChkColumn & LastRow)
Range(InputColumn & "1").Value = WorksheetFunction.CountIf(rng, SuccessKeyWord)
End Sub
感谢@JvdV @chrisneilsen @user11982798 @Davesexcel