Consulting

Results 1 to 12 of 12

Thread: Solved: Where can I find a VBA guide?

  1. #1

    Solved: Where can I find a VBA guide?

    I need to create a VBA to calculate 13 columns with 13 different formulas.

    My problem is I don't know how to modify my formulas to VBA.

    Where can I find at least a guide to help me turn my cell formulas to VBA codes?

    Example:
    AG8=IF(VLOOKUP(A11,Category!A$1:B$65536,2,0)=1,1,IF(L11=0,1.4,IF(L11<=L$6,1 ,1.4)))

    Thanks!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is as simple as

    [vba]

    Range("AG8").Formula = "=IF(VLOOKUP(A11,Category!A$1:B$65536,2,0)=1,1,IF(L11=0,1.4,IF(L11<=L$6 ,1 ,1.4)))"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    What if I want to calculate column AG8:G1000?

    range(AG8:G1000)?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do you mean AG1000?

    What would the formula be in AG9?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    yes, i mean AG1000.

    AG8 to AG1000 has the same formula.

  6. #6
    I tried 1 of my formula, but it doesn't work

  7. #7
    Actually, the attached template is the one I'm working on right now.

    If I can only have a guide to start the VBA code for this file.

    Thanks in advance!

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Couple of things

    1. Put the VBA for the Sub "calc" in a standard module (VBE, Insert, Module). You had it in worksheet code space

    2. Good idea to use Option Explicit and always define your variables

    3. The humongous formula line ...

    [vba]
    Range("BG8").Formula =
    [/vba]

    was red because there was a syntax error -- when you use strings within a string and you need the 'inner' string to have it's own quotes (like for your WS formula) you need to double them


    4. I broke the formula line into pieces and built it since it was so long


    [vba]
    Option Explicit
    Sub calc()
    Dim sFormula As String
    sFormula = "=IF(OR(AND(CX8=""T"",CY8=""T"",CZ8=""T"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""T"",CY8=""T"",CZ8=""F"",DA8=""T""),"

    sFormula = sFormula & "AND(CX8=""T"",CY8=""T"",CZ8=""T"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""T"",CY8=""T"",CZ8=""F"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""T"",CY8=""F"",CZ8=""T"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""T"",CZ8=""T"",DA8=""T"")),""G"","
    sFormula = sFormula & "(IF(OR(AND(CX8=""T"",CY8=""F"",CZ8=""F"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""T"",CZ8=""F"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""T"",CY8=""F"",CZ8=""T"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""F"",CZ8=""T"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""T"",CZ8=""T"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""F"",CZ8=""T"",DA8=""F"")),""Y"","
    sFormula = sFormula & "(IF(OR(AND(CX8=""F"",CY8=""F"",CZ8=""F"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""F"",CZ8=""F"",DA8=""T""),"
    sFormula = sFormula & "AND(CX8=""T"",CY8=""F"",CZ8=""F"",DA8=""F""),"
    sFormula = sFormula & "AND(CX8=""F"",CY8=""T"",CZ8=""F"",DA8=""F"")),""R"")))))"
    Range("BG8").Formula = sFormula

    End Sub
    [/vba]

    Paul

  9. #9
    Thanks for the reply!

    follow up question.

    This is only for Col/row BG8, what if I also want the formula to calculate in BG9 to BG1000?

    will I do
     
    range("BG8:BG1000").Formula =
    instead of
    range("BG8")

  10. #10
    hey, i got it!

    It automatically calculate when I did the BG8:BG1000!

    Thanks a million!!!! Mwahhhhhh!!!

  11. #11
    sorry, I have another question...

    what if I want to calculate BG8 until the last row? not exactly BG1000.

  12. #12
    You could try this if Column DD is a relative column for your calculations.
    If not, change DD to a column you want to use and has a bearing on the calculations. Make sure to change the column offset (here it is -49) also.
    Try it on a backup file first please.

    Range("DD8", Range("DD" & Rows.Count).End(xlUp)).Offset(, -49).Formula = sFormula
    Hope it works.

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •