+ Reply to Thread
Results 1 to 6 of 6

Why execute this code EXCEL will shut down and restart

  1. #1
    Forum Contributor
    Join Date
    08-25-2020
    Location
    Taiwan
    MS-Off Ver
    MS 365+win 10(64bit)
    Posts
    201

    Why execute this code EXCEL will shut down and restart

    Please Login or Register  to view this content.
    Is it a code problem? Or is it caused by other reasons

    Any help would be appreciated!
    Attached Files Attached Files
    Last edited by metrostar; 02-26-2023 at 10:00 PM.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,645

    Re: Why execute this code EXCEL will shut down and restart

    You are using Worksheet_Change event, but you did not define which cell to trigger change to activate the code
    I can see your code trying to input sheetname into cell A55 (or I6). It should be placed in general module and run once only.
    Try below code. Hit F5 to run once.
    PHP Code: 
    Option Explicit
    Sub sheetnameInput
    ()
    Dim sheetName As Stringws As Worksheet
    For Each ws In Sheets ' loop through each sheet
        sheetName = ws.Name
        Select Case Left(sheetName, 1)
            Case "Q"
                ws.Range("A55").Value = Replace(sheetName, "Q_", "")
            Case "S"
                ws.Range("I6").Value = Replace(sheetName, "SVR_", "")
            Case Else
                ws.Range("A55").Value = ""
                ws.Range("I6").Value = ""
        End Select
    Next
    End Sub 
    Quang PT

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Why execute this code EXCEL will shut down and restart

    You need to disable the change event trigger...

    Please Login or Register  to view this content.
    Ben Van Johnson

  4. #4
    Forum Contributor
    Join Date
    08-25-2020
    Location
    Taiwan
    MS-Off Ver
    MS 365+win 10(64bit)
    Posts
    201

    Re: Why execute this code EXCEL will shut down and restart

    @protonLeah Perfect solution, you are great

    @bebo021999 , Except for the beginning of S and Q, the others remain the same, and where is the code changed? For example, the name of SHEET3 has no meaning at all

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Why execute this code EXCEL will shut down and restart


    Quote Originally Posted by metrostar View Post
    Is it a code problem ?
    O b v i o u s l y yes ‼
    'Cause when writing to any cell within this Change event you launch an infinite loop leading to a crash !
    So within this event procedure just desactivate the events - via EnableEvents - before writing to any cell and activate them before the end …

    Rather than creating such worksheet event within every worksheet remove all of them
    then use only this VBA workbook level event so to paste only to ThisWorkbook module :

    PHP Code: 
    Private Sub Workbook_SheetChange(ByVal Sh As ObjectByVal Target As Range)
        
    Application.EnableEvents False
        
    If Sh.Name Like "Q_*" Then Sh.[A55] = Replace(Sh.Name"Q_""") Else _
        
    If Sh.Name Like "SVR_*" Then Sh.[I6] = Replace(Sh.Name"SVR_""") Else Sh.[A55,I6].ClearContents
        Application
    .EnableEvents True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 02-26-2023 at 10:03 PM.

  6. #6
    Forum Contributor
    Join Date
    08-25-2020
    Location
    Taiwan
    MS-Off Ver
    MS 365+win 10(64bit)
    Posts
    201

    Re: Why execute this code EXCEL will shut down and restart

    @Marc L Perfect solution, you are great

+ 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. vba excel - asynchronous workaround...code to shut down all macros using ontime
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2017, 10:34 AM
  2. code to execute a keyboard shortcut or to execute a toolbar button
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-09-2015, 12:16 PM
  3. VBA Code to Auto Shut Down Sheet After 5 Minutes
    By Angnz in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-26-2014, 11:58 AM
  4. [SOLVED] Can Excel access and execute external code?
    By shadyferret in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-14-2012, 06:09 PM
  5. Does protecting a workbook shut off VBA code?
    By Stilla in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-12-2006, 03:40 AM
  6. Does protecting a workbook shut off VBA code?
    By Stilla in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-11-2006, 10:30 AM
  7. Does protecting a workbook shut off VBA code?
    By Stilla in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-11-2006, 10:25 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