简体   繁体   中英

Excel VBA - Get data from Pivot Table with 11000+ rows and 53 columns

I have a large Pivot Table with more than 11,000 rows and 53 columns. It looks like this:

数据透视表

The first column "Filial" is a department number, the "Lager" column describes what storage an article (product) is stored in and the "Hovedgruppe" column is the article group. The numbered columns are weeks.

Basically, the Pivot Table shows how many articles have been counted during a week, and in which department, store location and article group.

I need to be able to fetch data from a specific cell.

What is the best way to search a pivot table using multiple columns to find the precise row and then the right week?

The code I have right now works, but I'm sure it's not very optimal and takes much longer than necessary to run. This is the part of the code that needs improvement:

Option Explicit

Sub CountingData_ImportOneWeek()

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim tWS As Worksheet: Set tWS = ThisWorkbook.Worksheets("Egeneide")
Dim inputWeek As Integer
Dim inputStore As Integer

inputStore = 0

On Error GoTo inputWeekError
inputWeek = InputBox("Tast inn ukenr for uke som skal oppdateres...")

If IsNumeric(inputWeek) And inputWeek >= 1 And inputWeek <= 52 Then
    
    tWS.Activate
    Call CountingData_ImportExecute(inputWeek, inputStore)
    
Else
    
inputWeekError:
    MsgBox "Vennligst tast inn et gyldig ukenr mellom 1 og 52", vbCritical
    
End If

End Sub

Sub CountingData_ImportOneStore()

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim tWS As Worksheet: Set tWS = ThisWorkbook.Worksheets("Egeneide")
Dim inputWeek As Integer
Dim inputStore As Integer

inputWeek = 0

On Error GoTo inputStoreError
inputStore = InputBox("Tast inn avdelingsnummer til butikken som skal oppdateres...")

If IsNumeric(inputStore) And inputStore >= 1101 And inputStore <= 1199 Then
    
    tWS.Activate
    Call CountingData_ImportExecute(inputWeek, inputStore)
    
Else
    
inputStoreError:
    MsgBox "Vennligst tast inn et gyldig avdelingsnummer", vbCritical
    
End If

End Sub

Sub CountingData_ImportAllWeeks()

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim tWS As Worksheet: Set tWS = ThisWorkbook.Worksheets("Egeneide")
Dim inputWeek As Integer
Dim inputStore As Integer

inputWeek = 0
inputStore = 0

tWS.Activate
Call CountingData_ImportExecute(inputWeek, inputStore)

End Sub

Sub CountingData_ImportExecute(ByVal inputWeek As Integer, inputStore As Integer)

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim tWS As Worksheet: Set tWS = ThisWorkbook.Worksheets("Egeneide")
Dim mWS As Worksheet: Set mWS = ThisWorkbook.Worksheets("Min butikk")
Dim iWS As Worksheet: Set iWS = ThisWorkbook.Worksheets("Kontrollpanel")
Dim dWS As Worksheet: Set dWS = ThisWorkbook.Worksheets("Telledata")
Dim pWS As Worksheet: Set pWS = ThisWorkbook.Worksheets("Telleplandata")
Dim oWB As Workbook: Set oWB = Workbooks.Open(tWB.Path & "\Telledata egeneide YTD.xlsx", ReadOnly:=True)
Dim oWS As Worksheet: Set oWS = oWB.Sheets("Telledata")
Dim thisWeek As Integer
Dim oWSLastRow As Long
Dim storeNo As Integer
Dim storeStartRow As Long, storeEndRow As Long
Dim countedRows11_0001, countedRows12_0001, countedRows14_0001, countedRows16_0001 As Integer
Dim countedRows11_0002, countedRows12_0002, countedRows14_0002, countedRows16_0002 As Integer
Dim countedRows0003 As Integer
Dim wCount As Integer, qWeek As Range, qWeeks As Range
Dim inputWeekColumn As Integer
Dim qRows As Range, qCell As Range, qCount As Integer, qRowsNo As Integer
Dim weekSelection As Range

On Error GoTo ProcError

tWS.Activate
    
    thisWeek = Application.WorksheetFunction.Max(oWS.Range("D4:BD4"))
    tWS.Range("I2").Value = "Laster inn..."
    
    If inputWeek = 0 Then
        Set qWeeks = dWS.ListObjects("butikkdata").HeaderRowRange
        wCount = 0
    Else
        Set qWeeks = dWS.ListObjects("butikkdata").ListColumns(inputWeek + 3).DataBodyRange
        wCount = inputWeek - 1
    End If
    
    For Each qWeek In qWeeks.Columns
        
        wCount = wCount + 1
        If wCount > thisWeek Then
            tWS.Range("I2").Value = ""
            Exit For
        End If
        
        If Application.WorksheetFunction.CountIf(iWS.Range("H6:H15"), wCount) > 0 Then
            GoTo NextIteration
        End If
        
        If inputStore = 0 Then
            tWS.Range("I2").Value = "Laster inn uke " & wCount & "..."
        Else
            tWS.Range("I2").Value = "Laster inn uke " & wCount & " for filial " & inputStore & "..."
        End If
    
        inputWeekColumn = wCount + 3
        Set qRows = dWS.ListObjects("butikkdata").ListColumns(inputWeekColumn).DataBodyRange
        qRowsNo = dWS.ListObjects("butikkdata").ListColumns(inputWeekColumn).DataBodyRange.Rows.Count
        
        With oWS
            oWSLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        End With
        
        qCount = 0
        For Each qCell In qRows.Rows
            
            qCount = qCount + 1
            
            storeNo = qCell.Offset(0, -wCount - 2).Value
            
            If inputStore > 0 And Not storeNo = inputStore Then
                GoTo NextStore
            End If
            
            If Not qCell.Value = "X" And Not qCell.Value = "H" Then
                
                With oWS
                    storeStartRow = .Range("A:A").Find(what:=storeNo, after:=.Range("A1")).Row
                    storeEndRow = .Range("A:A").Find(what:=storeNo, after:=.Range("A1"), searchdirection:=xlPrevious).Row
                End With
                
                If Not Application.WorksheetFunction.CountIf(iWS.Range("J6:J15"), wCount) > 0 Then
                    
                    countedRows11_0001 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("C" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0001 = countedRows11_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("D" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0001 = countedRows11_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("E" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0001 = countedRows11_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("C" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows11_0001 = countedRows11_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("D" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows11_0001 = countedRows11_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("E" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows12_0001 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("K" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0001 = countedRows12_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("L" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0001 = countedRows12_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("M" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0001 = countedRows12_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("K" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows12_0001 = countedRows12_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("L" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows12_0001 = countedRows12_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("M" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows14_0001 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("G" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0001 = countedRows14_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("H" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0001 = countedRows14_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("I" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0001 = countedRows14_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("G" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows14_0001 = countedRows14_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("H" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows14_0001 = countedRows14_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("I" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows16_0001 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("O" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0001 = countedRows16_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("P" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0001 = countedRows16_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("Q" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0001 = countedRows16_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("O" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows16_0001 = countedRows16_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("P" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows16_0001 = countedRows16_0001 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0001", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("Q" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows11_0002 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("C" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0002 = countedRows11_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("D" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0002 = countedRows11_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("E" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows11_0002 = countedRows11_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("C" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows11_0002 = countedRows11_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("D" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows11_0002 = countedRows11_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("E" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows12_0002 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("K" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0002 = countedRows12_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("L" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0002 = countedRows12_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("M" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows12_0002 = countedRows12_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("K" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows12_0002 = countedRows12_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("L" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows12_0002 = countedRows12_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("M" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows14_0002 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("G" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0002 = countedRows14_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("H" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0002 = countedRows14_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("I" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows14_0002 = countedRows14_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("G" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows14_0002 = countedRows14_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("H" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows14_0002 = countedRows14_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("I" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")

                    countedRows16_0002 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("O" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0002 = countedRows16_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("P" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0002 = countedRows16_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("Q" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">4")
                    countedRows16_0002 = countedRows16_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("O" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows16_0002 = countedRows16_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("P" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    countedRows16_0002 = countedRows16_0002 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0002", oWS.Range("C" & storeStartRow & ":C" & storeEndRow), pWS.Range("Q" & wCount + 2), oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">4")
                    
                Else
                    countedRows11_0001 = 1
                    countedRows12_0001 = 1
                    countedRows14_0001 = 1
                    countedRows16_0001 = 1
                    countedRows11_0002 = 1
                    countedRows12_0002 = 1
                    countedRows14_0002 = 1
                    countedRows16_0002 = 1
                End If
                
                countedRows0003 = Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0003", oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount), ">0")
                countedRows0003 = countedRows0003 + Application.WorksheetFunction.CountIfs(oWS.Range("A" & storeStartRow & ":A" & storeEndRow), storeNo, oWS.Range("B" & storeStartRow & ":B" & storeEndRow), "0003", oWS.Range("C" & storeStartRow & ":C" & storeEndRow).Offset(0, wCount + 1), ">0")
                
                If countedRows11_0001 > 1 Then countedRows11_0001 = 1
                If countedRows12_0001 > 1 Then countedRows12_0001 = 1
                If countedRows14_0001 > 1 Then countedRows14_0001 = 1
                If countedRows16_0001 > 1 Then countedRows16_0001 = 1
                If countedRows11_0002 > 1 Then countedRows11_0002 = 1
                If countedRows12_0002 > 1 Then countedRows12_0002 = 1
                If countedRows14_0002 > 1 Then countedRows14_0002 = 1
                If countedRows16_0002 > 1 Then countedRows16_0002 = 1
                If countedRows0003 > 5 Then countedRows0003 = 1 Else countedRows0003 = 0
                
                If wCount Mod 2 = "0" And wCount < 40 Then
                    qCell.Value = countedRows11_0001 & countedRows12_0001 & countedRows14_0001 & countedRows16_0001 & countedRows11_0002 & countedRows12_0002 & countedRows14_0002 & countedRows16_0002
                Else
                    qCell.Value = countedRows11_0001 & countedRows12_0001 & countedRows14_0001 & countedRows16_0001 & countedRows11_0002 & countedRows12_0002 & countedRows14_0002 & countedRows16_0002 & countedRows0003
                End If
                
            End If
            
NextStore:
            tWS.Range("S2").Value = qCount / qRowsNo
            
        Next
        
        If inputWeek = 0 Then
            iWS.Range("D6").Value = wCount
        ElseIf inputWeek > iWS.Range("D6").Value Then
            iWS.Range("D6").Value = inputWeek
        End If
NextIteration:
    Next
    
    tWS.Range("I2").Value = ""
    tWS.Range("S2").Value = ""
    
    tWS.Range("egeneide_ytd").Sort Key1:=Range("D:D"), Order1:=xlAscending, _
    Header:=xlYes
    
    tWS.Activate

    If inputWeek = 0 And inputStore = 0 Then
        MsgBox "Alle uker har blitt oppdatert og oversikten har blitt sortert på nytt!", vbInformation
    ElseIf Not inputWeek = 0 And inputStore = 0 Then
        MsgBox "Uke " & inputWeek & " har blitt oppdatert og oversikten har blitt sortert på nytt!", vbInformation
    ElseIf inputWeek = 0 And Not inputStore = 0 Then
        MsgBox "Filial " & inputStore & " har blitt oppdatert og oversikten har blitt sortert på nytt!", vbInformation
    End If
    
    GoTo endOfSub
    
ProcError:
MsgBox "Makroen inneholder en feil! Vennligst kontakt forfatter av dette skjemaet.", vbCritical
    
endOfSub:
tWS.Sort.SortFields.Clear

End Sub

It is loaded into another workbook and the result looks like this: 结果

Formulas are then used in another sheet to turn that processed data into this: 最后结果

Try the simple stuff first.

At the very beginning, call:

Application.ScreenUpdating = False

At the very end, call:

Application.ScreenUpdating = True

Stop calling (because you're not referring to ActiveSheet or similar anywhere):

tWS.Activate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM