0

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) 但我不断收到语法错误。

谢谢。

4

2 回答 2

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

输出:

在此处输入图像描述

于 2013-01-14T17:38:08.847 回答
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
于 2013-03-07T08:52:48.803 回答