You are here:

Excel/General code question

Advertisement


Question
HI Tom

Could you explain why the formulas that are placed into cells G1, H1, I1, J1 and K1 by this code are copied all the way down to G400, H400, I400 J400 and K400 respectively?  I thought that they would only be placed in G1, H1, I1, J1 and K1.

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$400)"
sFormH = "=COUNTIF($E$2:$E$400,"">4"")"
sFormI = "=COUNTIF($F$2:$F$400,"">-1"")"
sFormJ = "=$H$1/$G$1"
sFormK = "=$I$1/$G$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("J1").Formula = sFormJ
.Range("K1").Formula = sFormK
   
 End With
Next
End Sub

Thanks in advance

Chris Mitchell

Answer
Christopher Mitchell,


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$400)"
sFormH = "=COUNTIF($E$2:$E$400,"">4"")"
sFormI = "=COUNTIF($F$2:$F$400,"">-1"")"
sFormJ = "=$H$1/$G$1"
sFormK = "=$I$1/$G$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("J1").Formula = sFormJ
.Range("K1").Formula = sFormK

End With
Next
End Sub

worked fine for me.

--
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.