+ Reply to Thread
Results 1 to 5 of 5

Using Text Formulas in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Using Text Formulas in VBA

    Hi All,

    I've stumbled into an issue while trying to use a text formula in VBA to set the value of certain cells. Hypothetically I could insert a formula into a cell then paste as value to get the same result, however I am working with a huge document and trying to write the most efficient code possible.

    Here is the code I've written, with the understanding that the "v = ..." part of the code isn't correct, but I wanted to show the formula I need to use there. In that formula, the cell "B2" should be dynamic such that it is "B"&i.

    Please let me know if you have any solutions or thoughts.


    Thanks in advance.

    Sub Test()
    
    
    
    Dim R As Long, v As String
    
    R = Range("B2").End(xlDown).Row
    
    
    For i = 2 To R
    
    v = IF(COUNT(SEARCH({"ib",";iber","iiber","iber","ibir"},B2)),"Cat1",IF(ISNUMBER(SEARCH("Unavailable",B2)),"Cat2","Cat3"))
    
    
    Range("G" & i).Value = v
    
    
    Next i
    
    
    
    End Sub

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Using Text Formulas in VBA

    bungeejumper2,

    Using 1,004,287 rows as a test sample size, this code completed in roughly 15 seconds:
    Sub tgr_v1()
        
        Dim lCalc As XlCalculation
        
        With Application
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        With Intersect(ActiveSheet.UsedRange.EntireRow, Columns("G"))
            .Formula = "=IF(COUNT(SEARCH({""ib"","";iber"",""iiber"",""iber"",""ibir""},B" & .Row & ")),""Cat1"",IF(ISNUMBER(SEARCH(""Unavailable"",B" & .Row & ")),""Cat2"",""Cat3""))"
            .Value = .Value
        End With
        
        With Application
            .Calculation = lCalc
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Using Text Formulas in VBA

    You need quotes around the string and you need to double up the quotes in the formula.

    Oh, and you need an = in front of the formula.
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: Using Text Formulas in VBA

    Tiger - That .UsedRange function is awesome. Can't believe I had not seen that yet!

  5. #5
    Registered User
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Using Text Formulas in VBA

    Thank you Tiger. That works perfect!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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