• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to use extract strings & store in required columns using VB Macro

Vijaychitra

New Member
Hi,

I'm new to VB Macro.. Need help to solve

A
B
Raw DataName (Expected result)
John-143-AccountsJohn
Amy-102-ITAmy
Bill-158-MarketingBill
Jack-198-SalesJack
Henry-147-ITHenry

When used trim function in excel : TRIM(LEFT(A2,(FIND("-",A2,1)-1)) the desired result is obtained.

However, When used in the below format, error is thrown
Range("B2:B").Formula = "=TRIM(LEFT(A2,(FIND("-",A2,1)-1))"

I would like to know how this TRIM can be used in VBA or any other alternatives
 
Hi, within a VBA string each double quote must be doubled and the range address must be valid like the formula as well ‼​
Range("B2:B6").Formula = "=TRIM(LEFT(A2,(FIND(""-"",A2,1)-1)))"
 
Vijaychitra
One sample with VBA ...
Code:
Sub Do_It()
    With ActiveSheet
        b = 2
        Do
            .Cells(b, "B") = Trim(Left(Cells(b, "A"), WorksheetFunction.Find("-", .Cells(b, "A"), 1) - 1))
            b = b + 1
        Loop Until .Cells(b, "A") = Empty
    End With
End Sub
 
If only the result is necessary without any formula in cells of the active worksheet :​
Code:
Sub Demo1()
    With [A1].CurrentRegion.Rows
        Range("B2:B" & .Count).Value2 = Evaluate(Replace("IF({1},LEFT(A2:A#,SEARCH(""-"",A2:A#)-1))", "#", .Count))
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
Back
Top