记录的宏给出了一个长嵌套IF公式的synax问题

时间:2018-01-05 17:49:00

标签: excel excel-vba formula vba

如果值以字母结尾(即“554B”变为“554.02”,“348M”变为“348.13”等),我将代码粘贴到E2中以修改A2内容的副本。

我在录制时将此公式粘贴到Excel中:

=IF(RIGHT(A2,1)="A",REPLACE(A2,LEN(A2),1,".01"),IF(RIGHT(A2,1)="B",REPLACE(A2,LEN(A2),1,".02"),IF(RIGHT(A2,1)="C",REPLACE(A2,LEN(A2),1,".03"),IF(RIGHT(A2,1)="D",REPLACE(A2,LEN(A2),1,".04"),IF(RIGHT(A2,1)="E",REPLACE(A2,LEN(A2),1,".05"),IF(RIGHT(A2,1)="F",REPLACE(A2,LEN(A2),1,".06"),IF(RIGHT(A2,1)="G",REPLACE(A2,LEN(A2),1,".07"),IF(RIGHT(A2,1)="H",REPLACE(A2,LEN(A2),1,".08"),IF(RIGHT(A2,1)="I",REPLACE(A2,LEN(A2),1,".09"),IF(RIGHT(A2,1)="J",REPLACE(A2,LEN(A2),1,".10"),IF(RIGHT(A2,1)="K",REPLACE(A2,LEN(A2),1,".11"),IF(RIGHT(A2,1)="L",REPLACE(A2,LEN(A2),1,".12"),IF(RIGHT(A2,1)="M",REPLACE(A2,LEN(A2),1,".13"),IF(RIGHT(A2,1)="N",REPLACE(A2,LEN(A2),1,".14"),IF(RIGHT(A2,1)="O",REPLACE(A2,LEN(A2),1,".15"),IF(RIGHT(A2,1)="P",REPLACE(A2,LEN(A2),1,".16"),IF(RIGHT(A2,1)="Q",REPLACE(A2,LEN(A2),1,".17"),IF(RIGHT(A2,1)="R",REPLACE(A2,LEN(A2),1,".18"),IF(RIGHT(A2,1)="S",REPLACE(A2,LEN(A2),1,".19"),IF(RIGHT(A2,1)="T",REPLACE(A2,LEN(A2),1,".20"),IF(RIGHT(A2,1)="U",REPLACE(A2,LEN(A2),1,".21"),IF(RIGHT(A2,1)="V",REPLACE(A2,LEN(A2),1,".22"),IF(RIGHT(A2,1)="W",REPLACE(A2,LEN(A2),1,".23"),IF(RIGHT(A2,1)="X",REPLACE(A2,LEN(A2),1,".24"),IF(RIGHT(A2,1)="Y",REPLACE(A2,LEN(A2),1,".25"),IF(RIGHT(A2,1)="Z",REPLACE(A2,LEN(A2),1,".26")))))))))))))))))))))))))))

它在Excel中正常工作。当我尝试根据录制运行宏时,它会出现语法问题。我已修改宏来修复换行符和一些缺失值,当录音打破行,但仍有问题。这是独立模块:

Sub formatE2()
Range("E2").Select
ActiveCell.FormulaR1C1 = _
    "=IF(RIGHT(RC[-4],1)=""A"",REPLACE(RC[-4],LEN(RC[-4]),1,"".01""),IF(RIGHT(RC[-4],1)=""B"",REPLACE(RC[-4],LEN(RC[-4]),1,"".02""), " & _
     IF(RIGHT(RC[-4],1)=""C"",REPLACE(RC[-4],LEN(RC[-4]),1,"".03""),IF(RIGHT(RC[-4],1)=""D"",REPLACE(RC[-4],LEN(RC[-4]),1,"".04""), " & _
     IF(RIGHT(RC[-4],1)=""E"",REPLACE(RC[-4],LEN(RC[-4]),1,"".05""),IF(RIGHT(RC[-4],1)=""F"",REPLACE(RC[-4],LEN(RC[-4]),1,"".06""), " & _
     IF(RIGHT(RC[-4],1)=""G"",REPLACE(RC[-4],LEN(RC[-4]),1,"".07""),IF(RIGHT(RC[-4],1)=""H"",REPLACE(RC[-4],LEN(RC[-4]),1,"".08""), " & _
     IF(RIGHT(RC[-4],1)=""I"",REPLACE(RC[-4],LEN(RC[-4]),1,"".09""),IF(RIGHT(RC[-4],1)=""J"",REPLACE(RC[-4],LEN(RC[-4]),1,"".10""), " & _
     IF(RIGHT(RC[-4],1)=""K"",REPLACE(RC[-4],LEN(RC[-4]),1,"".11""),IF(RIGHT(RC[-4],1)=""L"",REPLACE(RC[-4],LEN(RC[-4]),1 "".12""), " & _
     IF(RIGHT(RC[-4],1)=""M"",REPLACE(RC[-4],LEN(RC[-4]),1,"".13""),IF(RIGHT(RC[-4],1)=""N"",REPLACE(RC[-4],LEN(RC[-4]),1,"".14""), " & _
     IF(RIGHT(RC[-4],1)=""O"",REPLACE(RC[-4],LEN(RC[-4]),1,"".15""),IF(RIGHT(RC[-4],1)=""P"",REPLACE(RC[-4],LEN(RC[-4]),1,"".16""), " & _
     IF(RIGHT(RC[-4],1)=""Q"",REPLACE(RC[-4],LEN(RC[-4]),1,"".17""),IF(RIGHT(RC[-4],1)=""R"",REPLACE(RC[-4],LEN(RC[-4]),1,"".18""), " & _
     IF(RIGHT(RC[-4],1)=""S"",REPLACE(RC[-4],LEN(RC[-4]),1,"".19""),IF(RIGHT(RC[-4],1)=""T"",REPLACE(RC[-4],LEN(RC[-4]),1,"".20""), " & _
     IF(RIGHT(RC[-4],1)=""U"",REPLACE(RC[-4],LEN(RC[-4]),1,"".21""),IF(RIGHT(RC[-4],1)=""V"",REPLACE(RC[-4],LEN(RC[-4]),1,"".22""), " & _
     IF(RIGHT(RC[-4],1)=""W"",REPLACE(RC[-4],LEN(RC[-4]),1,"".23""),IF(RIGHT(RC[-4],1)=""X"",REPLACE(RC[-4],LEN(RC[-4]),1,"".24""), " & _
     IF(RIGHT(RC[-4],1)=""Y"",REPLACE(RC[-4],LEN(RC[-4]),1,"".25""),IF(RIGHT(RC[-4],1)=""Z"",REPLACE(RC[-4],LEN(RC[-4]),1,"".26"")))))))))))))))))))))))))))"

End Sub

为什么我一直会遇到语法问题?

1 个答案:

答案 0 :(得分:2)

您错过了每一行的公开引语,而您在,之前错过了""0.12""

Sub formatE2()
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RIGHT(RC[-4],1)=""A"",REPLACE(RC[-4],LEN(RC[-4]),1,"".01""),IF(RIGHT(RC[-4],1)=""B"",REPLACE(RC[-4],LEN(RC[-4]),1,"".02"")," & _
 "IF(RIGHT(RC[-4],1)=""C"",REPLACE(RC[-4],LEN(RC[-4]),1,"".03""),IF(RIGHT(RC[-4],1)=""D"",REPLACE(RC[-4],LEN(RC[-4]),1,"".04"")," & _
 "IF(RIGHT(RC[-4],1)=""E"",REPLACE(RC[-4],LEN(RC[-4]),1,"".05""),IF(RIGHT(RC[-4],1)=""F"",REPLACE(RC[-4],LEN(RC[-4]),1,"".06"")," & _
 "IF(RIGHT(RC[-4],1)=""G"",REPLACE(RC[-4],LEN(RC[-4]),1,"".07""),IF(RIGHT(RC[-4],1)=""H"",REPLACE(RC[-4],LEN(RC[-4]),1,"".08"")," & _
 "IF(RIGHT(RC[-4],1)=""I"",REPLACE(RC[-4],LEN(RC[-4]),1,"".09""),IF(RIGHT(RC[-4],1)=""J"",REPLACE(RC[-4],LEN(RC[-4]),1,"".10"")," & _
 "IF(RIGHT(RC[-4],1)=""K"",REPLACE(RC[-4],LEN(RC[-4]),1,"".11""),IF(RIGHT(RC[-4],1)=""L"",REPLACE(RC[-4],LEN(RC[-4]),1,"".12"")," & _
 "IF(RIGHT(RC[-4],1)=""M"",REPLACE(RC[-4],LEN(RC[-4]),1,"".13""),IF(RIGHT(RC[-4],1)=""N"",REPLACE(RC[-4],LEN(RC[-4]),1,"".14"")," & _
 "IF(RIGHT(RC[-4],1)=""O"",REPLACE(RC[-4],LEN(RC[-4]),1,"".15""),IF(RIGHT(RC[-4],1)=""P"",REPLACE(RC[-4],LEN(RC[-4]),1,"".16"")," & _
 "IF(RIGHT(RC[-4],1)=""Q"",REPLACE(RC[-4],LEN(RC[-4]),1,"".17""),IF(RIGHT(RC[-4],1)=""R"",REPLACE(RC[-4],LEN(RC[-4]),1,"".18"")," & _
 "IF(RIGHT(RC[-4],1)=""S"",REPLACE(RC[-4],LEN(RC[-4]),1,"".19""),IF(RIGHT(RC[-4],1)=""T"",REPLACE(RC[-4],LEN(RC[-4]),1,"".20"")," & _
 "IF(RIGHT(RC[-4],1)=""U"",REPLACE(RC[-4],LEN(RC[-4]),1,"".21""),IF(RIGHT(RC[-4],1)=""V"",REPLACE(RC[-4],LEN(RC[-4]),1,"".22"")," & _
 "IF(RIGHT(RC[-4],1)=""W"",REPLACE(RC[-4],LEN(RC[-4]),1,"".23""),IF(RIGHT(RC[-4],1)=""X"",REPLACE(RC[-4],LEN(RC[-4]),1,"".24"")," & _
 "IF(RIGHT(RC[-4],1)=""Y"",REPLACE(RC[-4],LEN(RC[-4]),1,"".25""),IF(RIGHT(RC[-4],1)=""Z"",REPLACE(RC[-4],LEN(RC[-4]),1,"".26""),RC[-4]))))))))))))))))))))))))))"

或者您可以使用@ YowE3K的版本:

ActiveCell.FormulaR1C1 = "=IF(ISNUMBER(--RIGHT(RC[-4],1)),RC[-4],LEFT(RC[-4],LEN(R‌​C[-4])-1)&TEXT(CODE(RIGH‌​T(RC[-4],1))-64)/100‌​,""#.00""))"