Consulting

Results 1 to 5 of 5

Thread: VBA - Use variable from InputBox in formula?

  1. #1
    VBAX Newbie
    Joined
    Nov 2018
    Location
    Norway
    Posts
    3
    Location

    VBA - Use variable from InputBox in formula?

    Hi!
    I've a macro where I need to search for a string, given by InputBox and split the result in to two columns.
    How may I use the string from the InputBox, in the forumula?

    If I use the following, the code will not search for anything. If I replace the sReturn-variable in the forumla with a fixed string (eq. ""apple"") it will act as intended

    sReturn = InputBox("What to search for?")
    
    
    'Insert two new columns with format "General" to the right of the selected cell/column
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 1"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 2"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    
    
    Dim i, LastRow
      LastRow = Range("A" & Rows.Count).End(xlUp).Row
      For i = 1 To LastRow - 1
      ActiveCell(i + 1, 0).FormulaR1C1 = _
            "=IF(ISNUMBER(FIND(sReturn,RC[-1])),LEFT(RC[-1],FIND(sReturn,RC[-1])-1),"""")"
      ActiveCell(i + 1, 1).FormulaR1C1 = _
            "=IF(ISNUMBER(FIND(sReturn,RC[-2])),SUBSTITUTE(RC[-2],RC[-1]&sReturn,""""),"""")"

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    You need to construct the formulas a little differently:

    This is what you end with the way you have it

    "=IF(ISNUMBER(FIND(sReturn,RC[-1])),LEFT(RC[-1],FIND(sReturn,RC[-1])-1),"")"
    In VBA, you use 2 double quotes embed a double quote as a character in a string or use Chr(34)


    BTW, since I find building formulas a little finicky, I like to build the formula using a separate, testable variable (like sFormula below). It just seems easier to me

    Option Explicit
    Sub test()
        Dim sReturn As String, sFormula As String
        Dim i As Long
        Range("C3").Select
        i = 0
        sReturn = "ABCDEF"
        
        'wrong
        sFormula = "=IF(ISNUMBER(FIND(sReturn,RC[-1])),LEFT(RC[-1],FIND(sReturn,RC[-1])-1),"""")"
        Debug.Print 1 & " -- " & sFormula
        MsgBox sFormula
        
        'right (I think) :-)
        sFormula = "=IF(ISNUMBER(FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])),LEFT(RC[-1],FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])-1),"""")"
        Debug.Print 2 & " -- " & sFormula
        MsgBox sFormula
        
        
        'right (I think) :-)
        sFormula = "=IF(ISNUMBER(FIND(""" & sReturn & """,RC[-1])),LEFT(RC[-1],FIND(""" & sReturn & """,RC[-1])-1),"""")"
        Debug.Print 3 & " -- " & sFormula
        MsgBox sFormula
        
    End Sub


    Debug output:


    1 -- =IF(ISNUMBER(FIND(sReturn,RC[-1])),LEFT(RC[-1],FIND(sReturn,RC[-1])-1),"")
    2 -- =IF(ISNUMBER(FIND("ABCDEF",RC[-1])),LEFT(RC[-1],FIND("ABCDEF",RC[-1])-1),"")
    3 -- =IF(ISNUMBER(FIND("ABCDEF",RC[-1])),LEFT(RC[-1],FIND("ABCDEF",RC[-1])-1),"")
    Last edited by Paul_Hossler; 11-01-2018 at 01:19 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Nov 2018
    Location
    Norway
    Posts
    3
    Location
    Quote Originally Posted by Paul_Hossler View Post
    ...
    BTW, since I find building formulas a little finicky, I like to build the formula using a separate, testable variable (like sFormula below). It just seems easier to me
    Thanks Paul!
    Sadly the resulting MsgBox'es are blank. When I run the code in debug I get the String I put in, when holding the mouse both over the "Chr(34)" and the "sReturn".
    Here is the complete code I'm using, after your input:

    Sub SearchAndSplitt()'
    ' Search for given String and print tekst before and after in two new columns
    '
    ActiveWorkbook.ActiveSheet.Select    'Executes in the open workbookvba and "insert a new column"
    ActiveWindow.DisplayFormulas = False ' Show calculated value of formulas
    '
    Dim sReturn As String, sFormula As String
    'Dim i As Long
    sReturn = InputBox("What to search for?")
    
    
    'Insert two new columns with format "General" to the right of the selected cell/column
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 1"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 2"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    
    
    Dim i As Long, LastRow
      LastRow = Range("A" & Rows.Count).End(xlUp).Row
      For i = 1 To LastRow - 1
      ActiveCell(i + 1, 0).FormulaR1C1 = _
            sFormula = "=IF(ISNUMBER(FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])),LEFT(RC[-1],FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])-1),"""")"
            Debug.Print 1 & " -- " & sFormula
            MsgBox sFormula
    
    
      ActiveCell(i + 1, 1).FormulaR1C1 = _
            sFormula = "=IF(ISNUMBER(FIND(""" & sReturn & """,RC[-1])),LEFT(RC[-1],FIND(""" & sReturn & """,RC[-1])-1),"""")"
            Debug.Print 2 & " -- " & sFormula
            MsgBox sFormula
      Next i
    
    
    'Selects the two new columns, copy the results and paste only the values
      Selection.Offset(0, -1).Resize(Selection.Rows.Count + 0, _
      Selection.Columns.Count + 1).Select
      Selection.Copy
      Selection.PasteSpecial Paste:=xlPasteValues
      
    'Cleanup
      Range("A1").Select
      Application.CutCopyMode = False
    End Sub

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try it this way

        sFormula = "=IF(ISNUMBER(FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])),LEFT(RC[-1],FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])-1),"""")"
    
    
        ActiveCell(i + 1, 0).FormulaR1C1 = sFormula
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    Nov 2018
    Location
    Norway
    Posts
    3
    Location
    Fantastic Paul!
    This did the trick

    Here is the full working code, if anyone need it:
    Sub SearchAndSplitt()'
    ' Search for given String and print tekst before and after in two new columns
    '
    ActiveWorkbook.ActiveSheet.Select    'Executes in the open workbookvba and "insert a new column"
    ActiveWindow.DisplayFormulas = False ' Show calculated value of formulas
    '
    Dim sReturn As String, sFormula As String
    'Dim i As Long
    sReturn = InputBox("What to search for?")
    
    
    'Insert two new columns with format "General" to the right of the selected cell/column
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 1"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    ActiveCell.EntireColumn.Offset(0, 1).Insert
    ActiveCell.EntireColumn.Offset(0, 1).Select
    ActiveCell.Offset(0, 0).FormulaR1C1 = "Part 2"
    ActiveCell.EntireColumn.Offset(0, 1).NumberFormat = "General"
    '
    
    
    Dim i As Long, LastRow
      LastRow = Range("A" & Rows.Count).End(xlUp).Row
      For i = 1 To LastRow - 1
    
    
    sFormula = "=IF(ISNUMBER(FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])),LEFT(RC[-1],FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-1])-1),"""")"
    ActiveCell(i + 1, 0).FormulaR1C1 = sFormula
    'Debug.Print 1 & " -- " & sFormula
    'MsgBox sFormula
    
    
    sFormula = "=IF(ISNUMBER(FIND(" & Chr(34) & sReturn & Chr(34) & ",RC[-2])),SUBSTITUTE(RC[-2],RC[-1]&" & Chr(34) & sReturn & Chr(34) & ",""""),"""")"
    ActiveCell(i + 1, 1).FormulaR1C1 = sFormula
    'Debug.Print 2 & " -- " & sFormula
    'MsgBox sFormula
    Next i
    
    
    'Selects the two new columns, copy the results and paste only the values
      Selection.Offset(0, -1).Resize(Selection.Rows.Count + 0, _
      Selection.Columns.Count + 1).Select
      Selection.Copy
      Selection.PasteSpecial Paste:=xlPasteValues
      
    'Cleanup
      Range("A1").Select
      Application.CutCopyMode = False
    End Sub

Tags for this Thread

Posting Permissions

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