+ Reply to Thread
Results 1 to 7 of 7

VBA code to automate simple task

  1. #1
    Forum Contributor
    Join Date
    02-18-2013
    Location
    canada
    MS-Off Ver
    Excel 2003
    Posts
    112

    VBA code to automate simple task

    I would like to have a vba code that will trim string.

    Example:

    When I enter Benjamin in cell A1, "Ben" will appear in C1.

    Please help. Thank you.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA code to automate simple task

    You need to be more specific about your problem. To many of us, the solution to what you stated is just a simple formula of "=Left(A1,3)" placed in cell C1.
    Does this need to work for every row in that worksheet? And how do you determine the trim? Is it always only taking the first 3 letters?

  3. #3
    Forum Contributor
    Join Date
    02-18-2013
    Location
    canada
    MS-Off Ver
    Excel 2003
    Posts
    112

    Re: VBA code to automate simple task

    Thank you for your respond. I know how to use the Trim function and apply it on the cell. I just don't know how I can turn that into vba code.
    This is really what i want:

    once I entered the name "Benjamin" then hit ENTER key, VBA code should right away exeuted so that on C1 it will appear "Ben". This is just a sample for me to start my vba code. Once I got the code I will try to expand it myself. Thank you.

  4. #4
    Forum Contributor
    Join Date
    02-18-2013
    Location
    canada
    MS-Off Ver
    Excel 2003
    Posts
    112

    Re: VBA code to automate simple task

    Thank you for your respond. I know how to use the Trim function and apply it on the cell. I just don't know how I can turn that into vba code.
    This is really what i want:

    once I entered the name "Benjamin" then hit ENTER key, VBA code should right away exeuted so that on C1 it will appear "Ben". This is just a sample for me to start my vba code. Once I got the code I will try to expand it myself. Thank you.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: VBA code to automate simple task

    Hi, itselflearn,

    you might like to use the Worksheet_Change-event for this. Right click on the worksheet tab, choose View Code, paste the code into teh code window:
    Please Login or Register  to view this content.
    Code will only work on a single cell in Column A at a time, check if there is a value in Column A and abbreviate that to 3 characters. If the value from the cell is deleted, the value in COlumn C is deleted too.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA code to automate simple task

    So I'll assume you have got the worksheet_change event working already, you can try adding this:

    Please Login or Register  to view this content.
    But of course this is just simply hard coding the values, if you want to trim it to only the first 3 letters, use something like this:

    Please Login or Register  to view this content.
    Then again, this would only apply if you entered "Benjamin". But it's hard to suggest what you can do when you didn't really explain your whole scenario.

  7. #7
    Forum Contributor
    Join Date
    02-18-2013
    Location
    canada
    MS-Off Ver
    Excel 2003
    Posts
    112

    Re: VBA code to automate simple task

    Hi HaHoBe. Thank you very much for your code. It worked perfectly.

    Millz, I would like to thank you for your time. I know that you would have probably given me a different working code but i am sorry for not so clear on my post. I appreciated all your time and effort.

    Thank you all.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel Help - advice on how to automate this task
    By KingLeviathan in forum Excel General
    Replies: 2
    Last Post: 08-23-2010, 08:28 AM
  2. How to automate this task
    By [email protected] in forum Excel General
    Replies: 2
    Last Post: 07-13-2006, 07:45 PM
  3. [SOLVED] macro for excel, automate task
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2006, 10:45 AM
  4. can i automate task reminders in excel
    By shootist55 in forum Excel General
    Replies: 0
    Last Post: 05-02-2005, 08:06 PM
  5. Need a simple VBA code to automate a repetitive task.
    By madhu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2005, 07:06 AM

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