+ Reply to Thread
Results 1 to 9 of 9

How to run a macro on sheet change with auto clear of the cell and not creating a loop

  1. #1
    Registered User
    Join Date
    03-15-2013
    Location
    Fond du Lac, WI
    MS-Off Ver
    Excel 2010
    Posts
    16

    How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Hello everyone, this one has stumped me pretty good.

    What i am doing is locking down an excel sheet and leaving one 1 open cell available. I then use a barcode scanner to scan in a barcode into that specific cell (B3). I have a chunck of VBA that will copy that cell data to sheet2 and put a time stamp with it for logging purposes. I also have a command button that does an auto print to print the new barcode and then it clears the data out of B3.

    What I am trying to do is not have to use a command button. When I scan in the barcode, i want it to print it automatically and then clear the data out of cell B3.

    I put my chunk of code that does the auto print and cell data clear into a Worksheet_Change but what happens is they fight eachother. When i scan in the data it acts as a sheet change, and then when the macro runs and i clear the data it also acts as a sheet change. So it just sits there in a loop of printing and clearing the cell data over and over and over and over again untill i have to manually close the excel sheet. It would never end!!!

    Any help with this would be greatly appreciated!!!!!!

  2. #2
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Hi,

    What you can do is On Worksheet Change check whether B3 is changed or not? If changed then only run the macro?


    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Plus also turn off Event handling. This stops the cell raising the change event when it is changed...

    Please Login or Register  to view this content.
    The Worksheet_Change event will not be fired....

    If you do use this, this make sure you include proper error handling in any non trivial code to make sure event handling is turned on again. If it's not, then Excel becomes unresponsive, to say the least... nothing will work.
    Last edited by cytop; 10-10-2014 at 07:23 AM.

  4. #4
    Registered User
    Join Date
    03-15-2013
    Location
    Fond du Lac, WI
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    If I turn off the event handling to stop the event change then the macro won't run. I need the sheet change to execute the copy of cell data, print auto, and clear the cell, but when the data is cleared it runs the macro again because it causes another sheet change and just keeps going. What will turning off the event handling do?

  5. #5
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Hi,

    You just try out what i suggested in my post#2 and check if it works out for you.

    Regards,
    Paresh J

  6. #6
    Registered User
    Join Date
    03-15-2013
    Location
    Fond du Lac, WI
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    So I should check to see if cell b3 has changed before I run my VBA?

  7. #7
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Hi,

    Yes, Inside Worksheet change event, you can write the following code:

    Please Login or Register  to view this content.
    Regards,
    Paresh J

  8. #8
    Registered User
    Join Date
    03-15-2013
    Location
    Fond du Lac, WI
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Alright I will try it out, thanks much!

  9. #9
    Registered User
    Join Date
    03-15-2013
    Location
    Fond du Lac, WI
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to run a macro on sheet change with auto clear of the cell and not creating a loop

    Well that little bit of VBA didnt work where it checks for B3 range. It just continues in a loop over and over again. Anybody else have any suggestions?

+ 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. Replies: 1
    Last Post: 06-21-2013, 04:05 AM
  2. exclude cell from sheet clear macro
    By cowboy560 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2012, 01:52 PM
  3. [SOLVED] The VBA code to auto-clear dependent drop down selection when parent cell change.
    By Hotgirl in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2012, 07:48 PM
  4. [SOLVED] Need a macro for creating payslip using auto vlookup and inserting and renaming sheet
    By srinivasan1965 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2012, 01:17 AM
  5. [SOLVED] Need Auto Invoicing and pickup details from the main sheet. Creating Auto Invoice.
    By kulins in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-18-2012, 09:22 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