Excel/Altering VBA

Advertisement


Question
Hi Tom

I have altered a piece of code that you worte for me so that it includes five more formulas than the original code.

When I paste the code into a module the following two lines are highlighted in red.  Whe n the code is run a message appears that says there is a syntax error.

could you tell me where I have gone wrong?

The two offending lines are:

sFormH = "=COUNTIF($E$2:$E$7,">4")"
sFormI = "=COUNTIF($F$2:$F$7,">-1")"


The complete code is:

Sub WriteFormula()
Dim i As Long, sFormE As String, sFormF As String
sFormE = "=IFERROR(VLOOKUP(D2,Grade_Conv,2,FALSE),"""")"
sFormF = "=IFERROR(VLOOKUP(D2,Grade_Conv,2,FALSE)-VLOOKUP($C2,Grade_Conv,2,FALSE),"""")"
sFormG = "=COUNTA($D$2:$D$6)"
sFormH = "=COUNTIF($E$2:$E$7,">4")"
sFormI = "=COUNTIF($F$2:$F$7,">-1")"
sFormJ = "=$I$1/$H$1"
sFormK = "=$J$1/$H$1"

For i = 1 To 7
 With Worksheets("Sheet" & i)
   .Range("E2:E400").Formula = sFormE
   .Range("F2:F400").Formula = sFormF
.Range("G1").Formula = sFormG
.Range("H1").Formula = sFormH
.Range("I1").Formula = sFormI
.Range("J2:F400").Formula = sFormJ
.Range("K2:F400").Formula = sFormK
   
  
   
 End With
Next
End Sub


The original code was:

Sub WriteFormula()
Dim i As Long, sFormE As String, sFormF As String
sFormE = "=IFERROR(VLOOKUP(D2,Grade_Conv,2,FALSE),"""")"
sFormF = "=IFERROR(VLOOKUP(D2,Grade_Conv,2,FALSE)-VLOOKUP($C2,Grade_Conv,2,FALSE),"""")"

For i = 1 To 7
 With Worksheets("Sheet" & i)
   .Range("E2:E400").Formula = sFormE
   .Range("F2:F400").Formula = sFormF
   
 End With
Next
End Sub


Thanks in advance

Chris

Answer
Christopher Mitchell,

sFormH = "=COUNTIF($E$2:$E$7,">4")"
sFormI = "=COUNTIF($F$2:$F$7,">-1")"


should be

sFormH = "=COUNTIF($E$2:$E$7,"">4"")"
sFormI = "=COUNTIF($F$2:$F$7,"">-1"")"


when you use a double quote within a string that is bounded by double quotes, you need to double each internal double quote.   Otherwise, it looks like the terminating double quote for the preceding double quote and you get the error because your double quoted don't line up properly.

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.