Solved

Converting Excel Formulas to a VBA macro or module

Posted on 2008-10-29
4
513 Views
Last Modified: 2013-11-25
Experts

I am just starting down the road in terms of VBA and macros/modules, I do a lot of data manipulation with very large spreadsheets.  The spreadsheets contain many complex formulas, it is obviously easier to manipulate, sort, etc an excel file without formulas, actually my files crash often when I sort with these formulas present.

So my question:  In the workbook attached the first several columns with the green header is static meaning no formulas, the rest of the spreadsheet in yellow is all populated via the use of formulas which you can see in the first few rows of data.  I would like to have a macro or module or some type of vba script that would analyze my data and populate the excel file in the yellow areas with "VALUES" only and not formulas.

Not sure how to do it but if I could get an example in VBA for a few of the columns and how to insert it in excel, run it etc I could probably figure out the rest.

Formula snipet and file attached

Thanks
=IF(J3<>"Weekend","Weekday",IF($Z3="","No NADM",IF(OR(AA3<=$L3,$O3<=Z3,AA3-Z3<0.084),"NA Does Not Apply",IF(AND((TEXT(WEEKDAY(L3),"ddd"))="Sat",Z3<=($L3+0.084),AA3>=((INT($L3)+2+'Ref Data'!$B$2))),"Yes",IF(AND((TEXT(WEEKDAY(L3),"ddd"))="Sun",Z3<=($L3+0.084),AA3>=((INT($L3)+1+'Ref Data'!$B$2))),"Yes","No")))))

Open in new window

OOH-Example-Data.xls
0
Comment
Question by:jclem1
  • 3
4 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 22834051
The easiest might be to just use Vba to copy the formula to the range, then do a paste values so you only show the result.

That's something you could do by recording a macro.

Thomas
0
 
LVL 1

Author Comment

by:jclem1
ID: 22834866
I guess in simplest terms I would like to see a VBA example of a formula in a column and then of course how to copy it down etc.
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 22835429
This is an example (I limited to columns AN to AR)


Sub Macro1()
 
Dim lastRow As Long
 
lastRow = Range("A65536").End(xlUp).Row
 
    Range("AN3:AN" & lastRow).FormulaR1C1 = "=IF(RC[-21]<=4,""Yes"",""No"")"
    Range("AO3:AO" & lastRow).FormulaR1C1 = _
        "=IF(OR(RC[3]=""No"",RC[-31]=""Overnight"",RC[-31]=""Evening"",RC[-31]=""weekend""),""Yes"",""No"")"
    Range("AP3:AP" & lastRow).FormulaR1C1 = _
        "=IF(RC[1]="""",IF(AND(RC[9]=""No"",RC[10]<>""yes"",RC[11]<>""Yes"",RC[12]<>""Yes"",RC[13]<>""Yes""),""MISSED: Weekend"",IF(OR(RC[15]='Ref Data'!R22C1,RC[16]='Ref Data'!R22C1,RC[17]='Ref Data'!R22C1,RC[18]='Ref Data'!R22C1,RC[19]='Ref Data'!R22C1,RC[20]='Ref Data'!R22C1,RC[21]='Ref Data'!R22C1, RC[22]='Ref Data'!R22C1,RC[23]='Ref Data'!R22C1,RC[24]='Ref Data'!R22C1,),""Access Not Avail"",IF(RC[-2]=""Yes"",""MET Other"",""MISSED""))),RC[1])"
    Range("AQ3:AQ" & lastRow).FormulaR1C1 = _
        "=IF(RC[8]=""Yes"",""MET: HDD & DSP Weekend"",IF(RC[1]=""Yes"","" Not OOH"",IF(RC[2]=""yes"",""MET:  HDD & DSP Same OOH Period"",IF(RC[3]=""Yes"",""MET: HDD Day & DSP Same Evening"",IF(RC[4]=""Yes"",""MET:  HDD Day and DSP Next Day AM OOH"",IF(RC[6]=""Yes"",""MET: HDD > 0400 DSP 1st Job"",IF(OR(RC[9]=""yes"", RC[10]=""Yes"", RC[11]=""Yes"", RC[12]=""Yes""),""Weekend Acc Not Avail"", """")))))))"
    Range("AR3:AR" & lastRow).FormulaR1C1 = _
        "=IF(INT(RC[-32])<>(INT(RC[-30])),""No"",IF(AND(AND(MOD(RC[-32],1)>='Ref Data'!R6C2,MOD(RC[-32],1)<='Ref Data'!R9C2),AND(MOD(RC[-30],1)>='Ref Data'!R6C2,MOD(RC[-30],1)<='Ref Data'!R9C2)),""Yes"",""No""))"
    
    Range("AN3:AR" & lastRow).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 
End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:nutsch
ID: 22843101
Thanks for the grade. Glad to help.

Thomas
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question