我目前在一列单元格中有以下公式但是你可以看到它的大而且凌乱,我担心这些单元格可能会被工作簿中的某个人意外地编辑。
所以我尝试在VBA中对其进行编码,但我不断获得Run-Time Error '91'
。而且我对于需要调整以使其运行感到困惑。
感谢您的帮助
当前公式
=IF(B7=$H$5,"1",IF(B7=$H$6,".75",IF(B7=$H$7,".75",IF(B7=$H$8,"1",IF(B7=$H$9,"1",IF(B7=$H$10,"1",IF(B7=$H$11,".5",IF(B7=$H$12,".5",IF(B7=$H$13,".5",IF(B7=$H$14,".5",IF(B7=$H$15,"1",IF(B7=$H$16,".75",IF(B7=$H$17,"1",IF(B7=$H$18,"1",IF(B7=$H$19,".75",IF(B7=$H$20,"1",IF(B7=$H$21,"1",IF(B7=$H$22,"1",IF(B7=$H$23,"1",IF(B7=$H$24,".75",IF(B7=$H$25,"1",IF(B7=$H$26,".75",IF(B7=$H$27,".5",IF(B7=$H$28,"1",IF(B7=$H$29,".75",IF(B7=$H$30,".5",IF(B7=$H$31,"1",IF(B7=$H$32,"1",IF(B7=$H$33,"1",IF(B7=$H$34,".5",IF(B7=$H$35,"1",IF(B7=$H$36,".25",IF(B7=$H$37,"1",IF(B7=$H$38,"1",IF(B7=$H$39,"1",IF(B7=$H$40,"1",IF(B7=$H$41,"1",IF(B7=$H$42,"1",IF(B7=$H$43,"1",IF(B7=$H$44,"1",IF(B7=$H$45,"1",IF(B7=$H$46,"1",IF(B7=$H$47,"1",IF(B7=$H$48,"1",IF(B7=$H$49,"1",IF(B7=$H$50,".5",IF(B7=$H$51,"1",IF(B7=$H$52,".25",IF(B7=$H$53,"1",IF(B7=$H$54,".75",IF(B7=$H$55,"1",IF(B7=$H$56,"1",IF(B7=$H$57,"1")))))))))))))))))))))))))))))))))))))))))))))))))))))
我尝试将其转换为VBA代码
Sub Macro()
Dim Whole as long
Dim Third as long
Dim half as long
Dim quarter as long
Dim lookat as range
Dim answer as range
Whole = 1
third = .75
Half = .5
Quarter = .25
Lookat = Worksheets("sheet1".Range("B2:B300")
Answer = worksheets("Sheet1").range("C2:C300")
If Lookat = "AAAA" Or "AAAB" Or "AAAC" Or "AAAD" Or "AAAE" Or "AAAF" Or "AAAG" Or "AAAH" Or "AAAI" Or "AAAJ" Or "AAAK" Or "AAAL" Or "AAAM" Or "AAAN" Or "AAAO" Or "AAAP" Or "AAAQ" Or "AAAR" Or "AAAS" Or "AAAT" Or "AAAU" Or "AAAV" _
Or "AAAW" Or "AAAX" Or "AAAY" Or "AAAZ" Or "BBBA" Or "BBBB" Or "BBBC" Or "BBBD" Or "BBBE" Or "BBBF" Or "BBBG" Then
Answer.value=whole
ElseIf Lookat = "AAA" Or "AAB" Or "AAC" Or "AAD" Or "AAE" Or "AAF" Or "AAG" Or "AAH" Then
Answer.Value = Third
ElseIf Lookat = "AA" Or "AB" Or "AC" Or "AD" Or "AE" Or "AF" Or "AG" Or "AH" Then
Answer.Value = Half
ElseIf Lookat = "A" Or "B" Then
Answer.Value = Quarter
end if
End Sub
答案 0 :(得分:0)
1保护工作簿Link或将其隐藏在工作表中
2创建您的数据库(您的'H'列)
3在'I'栏中你的体重[整体,三分之一,一半,四分之一]。样品:
+---------+--------+
| COL 'H' | HEIGHT |
+---------+--------+
| 6 | 1 |
| 9 | 0,5 |
| 4 | 0,75 |
| 6 | 1 |
| 8 | 0,5 |
| 1 | 0,75 |
| 5 | 1 |
| 4 | 0,5 |
| 5 | 0,75 |
| 7 | 1 |
| 4 | 0,5 |
| 9 | 0,75 |
| 1 | 1 |
| 8 | 0,5 |
| 1 | 0,75 |
| 5 | 1 |
| 1 | 0,5 |
| 4 | 0,75 |
| 4 | 1 |
| 1 | 0,5 |
| 7 | 0,75 |
+---------+--------+
4将您当前的公式更改为:
=VLOOKUP(B8,H:I,2,FALSE)
PS:您可以尝试命名数据库,以便更好地理解Link。
答案 1 :(得分:0)
这样的事可能适合:
+----+-----+------+ | | A | B | +----+-----+------+ | 1 | H10 | 1 | | 2 | H14 | 0.5 | | 3 | H15 | 1 | | 4 | H16 | 0.75 | | 5 | H18 | 1 | | 6 | H19 | 0.75 | | 7 | H23 | 1 | | 8 | H24 | 0.75 | | 9 | H25 | 1 | | 10 | H26 | 0.75 | | 11 | H27 | 0.5 | | 12 | H28 | 1 | | 13 | H29 | 0.75 | | 14 | H30 | 0.5 | | 15 | H33 | 1 | | 16 | H34 | 0.5 | | 17 | H35 | 1 | | 18 | H36 | 0.25 | | 19 | H49 | 1 | | 20 | H5 | 1 | | 21 | H50 | 0.5 | | 22 | H51 | 1 | | 23 | H52 | 0.25 | | 24 | H53 | 1 | | 25 | H54 | 0.75 | | 26 | H57 | 1 | | 27 | H7 | 0.75 | +----+-----+------+
位于工作簿中的工作表中,并且数组名为Workbook Scope的Vt
。
然后可以将其用作具有以下公式的查找表:
=VLOOKUP(A1,Vt,2)
其中A1包含诸如“H10”之类的值,用于本示例的目的。
如果要利用不特定等于每个可能值的表,重要的是按ColumnA的升序对“其他”(待隐藏/受保护)表单中的A:B进行排序。 VLOOKUP公式中缺少第四个参数(虽然经常导致问题!)意味着在没有完全匹配的情况下会找到近似匹配。