2013-01-14 116 views
0

新VBA所以請溫柔.....的Excel COUNTIF用戶輸入變量

我有重複檢查,並在列插入計數的腳本,這工作得很好,不過牀單往往不同所以我需要詢問用戶在哪一列檢查重複項以及在哪一列插入計數。我修改了腳本,但是我只是將零輸入到目標列中。我看不出有什麼問題。任何幫助都會很棒。提前致謝。

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 

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

「這是原來的版本我的腳本與組列偉大的工程.....不過,我需要用戶指定的列checck也哪一列將結果輸入。

'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($列1:$ COLUMN1,column12),但我不斷收到語法錯誤。

謝謝。

+0

我已經更新了您的意見的答案。請看看它。我用'range',因爲它相比,將代表範圍/電池串的靈活得多.. :) – bonCodigo

回答

0

如果其他人可能會發現解決這個有用:

的問題是,即使第1列被輸入,例如COUNTIF函數一列參考H要求將此值作爲數字參考,以便將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 
0

如果你期望從你的輸入框中String /文本價值,那麼你應該指定它,

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

而是與這裏的String雜耍的,你爲什麼不只是使用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

+0

感謝@bonCodigo我真的很感謝您抽出寶貴時間來幫助然而一個範圍是不是真的適合這個。這些專欄通常很長,用戶會爲此而苦惱。我真的需要指定一個列來檢查和一列輸出。 – Paul

+0

在上面的例子,你**不必選擇整列範圍**。第一個單元就足夠了,這就是我上面所做的。然後代碼在該列中找到上次使用的行('LASTROW = column1.Rows.Count'),而不是你要通過整列,並且是一個性能題材來搜索。我不確定你是否真的明白了我在上面的代碼中的含義。請讓我知道:) – bonCodigo

+0

啊,是的,這是更有意義。我的印象是你必須選擇整個數據選擇。非常感謝您的幫助,非常感謝。現在,如果我運行該腳本並選擇一列數據,然後再輸入另一列數據,則會在行上遇到錯誤1004 Apllication-defined或object-defined error:.FormulaR1C1 =「= COUNTIF(R」&column1.Row& 「C [-1]:R」&LastRow + 2&「C [-1],RC [-1])」我拿了_和新的線出櫃... – Paul