Consulting

Results 1 to 5 of 5

Thread: Solved: How to Write Formulas in VBA?

  1. #1

    Solved: How to Write Formulas in VBA?

    Good morning all,

    In column A starting from A6 there are some values, I need in coumn B

    Starting from B6 the following formula:
    "=IF(A6<>"",$A$2&A6,"")"

    Starting from C6 the formula:
    "=IF(A6<>"",IF(ISNA(VLOOKUP(LEFT(A6,8),Data!A:H,2,0)),"Not Available",VLOOKUP(LEFT(A6,8),Data!A:H,2,0)),"")"

    Starting from D6 the formula:
    "=IF(A6<>"",IF(ISNA(VLOOKUP(LEFT(A6,8),Data!A:H,3,0)),"Not Available",VLOOKUP(LEFT(A6,8),Data!A:H,3,0)),"")"

    and fill down to last used cell in column A

    Please help, Thanks.

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Use a string variable for the double-quotes:

    [VBA]Sub dural()
    Dim N As Long, dq As String, ddq As String
    N = Cells(Rows.Count, "A").End(xlUp).Row
    dq = Chr(34)
    ddq = dq & dq
    Cells(6, 2).Formula = "=IF(A6<>" & ddq & ",$A$2 & A6," & ddq & ")"
    Cells(6, 2).Copy Range("B7:B" & N)
    End Sub
    [/VBA]
    Have a Great Day!

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Normally, I like to build a formula string as Gary showed.

    A rule-of-thumb, add an extra quote for each quote in a string. e.g.
    [VBA]Sub ken()
    Range("B6").Formula = "=IF(A6<>"""",$A$2&A6,"""")"
    Range("C6").Formula = "=IF(A6<>"""",IF(ISNA(VLOOKUP(LEFT(A6,8),Data!A:H,2,0)),""Not Available"",VLOOKUP(LEFT(A6,8),Data!A:H,2,0)),"""")"
    Range("D6").Formula = "=IF(A6<>"""",IF(ISNA(VLOOKUP(LEFT(A6,8),Data!A:H,3,0)),""Not Available"",VLOOKUP(LEFT(A6,8),Data!A:H,3,0)),"""")"
    Range("B66").AutoFill Destination:=Range("B6" & Range("A" & Rows.Count).End(xlUp).Row), Type:=xlFillDefault
    End Sub[/VBA]

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Recording a macro is a good way to get a formula into the R1C1 format and it can be transported to other ranges as needed.

  5. #5
    Thanks a lot Kenneth, it was really useful...worked good.
    Thanks a lot GarysStudent and mikerickson.

Posting Permissions

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