Consulting

Results 1 to 5 of 5

Thread: Solved: Add Formula to Cell Using VBA

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    19
    Location

    Solved: Add Formula to Cell Using VBA

    Hello All,

    I am trying to add a formula in column B that will Trim() data in Column A.
    Below is the code I am using but I keep getting an object defined error


    Sub Format_DBMS_Dictionary_Description()
     
    Dim RngStart As Range
    Dim RngEnd As Range
     
    Set RngEnd = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
     
    Range("A1").Activate
     
    For Each RngStart In RngEnd
    RngStart.Offset(0, 1).Formula = "=Trim(A1)"
    Next RngStart
     
     
    End Sub

  2. #2
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Quote Originally Posted by bconner

    RngStart.Offset(0, 1).Formula = "=Trim(A1)"
    [/code]
    You need to change this line to

    [VBA]RngStart.Offset(0, 1).Formula = "=Trim(" & RngStart.Address(False, False, xlA1) & ")"[/VBA]

    But I didn't get any error with the code you provided.

    Best regards,

    Lincoln

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    19
    Location
    Lynnnow, thank you it worked perfectly

  4. #4
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Remember to mark your thread "Solved"

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No loop


    [vba]

    Sub Format_DBMS_Dictionary_Description()
    Range("B1").Resize(Cells(Rows.Count, 1).End(xlUp).Row).Formula = "=Trim(A1)"
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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