【问题标题】:Excel VBA - Get data from Pivot Table with 11000+ rows and 53 columnsExcel VBA - 从具有 11000 多行和 53 列的数据透视表中获取数据
【发布时间】:2020-12-19 03:19:09
【问题描述】:

我有一个包含 11,000 多行和 53 列的大型数据透视表。它看起来像这样:

第一列“Filial”是部门编号,“Lager”列描述存储文章(产品)的存储空间,“Hovedgruppe”列是文章组。编号的列是周。

基本上,数据透视表显示一周内统计了多少篇文章,以及在哪个部门、商店位置和文章组中。

我需要能够从特定单元格中获取数据。

使用多列搜索数据透视表以找到准确的行和正确的星期的最佳方法是什么?

我现在拥有的代码可以运行,但我确信它不是最优化的,并且运行时间比运行所需的时间要长得多。这是代码中需要改进的部分:

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

它被加载到另一个工作簿中,结果如下所示:

然后在另一个工作表中使用公式将处理后的数据转换为:

【问题讨论】:

  • 破译您的代码非常困难,因此用文字准确解释您想要获得什么输出以及输入是什么真的很有帮助。是什么决定了应该定位哪些行以及这些行中的哪些列?
  • 作为一个写了 15 年 VBA 的人,我会说,如果你发布的内容有效,我会使用它。除非您要扩展到每天要多次执行此例程的 100 位用户,否则我将保留代码原样。我们将无法弄清楚你在做什么,因为那太复杂了。如果它不是一段被大量使用的代码,即使它在运行时每天花费你 5 秒的额外时间,也可以保持原样。我敢肯定,你写它的投入远不止这些。
  • 我已经用整个代码和更多图片更新了原始帖子,以说明我在这里所做的事情。 :) 我意识到这非常复杂,而且不太可能有人会在这里提出更好的代码。就像我说的那样,即使我确信它不是最理想的,我也有工作......
  • 我在这里对这个问题做了一个过度简化的版本:stackoverflow.com/questions/65352434/… 也许那个更容易回答。 :) 虽然它可能缺乏上下文....

标签: excel vba excel-formula


【解决方案1】:

先尝试简单的东西。

一开始,调用:

Application.ScreenUpdating = False

最后,调用:

Application.ScreenUpdating = True

停止调用(因为您没有在任何地方引用 ActiveSheet 或类似内容):

tWS.Activate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2021-09-08
    • 2017-07-07
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多