You are here:

Excel/Excel formula into VB code

Question
Mr. Tom Ogilvy,

I made one Excel sheet where i need to use below formula in complete columns which increase the size of my work sheet, Is there why to put these formula in VB instead of formulas in excel to avoid such a big size of any file. It will be awesome if you guide me how to apply them in VB.

=IF(B16="","",IF(B16>=380,1,IF(AND(C15=1,B16>=362),1,0)))
=IF(C18="","",IF(C18=1,SUM(E17,1),""))
=IF(C16=1,((B16)*(B16))/25,"")
=IF(B18<0,AVERAGE(B17,B19),B18)
=IF(D18="","",IF(D18=1,(C18/\$F\$2)*C18,""))

Regards
Sarwar

Sarwar,

So you say you need formulas in an entire column - over 1 million cells.

You can certainly enter formulas by using VBA.

Range("D16").Formula = "=IF(B16="""","""",IF(B16>=380,1,IF(AND(C15=1,B16>=362),1,0)))"

will put your first formula in cell D16

You can put that formula all the way down column D with

After running that line of code, the last cell in column D had this formula

D1048576:  =IF(B1048576="","",IF(B1048576>=380,1,IF(AND(\$C\$15=1,B1048576>=362),1,0)))

Note that since C15 refers to the row before B16, I have fixed it as an absolute reference.  You can't start the formula in row 1 and refer to the cell above it.  I am just providing this as an example because you have not explained a lot of anomalies in your formulas.

But this enters the formula in the column.  You could then add code

Range("D:D").Formula = Range("D:D").Value

which will overwrite the formula with the values calculated.

so code like

Sub BuildFormula()
Range("D:D").Formula = "=IF(B1="""","""",IF(B1>=380,1,IF(AND(\$C\$15=1,B1>=362),1,0)))"
Range("D:D").Formula = Range("D:D").Value
End Sub

Another approach would be to write code that loops through column D and performs the same calculation as your formula

Sub EvaluateFormula()
Dim i As Long
For i = 2 To 1048576
If Not IsEmpty(Cells(i, "B")) Then
If Cells(i, "B") >= 380 Then
Cells(i, "D").Value = 1
ElseIf Cells(i, "B") >= 362 And Cells(i - 1, "C").Value = 1 Then
Cells(i, "D").Value = 1
Else
Cells(i, "D").Value = 0
End If
End If
Next i
End Sub

Here I have preserved you formula including a reference to the row prior so I start the code at row 2.

All this said, you may still end up with a large workbook if you put data in a lot of cells.

The code I provided is representative because you have said nothing about where your formulas need to go or the values that your formulas would produce need to go.

--
Regards,
Tom Ogilvy
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

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.