如何将字符串与VBA中的数字匹配

时间:2016-06-21 08:27:44

标签: loops if-statement do-while

希望有人能帮助我。

目标:我正在尝试匹配2个信息单元格,

细胞1数据:Chicken1234 Cell 2数据:1234

单元格2将确定从右侧用于匹配条件的单元格1的字符数。 因此,在excel上下文中,它应该是= if(cell2 =(right(cell1,len(cell2))," True"," False")

请告诉我我做错了什么以及如何改善这一点。

示例数据:

肉类|鸡肉1234 |回答正确

MeatNo | 1234 |回答正确

Sub matchingtest()

Dim MTAC As Variant, BankAC As Variant


[A1].Select

MeatNo = ActiveCell.Offset(1, 1)
No = ActiveCell.Offset(0, 1)

Do While Not IsEmpty(ActiveCell.Offset(1, 0))

    If Not IsEmpty(ActiveCell) And _
        No = MeatNo Then _

            ActiveCell.Offset(0, 3).FormulaR1C1 = ("True")
            ActiveCell.Offset(1, 0).Select

        Else

           ActiveCell.Offset(0, 10).FormulaR1C1 = ("False")
           ActiveCell.Offset(1, 0).Select

    End If

Loop



End Sub

1 个答案:

答案 0 :(得分:0)

一个基本的例子可能是这样的(未经测试):

myMeats = Array("Chicken", "Porc", "Beef")
Function CheckMeat(compareTo as String)
  For i = 0 To UBound(myMeats)
    Dim TestComp As Integer
    ' The two strings sort equally. Returns 0.
    TestComp = StrComp(myMeats[i], compareTo, CompareMethod.Text)
    ' -1 string1 is in string2, 0 equal, +1 string2 is in string1
    If TestComp = -1 or TestComp = 0 or TestComp = 1 Then
      return true
    End If
  End For
End Function

自:

https://msdn.microsoft.com/fr-fr/library/9s233cfc(v=vs.90).aspx
http://www.fontstuff.com/vba/vbatut01.htm
http://www.mrexcel.com/forum/excel-questions/485383-define-list-values-within-visual-basic-applications-code.html