Excel VBA将列与输入值进行比较

时间:2013-11-20 10:52:48

标签: excel vba excel-vba

我是excel宏上的noob,我正在尝试创建一个宏:

我想输入一些数字并将其与A列中的值进行比较

例如:

excel中的列包含:

1234

1233

1236

我的意见是:

1234

结果:

B栏

1 > 1234 - 1234(找一次)

0 > 1234 - 1233(不一样)

0 > 1234 - 1236(不一样)

C列

1233 >第一个值与输入

不一样

1236 >第二个值与输入

不一样

现在我只知道如何输入值:(类似这样:

Sub getInput()

    MyInput = InputBox("Enter Number")
    MsgBox ("Searching") & MyInput

End Sub

2 个答案:

答案 0 :(得分:0)

我找到了答案,所以我将分享

Sub getInput()

    Dim i As Integer           ' Integer used in 'For' loop
    Dim x As Integer
    Dim total As Integer
    Dim save As Integer

Do
MyInput = InputBox("Enter Number")

If MyInput = "" Then

    Exit Sub

Else

    MsgBox ("Searching ") & MyInput

    total = 0
    x = 2
    y = x + 1

    For i = 1 To 3         'hardcode - 3 because I have 3 value in A
        Cells(i, y).Value = ""
        If Int(Cells(i, 1).Value) = Int(MyInput) Then
            ' A match has been found to the supplied string
            ' Store the current row number and exit the 'For' Loop
            total = total + 1
            If Cells(i, x).Value = 0 Then

            Cells(i, x).Value = Int(total)
            Else
            Cells(i, x).Value = Int(Cells(i, x).Value) + Int(total)
            End If

        Else
            Cells(i, y).Value = Cells(i, 1).Value
        End If
    Next i

    ' Pop up a message box to let the user know if the text

    If total = 0 Then
        MsgBox "String " & MyInput & " not found"
    Else
        MsgBox "String " & MyInput & " found"
    End If
    x = x + 1
End If

Loop Until MyInput = ""    'Loop until user press cancel or input blank data

End Sub

答案 1 :(得分:0)

首先通过输入框输入值到纸张 希望它有助于

Sub Macro1()

    Dim myformula As String
    Dim lr As Long
    With Sheets(1)
    lr = .Cells(.Rows.Count, "A").End(xlUp).Row


    Range("p1").Value = Application.InputBox("Enter Number", , , , , , 1)

    Range("B1").Select
    myformula = "=IF(R1C16=RC[-1],1,0)"
    Range("b1").Formula = myformula
    Selection.AutoFill Destination:=Range("B1:B" & lr), Type:=xlFillDefault


    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=IF(C[-1]=0,C[-2],"""")"
    Selection.AutoFill Destination:=Range("C1:C" & lr), Type:=xlFillDefault

   End With

End Sub