Note that there are some explanatory texts on larger screens.

plurals
  1. POexcel countif with user input variables
    primarykey
    data
    text
    <p>new to VBA so please be gentle.....</p> <p>I have a script that check for duplicates and inserts a count in a column, this works fine however the sheets are often different so I need to ask the user which column to check for duplicates and which column to insert the count. I've modified the script, but I'm only getting zero's entered into the destination column. I can't see what's going wrong. Any help would be great. Thanks in advance.</p> <pre><code>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 '------------------------------------------------------- </code></pre> <p>'This is the original version of my script with set columns which works great..... However I need the user to specify the column to checck and also which column the results will be entered.</p> <pre><code> 'LastRow = Range("B" &amp; Rows.Count).End(xlUp).Row ' With Range("E1") ' .FormulaR1C1 = "=COUNTIF(C2,RC[-3])" ' .AutoFill Destination:=Range("E1:E" &amp; LastRow) ' Range("E1").Select ' ActiveCell.FormulaR1C1 = "Duplicates" '----------------------------------------------------- LastRow = Range(column1 &amp; Rows.Count).End(xlUp).Row With Range(column2 &amp; "1") .FormulaR1C1 = "=COUNTIF(C2,RC[-3])" .AutoFill Destination:=Range(column2 &amp; "1" &amp; ":" &amp; column2 &amp; LastRow) Range(column2 &amp; "1").Select ActiveCell.FormulaR1C1 = "Duplicates" End With End Sub </code></pre> <p>I cannot get this working with the user input variables, apologies if I'm missing something but I can't find any resources on this....</p> <p>The formula: =COUNTIF($B:$B,B2) works except when in the macro.</p> <p>I need to add this line to the macro replaced with variables from user input like: =COUNTIF($column1:$column1,column12) but I keep getting syntax errors.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload