简体   繁体   中英

VBA Excel: Compile error: Syntax error

Sub LoopCertain() 'Excel VBA to loop through only include sheets (1-3)

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets ' Start of the VBA loop
  Select Case sh.Name
    Case Is = "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", "D26", "D27", "D28", "D29", "D30", "D31"
     sh.[D15].Value = "=(4-(D16+D17+D18+D19))+(SUMIF(J27:J38,"Operational",N27:N38))"
     sh.[D21].Value = "=4-(SUM(D16:D20))"
     sh.[D23].Value = "=D15/4"
  End Select
Next sh

End Sub

The error is on the line listed below and is caused by "Operational", I assume because it sees the " and tries to end there, how do I make it so it takes "operational" literally as is and continues past it?

sh.[D15].Value = "=(4-(D16+D17+D18+D19))+(SUMIF(J27:J38,"Operational",N27:N38))"

You can either double the quotation marks or

sh.[D15].Value = "=(4-(D16+D17+D18+D19))+(SUMIF(J27:J38,""Operational"",N27:N38))"

use CHR(34) .

sh.[D15].Value = "=(4-(D16+D17+D18+D19))+(SUMIF(J27:J38," + CHR(34) + "Operational" + CHR(34)",N27:N38))"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM