Excel - 使用用户指定的列重复计数 [英] Excel - Count Duplicates with user specified columns
问题描述
我正在尝试在excel中创建一个宏,提示用户输入要检查的列和输出结果的列。
例如我我想检查列a是否有重复项并输出一个计数到列f
我已经尝试过各种各样但不能正常工作:
Sub LookForDuplicates()
Dim LastRow As Long
Dim column1 As String
''显示一个输入框询问列
column1 = InputBox(_
请输入列到ckeck)
''如果没有选择文件名,这样说并停止
如果Len( column1)= 0然后
MsgBox没有列输入
退出Sub
结束如果
Dim column2 As String
''显示一个输入框询问列
column2 = InputBox(_
请输入列以插入结果)
''如果没有选择文件名,这样说并停止
如果Len (column2)= 0然后
Msg框没有列输入
退出Sub
结束如果
''------------------ -------------------------------------
''这是我的脚本的原始版本,设置列很好用.....
''LastRow =范围(B& Rows.Count)。End(xlUp).Row
''With Range(E1)
''.FormulaR1C1 == COUNTIF(C2,RC [-3])
''.AutoFill Destination:= Range(E1:E& LastRow)
''Range(E1)。选择
''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 )。选择
ActiveCell.FormulaR1C1 =重复
结束
结束子
当脚本运行时,它会提示输入要检查的列和输出列,但是如果有重复项则填充列为零。
这似乎是填充公式的一个问题,这是列i选择检查重复的公式输出总是读取:
= COUNTIF($ B:$ B,E2)
我选择哪一列来检查这个应该是A而不是B没有区别。一旦我将它改为A它就可以了,但是它不能正确填充。
所以我猜这一行:
.FormulaR1C1 == COUNTIF(C2,RC [-3])
不正确,但我无法正常工作。
< blockquote> B:
B,E2)
我选择哪一列来检查这一列应该是A没有区别不是B.一旦我把它改成A就可以了,但它不能正确填充。
所以我猜这一行:
.FormulaR1C1 == COUNTIF(C2,RC [-3])
不正确,但我无法正常工作。
首先,你需要知道一些事情。 t cell referencing:公式概述 [ ^ ]。然后看一下: COUNTIF公式 [ ^ ]。将 条件 定义为单元格的常量calue或绝对引用。
I am trying to create a macro in excel that prompts a user to input a column to check and a column to output the results to.
for example I would like to check column a for duplicates and output a count to column f
I''ve tried everyhting but cannot get this working:
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 ''------------------------------------------------------- ''This is the original version of my script with set columns which works great..... ''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
When the script runs it prompts for the column to check and the column for the output as expected but if fills the column with zero''s even though there are duplicates.
It seems to be an issue with the fill formula, which ever column is selected to check for duplicates the formula output always reads:
=COUNTIF($B:$B,E2)
It makes no difference which column I selected to check this one should of been A not B. As soon as I change it to A it works, but it will not fill correctly.
So i guess this line:
.FormulaR1C1 = "=COUNTIF(C2,RC[-3])"
Is incorrect but I cannot get it working.
B:
B,E2)
It makes no difference which column I selected to check this one should of been A not B. As soon as I change it to A it works, but it will not fill correctly.
So i guess this line:
.FormulaR1C1 = "=COUNTIF(C2,RC[-3])"
Is incorrect but I cannot get it working.
First of all, you need to know something about cell referencing: Overview of formulas[^]. Then have a look here at: COUNTIF formula[^]. Define criteria as constant calue or absolute reference to the cell.
这篇关于Excel - 使用用户指定的列重复计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!