Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Converting Excel Formulas to a VBA macro or module

Posted on 2008-10-29
4
517 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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