+ Reply to Thread
Results 1 to 5 of 5

Excel crashes when I change code

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2014
    Location
    Philadelphia, PA
    MS-Off Ver
    2010
    Posts
    3

    Excel crashes when I change code

    I am trying to fix an old macro that someone else made a while ago. There are 2 drop down lists that are copied and pasted to a cell whenever data is imported using this macro. It seemed like everything was working fine because the data was being imported and there have not been any error messages. However, I recently noticed that the drop down list for "BadData" does not get copied and pasted to it's respective cell like it should. When I looked through the code, I noticed that the code for "Run Type" and "BadData" were identical except for the following two lines:

    If Range("C" & CStr(startRow)).Value Like "" Then       ' For "Run Type"
    If Range("F" & CStr(startRow)).Select Like "" Then      ' For "BadData"
    The drop down list for "Run Type" gets copied and pasted just fine. I am new to VBA so I just decided to try and fix the "BadData" code by simply changing

    If Range("F" & CStr(startRow)).Select Like "" Then      ' For "BadData"
    to

    If Range("F" & CStr(startRow)).Value Like "" Then      ' For "BadData"
    It seems like it should work because it works for "Run Type" but whenever I try to run the macro, Excel just crashes and asks me if I want to restart Excel and attempt to retrieve my workbook. Usually when I screw up a line of code, Excel stops the macro, gives me an error code and asks me if I want to Debug, and then takes me to the line of code that is causing trouble. I am not sure why Excel keeps crashing. Is it because I did something wrong with the code or does Excel just have some sort of bug that I am running into?

    Here is the old code:


        'Copying the drop down list to the columns 'Run Type' and 'BadData'
        
        Range("B" & CStr(startRow)).Value = CStr(subTestNumber) & subTestID
        Range("B" & CStr(startRow)).Font.ColorIndex = 1
        
        If Range("C" & CStr(startRow)).Value Like "" Then       ' For "Run Type"
            Range("C7").Select
            Selection.Copy
            Range("C" & CStr(startRow)).Select
            ActiveSheet.Paste
            Range("C" & CStr(startRow)).Font.ColorIndex = 1
        End If
        
        If Range("F" & CStr(startRow)).Select Like "" Then      ' For "BadData"
            Range("F7").Select
            Selection.Copy
            Range("F" & CStr(startRow)).Select
            ActiveSheet.Paste
            Range("F" & CStr(startRow)).Font.ColorIndex = 1
        End If

    Here is the changed code:


        'Copying the drop down list to the columns 'Run Type' and 'BadData'
        
        Range("B" & CStr(startRow)).Value = CStr(subTestNumber) & subTestID
        Range("B" & CStr(startRow)).Font.ColorIndex = 1
        
        If Range("C" & CStr(startRow)).Value Like "" Then       ' For "Run Type"
            Range("C7").Select
            Selection.Copy
            Range("C" & CStr(startRow)).Select
            ActiveSheet.Paste
            Range("C" & CStr(startRow)).Font.ColorIndex = 1
        End If
        
        If Range("F" & CStr(startRow)).Value Like "" Then      ' For "BadData"
            Range("F7").Select
            Selection.Copy
            Range("F" & CStr(startRow)).Select
            ActiveSheet.Paste
            Range("F" & CStr(startRow)).Font.ColorIndex = 1
        End If
    Any help would be greatly appreciated. Thanks.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Excel crashes when I change code

    Try this:

        With Rows(startrow)
            .Range("B1").Value = CStr(subTestNumber) & subTestID
            .Range("B1").Font.ColorIndex = 1
    
            If Len(.Range("C1").Text) = 0 Then     ' For "Run Type"
                Range("C7").Copy .Range("C1")
                .Range("C1").Font.ColorIndex = 1
            End If
    
            If Len(.Range("F1").Text) = 0 Then    ' For "BadData"
                Range("F7").Copy .Range("F1")
                .Range("F1").Font.ColorIndex = 1
            End If
        End With
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-11-2014
    Location
    Philadelphia, PA
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel crashes when I change code

    I tried it an I ended up getting the same result.
    Capture.JPG

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Excel crashes when I change code

    When you step through the code, what line fails?

  5. #5
    Registered User
    Join Date
    07-11-2014
    Location
    Philadelphia, PA
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel crashes when I change code

    I figured it out. My code was correct, I was just using it on the wrong computer. I kept trying to test the code on my laptop which is not connected to the server where the data is stored which is why Excel kept crashing. I ran the code this morning on a computer that is connected to the server and it worked.

    Thanks for all the help.

+ 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 always crashes at the very same point of the code
    By freakygreeneyes in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-31-2013, 09:59 AM
  2. Excel Crashes when adding code to workbook via VBA
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-24-2009, 01:10 PM
  3. My code crashes excel
    By ben_sumner in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-06-2006, 03:20 AM
  4. Excel crashes when code runs, Please help.
    By Potoroo in forum Excel General
    Replies: 2
    Last Post: 09-12-2006, 08:39 PM
  5. [SOLVED] VBA WinInet Code Crashes Excel
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2006, 12:55 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