Share via

Excel VBA Macro Shut Down

Anonymous
2022-08-03T04:20:22+00:00

사용 프로그램: MS Oficce Pro Plus 2016 Excel

안녕하세요

기존에 사용 중이던 매크로에 모든 시트에 적용되도록 반복 코드를 추가하여 매크로 실행하니 엑셀 프로그램이 꺼집니다.

해당 매크로를 단일 시트에 사용 시에는 문제가 발생하지 않습니다.

반복 코드가 문제일까요? 혹은 과부화 문제일까요?

도움 부탁드립니다.

Program: MS Oficce Pro Plus 2016 Excel

hello.

The Excel program is turned off when the macro is executed by adding a repeating code to be applied to all sheets in the previously used macro.

When using that macro on a single sheet, there is no problem.

Is the repeating code the problem? Or is it an overload problem?

Please help.

Code

Sub DDD()
'
DDD
Dim r As Range
Dim c As Range
Dim ds As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name <> "KEY" Then
ws.Select
Range("A2:N2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("AA2").Select
ActiveSheet.Paste
Range("A1:Z2000").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Selection.Value = "HARN"
Range("B1").Select
Selection.Value = "WIRE"
Range("C1").Select
Selection.Value = "FROM_CONNECTOR"
Range("D1").Select
Selection.Value = "FROM_CONNECTOR"
Range("E1").Select
Selection.Value = "FROM_PIN"
Range("F1").Select
Selection.Value = "FROM_CONNECTOR"
Range("G1").Select
Selection.Value = "SQUA"
Range("H1").Select
Selection.Value = "COL"
Range("I1").Select
Selection.Value = "TO_CONNECTOR"
Range("J1").Select
Selection.Value = "TO_CONNECTOR"
Range("K1").Select
Selection.Value = "TO_PIN"
Range("L1").Select
Selection.Value = "TO_CONNECTOR"
Range("M1").Select
Selection.Value = "J/S"
Range("N1").Select
Selection.Value = "APPCODE"
Range("O1").Select
Selection.Value = "MAT"
Range("P1").Select
Selection.Value = "T1"
Range("Q1").Select
Selection.Value = "T2"
Range("R1").Select
Selection.Value = "REV"
Range("S1").Select
Selection.Value = "REMARKS"
Range("T1").Select
Selection.Value = "JOINT"
Range("U1").Select
Selection.Value = "JOINT"
Range("AA2:AA2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("AB2:AB2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("B2").Select
ActiveSheet.Paste
Range("AC2:AC2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("G2").Select
ActiveSheet.Paste
Range("AD2:AD2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("H2").Select
ActiveSheet.Paste
Range("AE2:AE2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("O2").Select
ActiveSheet.Paste
Range("AF2:AF2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("C2, D2, F2").Select
ActiveSheet.Paste
Range("AG2:AG2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("E2").Select
ActiveSheet.Paste
Range("AH2:AH2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("P2").Select
ActiveSheet.Paste
Range("AI2:AI2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("I2, J2, L2").Select
ActiveSheet.Paste
Range("AJ2:AJ2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("K2").Select
ActiveSheet.Paste
Range("AK2:AK2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("Q2").Select
ActiveSheet.Paste
Range("AL2:AL2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("N2").Select
ActiveSheet.Paste
Range("AM2:AM2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("R2").Select
ActiveSheet.Paste
Range("AN2:AN2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("S2").Select
ActiveSheet.Paste
Range("AO2:AO2000").Select
Application.CutCopyMode = False
Selection.Copy
Range("R2").Select
ActiveSheet.Paste
Range("AA1:AN2000").Select
Selection.Delete Shift:=xlUp
Windows("CLIST.xlsx").Activate
Range("A3:H500").Select
Application.CutCopyMode = False
Selection.Copy
Range("A3").Select
Windows("WLIST.xlsm").Activate
Range("V2").Select
ActiveSheet.Paste
With Cells.Select
Selection.NumberFormatLocal = "G/표준"
End With
With Range("D2").Select
ActiveCell.FormulaR1C1 = "=INDEX(C[18],MATCH(RC[-1],C[19],0),0)"
Range("D2").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("F2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=INDEX(C[19],MATCH(RC[-3],C[17],0),0)"
Range("F2").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("J2").Select
Application.CutCopyMode = False
Range("F2").Select
ActiveCell.FormulaR1C1 = "=INDEX(C25,MATCH(RC[-3],C23,0),0)"
Range("F2").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("J2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=INDEX(C[12],MATCH(RC[-1],C[13],0),0)"
Range("J2").Select
ActiveCell.FormulaR1C1 = "=INDEX(C22,MATCH(RC[-1],C23,0),0)"
Range("J2").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("L2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=INDEX(C25,MATCH(RC[-3],C23,0),0)"
Range("L2").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("L2").Select
End With
End If
Next
End Sub
Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. Anonymous
    2022-08-05T21:52:38+00:00

    Hi KI

    Re, ".... Is the repeating code the problem? Or is it an overload problem? "

    Well .., Sort of,

    Notes:

    1. Please, Avoid using SELECT, SELECTION, ACTIVECELLwhen coding. It is slow and error-prone. Always refer to the objects directly.
    2. It is not clear Which workbook/file you are running the macro from.

    a) How many workbooks are you working with, when running the macro?

    b) What is the name of the workbook you run the macro from?

    1. It is not fully clear to me on the 2nd part of the code when you activate the "CLIST.xlsx" workbook ( from the Windows("CLIST.xlsx").Activate line downwards)

    I do not see the purpose of having those lines inside the loop.

    1. As our colleague Nothing Left to Lose mentioned

    "...select and add a custom number format to ~17 billion cells on each sheet."

    That's A LOT, my friend

    1. The following code use VBA arrays to speed up the loop process.

    Sub FixDataTable()

    Dim ws As Worksheet

    Dim HARN As Variant

    Dim WIRE As Variant

    Dim SQUA As Variant

    Dim COL As Variant

    Dim MAT As Variant

    Dim F_CONNECTOR As Variant

    Dim T_CONNECTOR As Variant

    Dim F_PIN As Variant

    Dim T_PIN As Variant

    Dim T1 As Variant

    Dim T2 As Variant

    Dim APPCODE As Variant

    Dim REMARKS As Variant

    Dim CLIST As Variant

    Dim copyRng As Variant

    Application.ScreenUpdating = False

    For Each ws In Worksheets

    If ws.Name <> "KEY" Then

    With ws

    ''' Populate the arrays with the data 
    
        HARN = .Range("A1:A2000") 
    
        WIRE = .Range("B1:B2000") 
    
        SQUA = .Range("C1:C2000") 
    
        COL = .Range("D1:D2000") 
    
        MAT = .Range("E1:E2000") 
    
        F\_CONNECTOR = .Range("F1:F2000") 
    
        F\_PIN = .Range("G1:G2000") 
    
        T1 = .Range("H1:H2000") 
    
        T\_CONNECTOR = .Range("I1:I2000") 
    
        T\_PIN = .Range("J1:J2000") 
    
        T2 = .Range("K1:K2000") 
    
        APPCODE = .Range("L1:L2000") 
    
        REMARKS = .Range("N1:N2000") 
    
        .Range("A1:N2000").ClearContents  ''' This line clear/deletes the source data table 
    
        ''' Paste back the data in their relevant columns 
    
        .Range("A1:A2000") = HARN 
    
        .Range("B1:B2000") = WIRE 
    
        .Range("C1:C2000") = F\_CONNECTOR 
    
        .Range("D1:D2000") = F\_CONNECTOR 
    
        .Range("E1:E2000") = F\_PIN 
    
        .Range("F1:F2000") = F\_CONNECTOR 
    
        .Range("G1:G2000") = SQUA 
    
        .Range("H1:H2000") = COL 
    
        .Range("I1:I2000") = T\_CONNECTOR 
    
        .Range("J1:J2000") = T\_CONNECTOR 
    
        .Range("K1:K2000") = T\_PIN 
    
        .Range("L1:L2000") = T\_CONNECTOR 
    
        .Range("O1:O2000") = MAT 
    
        .Range("P1:P2000") = T1 
    
        .Range("Q1:Q2000") = T2 
    
        .Range("S1:S2000") = REMARKS 
    
       ''' This line inserts the new Headers in one go 
    
       .Range("A1:U1").Value = Array("HARN", "WIRE", "FROM\_CONNECTOR", "FROM\_CONNECTOR", "FROM\_PIN", "FROM\_CONNECTOR", "SQUA", "COL", "TO\_CONNECTOR", "TO\_CONNECTOR", "TO\_PIN", "TO\_CONNECTOR", "J/S", "APPCODE", "MAT", "T1", "T2", "REV", "REMARKS", "JOINT", "JOINT") 
    

    End With

    '''''''''******PART 2*************************PART2**********'''''*****************PART2***************************************PART2*********

    Windows("CLIST.xlsx").Activate

    copyRng = Range("A3:H500")

    Windows("WLIST.xlsm").Activate

    Range("V2:AC499") = copyRng

    Range("V2:AC499").NumberFormatLocal = "G/??"

    Range("D2", Range("D2").End(xlDown)).FormulaR1C1 = "=INDEX(C[18],MATCH(RC[-1],C[19],0),0)"

    Range("F2", Range("F2").End(xlDown)).FormulaR1C1 = "=INDEX(C[19],MATCH(RC[-3],C[17],0),0)"

    Range("J2", Range("J2").End(xlDown)).FormulaR1C1 = "=INDEX(C22,MATCH(RC[-1],C23,0),0)"

    Range("L2", Range("L2").End(xlDown)).FormulaR1C1 = "=INDEX(C25,MATCH(RC[-3],C23,0),0)"

    End If

    Next ws

    Application.ScreenUpdating = True

    End Sub

    I hope this helps you and gives a solution to your problem

    Do let me know if you need more help

    Regards

    Jeovany

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-08-04T02:13:33+00:00

    Hi,

    Try to login with a new user profile and see whether it works.

    Go to this link for your reference https://answers.microsoft.com/en-us/msoffice/forum/all/excel-keeps-crashing-whenever-i-run-macro/395143eb-006c-4ff4-b74d-1c84ca838641

    0 comments No comments
  2. Anonymous
    2022-08-03T05:18:34+00:00

    RE: somethings wrong

    The 3 lines below add a custom number format to ~17 billion cells on each sheet.

    '---

    "With Cells.Select
    Selection.NumberFormatLocal = "G/??"
    End With"
    '---

    Also, the use of "Select" should be eliminated.

    I was going to tidy it up for you, but it is bedtime here (West Coast of the USA).

    '---

    Nothing Left to Lose

    0 comments No comments
  3. Anonymous
    2022-08-03T05:01:49+00:00

    Hi KI J,

    Thank you for posting your query. My name is Clarence, I'll be more than happy to assist you with your concern.

    To resolve your query kindly do the steps below in looping your worksheets.

    Sub Worksheet_Loop()

    Dim *** As Worksheet

    'Loop through each worksheet in a workbook
    For Each *** In ThisWorkbook.Worksheets
    Debug.Print ***. Name
    Next ***

    End Sub

    Go to this link for your reference https://www.thespreadsheetguru.com/blog/2015/4/9/repeat-tasks-with-vba-code-by-looping

    Note: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.'

    Do not hesitate to message us if you need further assistance.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Best Regards,
    Clarence

    0 comments No comments