Results 1 to 20 of 22

Thread: Using If function to separate data into multiple sheet

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #19
    VBAX Regular
    Joined
    Apr 2017
    Posts
    9
    Location
    I have 2 codes which run in the same sheet, and they do different tasks. When they runs together, my excel often freezes. I am trying to figure it out how to run them.

    1) This code runs when I put 1 in column M and the row has 1 in column M will be hidden.
    Private Sub Worksheet_Calculate()
    Dim Lastrow As Long, c As Range
    Application.EnableEvents = False
    Lastrow = Cells(Cells.Rows.Count, "M").End(xlUp).Row
    OnErrorResumeNext
    For Each c In Range("M3:M" & Lastrow)
       If c.Value = 1000 Then
            c.EntireRow.Hidden = True
        ElseIf c.Value = 0 Then
            c.EntireRow.Hidden = False
    EndIf
    Next
    OnErrorGoTo0
    Application.EnableEvents = True
    End Sub



    2) This code you've just written for me
    Private Sub Worksheet_Activate()
    Dim WS_Count As Integer
    Dim I AsInteger
        Dim rowcounterA() As Integer
        Dim rowcounterD() As Integer
        With Worksheets("Menu")
    Lastrowa = .Cells(Cells.Rows.Count, "AF").End(xlUp).Row
            lastrowD = .Cells(Cells.Rows.Count, "AJ").End(xlUp).Row
            If Lastrowa > lastrowD Then
            inarr = .Range(.Cells(1, 1), .Cells(Lastrowa, 36))
    Else
            inarr = .Range(.Cells(1, 1), .Cells(lastrowD, 36))
            End If
            End With
            WS_Count = ActiveWorkbook.Worksheets.Count
            ReDim rowcounterA(1 To WS_Count)
            ReDim rowcounterD(1 To WS_Count)
            For I = 1 To WS_Count
                rowcounterA(I) = 0
                rowcounterD(I) = 0
                With Worksheets(I)
                    If Not (.Name = "Menu") Then
                        For j = 3 To Lastrowa
                            If inarr(j, 32) = .Cells(1, 2) Then
                                .Cells(216 + rowcounterA(I), 11) = inarr(j, 35)
                                rowcounterA(I) = rowcounterA(I) + 1
                                End If
                                Next j
    For j = 3 To lastrowD
                                    If inarr(j, 36) = .Cells(1, 2) Then
                                        .Cells(216 + rowcounterD(I), 12) = inarr(j, 35)
                                        rowcounterD(I) = rowcounterD(I) + 1
                                    End If
                                Next j
                            End If
                        End With
                    Next I
    End Sub



    Last edited by Aussiebear; 04-14-2023 at 04:33 PM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •