+ Reply to Thread
Results 1 to 3 of 3

Excel formula nested in Macro

Hybrid View

rdowney79 Excel formula nested in Macro 11-19-2013, 06:16 PM
mehmetcik Re: Excel formula nested in... 11-19-2013, 06:27 PM
rdowney79 Re: Excel formula nested in... 11-20-2013, 04:44 PM
  1. #1
    Registered User
    Join Date
    10-31-2013
    Location
    Golden, CO
    MS-Off Ver
    Excel 2010
    Posts
    43

    Excel formula nested in Macro

    Attempting to create a macro to insert a column (into column P), then paste the following formula '= Mid(O2, Find("-", O2) + 1, Find("^^", Substitute(O2, "-", "^^", 2)) - Find("-", O2) - 1)' into cell P2. This formula extracts the data between 2 hyphens. I then need this formula to be copied down an X number of rows, which can be dependant on the row count in Column B and pasted as values. I'm new to this and any help would be greatly appreciated.

    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("P1").Select
    ActiveCell.FormulaR1C1 = "Store Number"
    Selection.Font.Bold = True
    Range("P2").Select
    ActiveCell.FormulaR1C1 = Mid(O2, Find("-", O2) + 1, Find("^^", Substitute(O2, "-", "^^", 2)) - Find("-", O2) - 1)
    Range("P2").Select
    Selection.Copy
    Range("P2:P" & Range("B" & Rows.Count).End(3)(1).Row).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1").Select
    Thanks in advance,
    Ray

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Excel formula nested in Macro

    This is your code.

    
    Sub Macro13()
        LR = Range("A1").End(xlDown).Offset(1, 0).Row
        Columns("P:P").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("P2:P" & LR).FormulaR1C1 = _
            "= MID(RC[-1], FIND(""-"", RC[-1]) + 1, FIND(""^^"", SUBSTITUTE(RC[-1], ""-"", ""^^"", 2)) - FIND(""-"", RC[-1]) - 1)"
        Range("P2:P" & LR).Value = Range("P2:P" & LR).Value
    End Sub
    The first line tries to find your last row by looking down from A1 to find the first blank.
    Change this so it matches your data.

  3. #3
    Registered User
    Join Date
    10-31-2013
    Location
    Golden, CO
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Excel formula nested in Macro

    Issue Solved. Your original code pasted the formula into one too many lines, so I tweaked it a bit and it worked like a charm.

        Sub Macro13()
        LR = Range("B1").End(xlDown).Offset(1, 0).Row
        Columns("P:P").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("P1").Select
        ActiveCell.FormulaR1C1 = "Store Number"
        Selection.Font.Bold = True
        Range("P2:P" & Range("B" & Rows.Count).End(3)(1).Row).FormulaR1C1 = _
        "= MID(RC[-1], FIND(""-"", RC[-1]) + 1, FIND(""^^"", SUBSTITUTE(RC[-1], ""-"", ""^^"", 2)) - FIND(""-"", RC[-1]) - 1)"
        Range("P2:P" & LR).Value = Range("P2:P" & LR).Value
        Range("A1").Select
        End Sub
    Thank you mehmetcik

    Ray

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Excel nested formula?
    By SAMPCUK in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-07-2012, 07:13 PM
  2. Using Nested loops in VBA macro for excel
    By sfoy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2011, 02:54 AM
  3. Excel IF Nested Formula
    By hamburg18w in forum Excel General
    Replies: 2
    Last Post: 03-15-2011, 11:41 AM
  4. excel formula with nested if
    By nidabp in forum Excel General
    Replies: 11
    Last Post: 05-06-2010, 04:04 AM
  5. Nested IFs In Excel Formula
    By Sean04 in forum Excel General
    Replies: 2
    Last Post: 02-04-2010, 05:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1