使用用户输入变量excel countif

时间:2013-01-14 16:15:55

标签: excel vba countif

VBA新手所以请温柔.....

我有一个检查重复项并在列中插入计数的脚本,这样可以正常工作,但是表单通常不同,所以我需要询问用户哪个列检查重复项以及要插入计数的列。我修改了脚本,但我只是将零输入目标列。我看不出有什么问题。任何帮助都会很棒。提前谢谢。

Sub LookForDuplicates()

Dim LastRow As Long

Dim column1 As String
'display an input box asking for column
column1 = InputBox( _
"Please enter column to ckeck")
'if no file name chosen, say so and stop
If Len(column1) = 0 Then
MsgBox "No column entered"

Exit Sub
End If

Dim column2 As String
'display an input box asking for column
column2 = InputBox( _
"Please enter column to insert results")
'if no file name chosen, say so and stop
If Len(column2) = 0 Then
MsgBox "No column entered"

Exit Sub
End If

'-------------------------------------------------------

'这是我的脚本的原始版本,设置列很有效.....但是我需要用户指定要检查的列以及输入结果的列。

  'LastRow = Range("B" & Rows.Count).End(xlUp).Row
  '   With Range("E1")
  '  .FormulaR1C1 = "=COUNTIF(C2,RC[-3])"
  '  .AutoFill Destination:=Range("E1:E" & LastRow)
  '   Range("E1").Select
  '  ActiveCell.FormulaR1C1 = "Duplicates"
'-----------------------------------------------------   
LastRow = Range(column1 & Rows.Count).End(xlUp).Row
 With Range(column2 & "1")
.FormulaR1C1 = "=COUNTIF(C2,RC[-3])"
.AutoFill Destination:=Range(column2 & "1" & ":" & column2 & LastRow)
 Range(column2 & "1").Select
ActiveCell.FormulaR1C1 = "Duplicates"

  End With
End Sub

我无法使用用户输入变量进行处理,如果我遗漏了某些东西而道歉,但我找不到任何资源....

公式:= COUNTIF($ B:$ B,B2)有效,除非在宏中。

我需要将此行添加到用用户输入的变量替换的宏中,例如:= COUNTIF($ column1:$ column1,column12)但我一直遇到语法错误。

感谢。

2 个答案:

答案 0 :(得分:0)

如果您希望输入框中有String / Text值,那么您应该指定它,

Dim column1 As String
'display an input box asking for column
column1 = InputBox("Please enter column to ckeck", "Range to Check", , , , 2)

为什么不使用Range对象,而不是在这里玩弄字符串,用户只需点击要检查的列中的整个范围或一个单元格即可。

使用范围获取输入框数据:您的主要问题似乎是设置范围以检查公式中的列。

Option Explicit

Sub LookForDuplicates()
    Dim LastRow As Long, StartRow as Long
    Dim column1 As Range, column2 As Range

    Set column1 = Application.InputBox("Please enter _ 
            column to ckeck", "Range to Check", , , , , , 8)
    If column1 Is Nothing Then
        MsgBox "No column entered"
        Exit Sub
    End If

    Set column2 = Application.InputBox("Please _ 
                  enter column to insert results", _
                              "Range to Output Results", , , , , , 8)
    If column2 Is Nothing Then
        MsgBox "No column entered"
        Exit Sub
    End If

    LastRow = Cells(Rows.Count, column1.Column).End(xlUp).Row '--updated here
    StartRow = column2.Row '-- here a new code added, assuming that you will have at least one row for column titles 
     With column2
        .FormulaR1C1 = "=COUNTIF(R" & column1.Row _ 
                & "C[-1]:R" & LastRow + 2 & "C[-1],RC[-1])"
        .AutoFill Destination:=column2.Resize(LastRow - StartRow, 1)
    End With
    column2.Offset(-1, 0).FormulaR1C1 = "Duplicates"
End Sub

输出:

enter image description here

答案 1 :(得分:0)

解决方案,如果其他人可能会发现这有用:

问题是即使第1列作为列引用H输入,例如COUNTIF函数需要将其作为数字引用,因此将column1值的额外变量添加到数值并修改公式以适应。现在全部工作:

Dim LastRow As Long

Dim column1 As String
'display an input box asking for column
column1 = InputBox( _
"Please enter column to ckeck")
'if no file name chosen, say so and stop
ColumnNumber = Columns(column1).Column

If Len(column1) = 0 Then
MsgBox "No column entered"


Exit Sub
End If

Dim column2 As String
'display an input box asking for column
column2 = InputBox( _
"Please enter column to insert results")
'if no file name chosen, say so and stop
If Len(column2) = 0 Then
MsgBox "No column entered"

Exit Sub
End If

LastRow = Range(column1 & Rows.Count).End(xlUp).Row
     With Range(column2 & "1")
    .FormulaR1C1 = "=COUNTIF(C" & ColumnNumber & ",C" & ColumnNumber & ")"
    .AutoFill Destination:=Range(column2 & "1" & ":" & column2 & LastRow)
     Range(column2 & "1").Select
    ActiveCell.FormulaR1C1 = "Duplicates"

  End With
End Sub