+ Reply to Thread
Results 1 to 10 of 10

VBA code clean up help

  1. #1
    Registered User
    Join Date
    02-15-2012
    Location
    entsRif confftent
    MS-Off Ver
    Excel 2007
    Posts
    3

    VBA code clean up help

    Hi, I am new to VBA. Just learning a few things. I wrote a quick macro for a button that copies data in a cell and records it at the bottom of a separate column.

    Three things:

    Would any experienced VBA programmer be able to look at this code and help me clean it up?
    And, is there any way to unselect a cell? After the macro runs, there is a border on the original cell marking at as "selected". I want to get rid of it.
    Is it possible to include a code to save the workbook automatically (just save, not save as.).

    Thanks.

    Macro written below:
    Please Login or Register  to view this content.
    Last edited by recon427; 02-15-2012 at 03:07 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA code clean up help

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA code clean up help

    Hello recon427,

    there is a border on the original cell marking at as "selected". I want to get rid of it.
    By the looks of things, I would imagine that you mean there is a border around the Macro "Selected Area" and not a specific Cell.

    If you want the border removed from the Macro "Selected Area" afterwards, simply add a line at the bottom of your code to activate another Cell: i.e.

    Sheet2.Range("A1").Activate
    or whatever the Sheet name is, or the Cell is, which you want to return to.

    Please Login or Register  to view this content.
    Hope it helps!
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: VBA code clean up help

    or
    Please Login or Register  to view this content.
    regards, J

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA code clean up help

    @john55,

    Yeah you're right. I completely forgot about the Save part as well!

  6. #6
    Registered User
    Join Date
    02-15-2012
    Location
    entsRif confftent
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code clean up help

    Winon and john55, thank you for your help! This is great

  7. #7
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: VBA code clean up help

    glad it helped you!

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA code clean up help

    Try
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-15-2012
    Location
    entsRif confftent
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code clean up help

    Hi royUK, that is great. Sure does make things look nice and simple! Really appreciate this!

  10. #10
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: VBA code clean up help

    As you have learned from this thread, there are many ways to achieve similar results.

    Because I learned (and will continue to learn) from here and like to help, I would offer these additional notes.

    Range().Select is great and I find most useful if I am about to use that selection to perform another task. If I am just jumping around or wanting general navigation, I prefer: (Assumes you would like to go to Range("A10"):
    Please Login or Register  to view this content.
    where "True" represents a scroll such that the selected cell will be the upper left most cell on the visible sheet. "False" would leave the cells position as is regardless of where it is on the screen.

    If you were to create a named range of "A10", say, MyRange, both of the following would accomplish the same as the above:
    Please Login or Register  to view this content.
    &
    Please Login or Register  to view this content.
    Building on John's code, you could bundle all three close out tasks nicely within a With End With construct. Something like:

    Please Login or Register  to view this content.
    If you stay with it, you will see that most guru's advise against .activate and .select unless absolutely necessary. There are reasons that you can google ad nauseum, but that's why you appreciated the simplicity of Roy's offering when you saw it. It does not select, copy, or paste like the macro recorder did for you initially. Much cleaner. Much faster.
    Last edited by AlvaroSiza; 02-17-2012 at 01:27 AM.

+ 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