Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 我如何制作我的If…;那么是动态的吗?_Excel_Vba - Fatal编程技术网

Excel 我如何制作我的If…;那么是动态的吗?

Excel 我如何制作我的If…;那么是动态的吗?,excel,vba,Excel,Vba,请参见VBA中循环的使用 Sub ValidateCode() Dim list, llist, lllist As String, result, rresult, rrresult As String list = Range("J12").Value llist = Range("J18").Value lllist = Range("J24").Value 'Division If list = Rang

请参见VBA中循环的使用

Sub ValidateCode()    
    Dim list, llist, lllist As String, result, rresult, rrresult As String

    list = Range("J12").Value    
    llist = Range("J18").Value    
    lllist = Range("J24").Value

    'Division    
    If list = Range("E2").Value Then result = Range("D2").Value    
    If list = Range("E3").Value Then result = Range("D3").Value    
    If list = Range("E4").Value Then result = Range("D4").Value    
    If list = Range("E5").Value Then result = Range("D5").Value    
    If list = Range("E6").Value Then result = Range("D6").Value    
    If list = Range("E7").Value Then result = Range("D7").Value    
    If list = Range("E8").Value Then result = Range("D8").Value    
    If list = Range("E9").Value Then result = Range("D9").Value    
    If list = Range("E10").Value Then result = Range("D10").Value    
    If list = Range("E11").Value Then result = Range("D11").Value    
    If list = Range("E12").Value Then result = Range("D12").Value    
    If list = Range("E13").Value Then result = Range("D13").Value    
    If list = Range("E14").Value Then result = Range("D14").Value    
    If list = Range("E15").Value Then result = Range("D15").Value    
    If list = Range("E16").Value Then result = Range("D16").Value    
    If list = Range("E17").Value Then result = Range("D17").Value    
    If list = Range("E18").Value Then result = Range("D18").Value    
    If list = Range("E19").Value Then result = Range("D19").Value    
    If list = Range("E20").Value Then result = Range("D20").Value    
    f list = Range("E21").Value Then result = Range("D21").Value    
    If list = Range("E22").Value Then result = Range("D22").Value    
    If list = Range("E23").Value Then result = Range("D23").Value        

    'Product Category    
    If llist = Range("E26").Value Then rresult = Range("D26").Value    
    If llist = Range("E27").Value Then rresult = Range("D27").Value    
    If llist = Range("E28").Value Then rresult = Range("D28").Value    
    If llist = Range("E29").Value Then rresult = Range("D29").Value    
    If llist = Range("E30").Value Then rresult = Range("D30").Value    
    If llist = Range("E31").Value Then rresult = Range("D31").Value    
    If llist = Range("E32").Value Then rresult = Range("D32").Value    
    If llist = Range("E33").Value Then rresult = Range("D33").Value    
    If llist = Range("E34").Value Then rresult = Range("D34").Value    
    If llist = Range("E35").Value Then rresult = Range("D35").Value    
    If llist = Range("E36").Value Then rresult = Range("D36").Value    
    If llist = Range("E37").Value Then rresult = Range("D37").Value    
    If llist = Range("E38").Value Then rresult = Range("D38").Value    
    If llist = Range("E39").Value Then rresult = Range("D39").Value    
    If llist = Range("E40").Value Then rresult = Range("D40").Value    
    If llist = Range("E41").Value Then rresult = Range("D41").Value    
    If llist = Range("E42").Value Then rresult = Range("D42").Value    
    If llist = Range("E43").Value Then rresult = Range("D43").Value    
    If llist = Range("E44").Value Then rresult = Range("D44").Value    
    If llist = Range("E45").Value Then rresult = Range("D45").Value    
    If llist = Range("E46").Value Then rresult = Range("D46").Value    
    If llist = Range("E47").Value Then rresult = Range("D47").Value    
    If llist = Range("E48").Value Then rresult = Range("D48").Value    
    If llist = Range("E49").Value Then rresult = Range("D49").Value    
    If llist = Range("E50").Value Then rresult = Range("D50").Value    
    If llist = Range("E51").Value Then rresult = Range("D51").Value    
    f llist = Range("E52").Value Then rresult = Range("D52").Value    
    If llist = Range("E54").Value Then rresult = Range("D54").Value    
    If llist = Range("E55").Value Then rresult = Range("D55").Value    
    If llist = Range("E56").Value Then rresult = Range("D56").Value    
    If llist = Range("E57").Value Then rresult = Range("D57").Value

    'Brand Name    
    If lllist = Range("B2").Value Then rrresult = Range("A2").Value    
    If lllist = Range("B3").Value Then rrresult = Range("A3").Value    
    If lllist = Range("B4").Value Then rrresult = Range("A4").Value    
    If lllist = Range("B5").Value Then rrresult = Range("A5").Value    
    If lllist = Range("B6").Value Then rrresult = Range("A6").Value    
    If lllist = Range("B7").Value Then rrresult = Range("A7").Value    
    If lllist = Range("B8").Value Then rrresult = Range("A8").Value    
    If lllist = Range("B9").Value Then rrresult = Range("A9").Value    
    f lllist = Range("B10").Value Then rrresult = Range("A10").Value    
    If lllist = Range("B11").Value Then rrresult = Range("A11").Value    
    If lllist = Range("B12").Value Then rrresult = Range("A12").Value    
    If lllist = Range("B13").Value Then rrresult = Range("A13").Value    
    If lllist = Range("B14").Value Then rrresult = Range("A14").Value    
    If lllist = Range("B15").Value Then rrresult = Range("A15").Value    
    If lllist = Range("B16").Value Then rrresult = Range("A16").Value    
    If lllist = Range("B17").Value Then rrresult = Range("A17").Value    
    If lllist = Range("B18").Value Then rrresult = Range("A18").Value    
    If lllist = Range("B19").Value Then rrresult = Range("A19").Value    
    If lllist = Range("B20").Value Then rrresult = Range("A20").Value

    Range("J8").Value = result + rresult + rrresult    
    MsgBox "Cost Unit Code is " & Range("J8").Value
End Sub

请注意,如果在VBA中
Dim list、llist、lllist As String
lllist
声明为
String
,但前两个声明为
Variant
,则需要为每个变量指定一个类型,否则默认情况下为
Variant
另外,只要
i
,就应该声明
•我建议始终激活
选项显式
:在VBA编辑器中,转到工具›选项›强制正确的变量声明。实际上
结果
变量甚至应该是
双变量
,因为它们用于获取总和
结果+rresult+rrresult
。所以字符串在这里被转换成一个数字。如果我从另一张表中获取数据呢?
Option Explicit

Sub ValidateCode()

Dim list As String, llist As String, lllist As String
Dim result As String, rresult As String, rrresult As String
Dim i As Long

list = Range("J12").Value
llist = Range("J18").Value
lllist = Range("J24").Value

'Division

For i = 2 To 23
    If list = Range("E" & i).Value Then result = Range("D" & i).Value
Next

'Product Category
For i = 26 To 57
    If llist = Range("E" & i).Value Then rresult = Range("D" & i).Value
Next

'Brand Name
For i = 2 To 20
    If lllist = Range("B" & i).Value Then rrresult = Range("A" & i).Value
Next

Range("J8").Value = result + rresult + rrresult

MsgBox "Cost Unit Code is " & Range("J8").Value
End Sub