+ Reply to Thread
Results 1 to 5 of 5

How to enter blank spaces before and after text in a particular cell

  1. #1
    Forum Contributor
    Join Date
    01-26-2012
    Location
    Mumbai
    MS-Off Ver
    Excel 2013
    Posts
    154

    How to enter blank spaces before and after text in a particular cell

    This is a quickie for most ,i guess !

    Basically i want to insert blank text after and before text in the column B ..........i am trying to do


    For i = 1 To noofrows

    Cells(i, B).Value = X
    Cells(i, B).Value = vbLf & X & vbLf


    Next i


    I fail to understand what i am doing wrong ...please see the excel sheet attached ... i had 2 purposes of the macro ,the first i have already achieved but i am failing to insert blank spaces before and after the text ...here my code ,

    Please Login or Register  to view this content.
    Will appreciate ,if someone can help ,

    Regards,

    Amlan
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: How to enter blank spaces before and after text in a particular cell

    Just a guess here, but shouldn't it be:

    X = Cells(i, B).Value

    then:

    Cells(i, B).Value = vbLf & X & vbLf

    Also:

    Dim i As Long
    Dim X as Variant

  3. #3
    Forum Contributor
    Join Date
    01-26-2012
    Location
    Mumbai
    MS-Off Ver
    Excel 2013
    Posts
    154

    Re: How to enter blank spaces before and after text in a particular cell

    Thanks for the response ,that doesn't work as well ,can anyone help please?I get object
    defined error at line -> X = Cells(i, B).Value



    Please Login or Register  to view this content.

    Apparently i guee there must be some other way of adding a space before and after the text ,i fail to understand how,i bet the solution will be so easy that i will feel like killing myself ,

    Anyone has any clues ?


    Regards ,

    Amlan Dutta

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: How to enter blank spaces before and after text in a particular cell

    How about?

    Please Login or Register  to view this content.
    I overlooked that you didn't have "B" in quotes.
    And there is no need to loop the same area twice.
    And there is no need to use selection.
    See if the above code works for you.

    P.S.

    You might want to change:
    Please Login or Register  to view this content.
    to:

    Please Login or Register  to view this content.
    Depending on what your data looks like.
    Last edited by StevenM; 06-13-2012 at 03:15 PM. Reason: P.S.

  5. #5
    Forum Contributor
    Join Date
    01-26-2012
    Location
    Mumbai
    MS-Off Ver
    Excel 2013
    Posts
    154

    Re: How to enter blank spaces before and after text in a particular cell

    Steve,thanks a tonne ,i have marked the thread as solved !


    i kept last row same and for data flow cannot end "If "like shown above .....But dude ,i owe you billion thanks for showing me where i was wrong ,i fill like killing myself for missing the " B".value .....i's the basics ,i sometimes make the silliest of mistakes ,

    Also you are right ,i shouldn't be using 2 for loop ,that way the execution time gets doubled ,i was wondering how to do it ,after seeing your code ,i got a idea and placed it after End if rather than before -It's because i dont want the "B".value to just work for that if condition for inserting the blank spaces and want it to happen for every value in B ........


    Finally million thanks to you ,the trailing code works ,
    Please Login or Register  to view this content.



    I attach the output file with a button called highlight which activates the Module with subroutine highlight wherein the code resides ,

    Thanks a tonne ,Steve,

    Humble regards ,

    Amlan Dutta
    Attached Files Attached Files

+ 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