Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 正确的变暗类型是什么?为什么我的宏变慢了?_Excel_Vba_Performance_Variable Declaration - Fatal编程技术网

Excel 正确的变暗类型是什么?为什么我的宏变慢了?

Excel 正确的变暗类型是什么?为什么我的宏变慢了?,excel,vba,performance,variable-declaration,Excel,Vba,Performance,Variable Declaration,我有一个Excel宏,在大多数情况下基本上都可以正常工作,但有三个问题困扰着我 代码有点长,所以我减少了它以解决问题:(问题也在我的代码中标记。) 第1条:当uniqueArray包含多个条目时,项和uniqueArray的Dim可以正常工作。但是当我测试了一个不太可能的情况,即uniqueArray只包含一个条目时,我得到了一个错误,即类型不匹配。我通常不在Excel中编程,所以我对vba中的不同类型不太熟悉。我是否需要这里的数组,或者我可以只更改Dim Nr.2:代码变得越来越慢,宏向工作簿

我有一个Excel宏,在大多数情况下基本上都可以正常工作,但有三个问题困扰着我

代码有点长,所以我减少了它以解决问题:(问题也在我的代码中标记。)

第1条:当
uniqueArray
包含多个条目时,
uniqueArray
Dim
可以正常工作。但是当我测试了一个不太可能的情况,即
uniqueArray
只包含一个条目时,我得到了一个错误,即类型不匹配。我通常不在Excel中编程,所以我对vba中的不同类型不太熟悉。我是否需要这里的数组,或者我可以只更改
Dim

Nr.2:代码变得越来越慢,宏向工作簿添加的工作表越多。这是正常的行为,还是我可以加快我的代码一点

Nr.3:几年前,我遇到了宏速度慢的问题。然后我发现了这个暗示,并被迫停顿了一下。我用这个宏又试了一次,速度提高了很多。为什么暂停会加快宏的速度

   Sub Three_Issues()
    Dim ColumnLetter As String
    Dim cell As Range
    Dim sheetCount, TotalRow, TotalCol As Integer
    'Dim item, uniqueArray As Variant
    Dim item, uniqueArray() As Variant
    Dim lastRow As Long

    Application.ScreenUpdating = False

    'Get unique brands:
    With Sheets("Brand")
    .Columns(1).EntireColumn.Delete
    Sheets("Sales").Columns("R:R").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("A1"), Unique:=True
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'uniqueArray = .Range("A3:A" & lastRow)
    'Update:
    If .Range("A3:A" & lastRow).Cells.Count = 1 Then
    ReDim uniqueArray(1, 1)
    uniqueArray(1, 1) = .Range("A3")
    Else
    uniqueArray = .Range("A3:A" & lastRow).Value
    End With

    TotalRow = Sheets("Sales").UsedRange.Rows.Count
    TotalCol = Sheets("Sales").UsedRange.Columns.Count
    ColumnLetter = Split(Cells(1, TotalCol).Address, "$")(1) 'Num2Char
    sheetCount = 0 'Counter for statusbar

For Each item In uniqueArray 'item=Brand
'->Issue 1: Runtimer error 13 Types don't match: This happens if the uniqueArray consists of only one brand.
'Then item is Variant/Empty and uniqueArray is Variant/String
'If uniqueArray consists of more than one brand - which is usually the case - it works fine.
'item=Variant/Empty uniqueArray=e.g. Variant/Variant(1 to 2, 1 to 1)
'Can I change the Dim statement to solve this special case, or do I need arrays maybe?

    'Filter sales for each brand:
    With Sheets("Sales")
    .Range(.Cells(2, 1), .Cells(TotalRow, TotalCol)).AutoFilter Field:=18, Criteria1:=item
    End With

    With Sheets("Agents")
    'Delete old...
    .Range(.Cells(2, 1), .Cells(2, 1).End(xlDown)).Clear
    '...and get new
    Sheets("Sales").Range(Sheets("Sales").Cells(3, 2), Sheets("Sales").Cells(2, 2).End(xlDown)).SpecialCells(xlCellTypeVisible).Copy
    .Range("A2").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    End With

    'List with all agents
    For Each cell In Worksheets("Agents").Range("A2", Worksheets("Agents").Range("A1").End(xlDown))

    With Sheets("Report")
    .Range("I4") = cell 'Copy agent and update the formulas within the report
'->Issue 2: It takes around 10 seconds to fill 10 sheets with the reports of 10 agents.
'When I reach 70-80 sheets, it slows down to 30 seconds for 10 sheets.
'Is this just because of the number of sheets, or can I speed it up again?

    .Range(.PageSetup.PrintArea).Copy
    Sheets.Add After:=Sheets("Report")

    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value 'Replace all formulas with values
    Application.CutCopyMode = False
    ActiveSheet.Name = cell

    sheetCount = sheetCount + 1
    If sheetAnz Mod 10 = 0 Then Application.StatusBar = sheetAnz 'Get statusupdate every 10 sheets
    End With
    Next

'->Issue 3: I create up to 400 sheets and when I want to continue and do some sorting of the sheets for example it takes a very long time.
'But if I add this break for a second, it works reasonably fine again. Why is that? Does vba needs the break to catch up with itself?
'Since the issue is not the sorting and the other stuff after the pause.

 Application.Wait (Now + TimeValue("0:00:01")) 'Code becomes faster after that...

    'Continue with other stuff.... sorting sheets and so on

Next

    Application.ScreenUpdating = True

End Sub

对其中一个问题有什么想法吗?

您可以使用下面的
UDF
输出一个或多个值的数组。这也将受益于传递工作表变量,以便可以正确限定对象


从当前宏调用函数,如下所示

uniqueArray = MyArr(lastrow)


请参阅-如果要将范围值读入数组,仅一个单元格是一种特殊情况。我通常创建一个UDF来测试范围是一个单元格还是多个单元格,并让它相应地返回正确的数组。当你试图将一个数组设置为一个单元格时,你只会得到一个值,而不是一个类型不匹配的数组,我想我已经在我的代码中添加了你的建议,但是现在在只有一个条目的情况下,“item”仍然为空,尽管“uniqueArray”正确地填充了这一条目。这是一个不同的问题,值得提出自己的问题。具体问题得到具体答案。你不能发布1、2、3、4、5个问题,然后期望我们在一个帖子中回答每个问题。搜索如何在数组中循环(提示:
UBOUND
LBOUND
),然后在遇到stuckI时发布另一个问题。我不确定我是否应该从中提出一个或多个问题。看来我弄错了我会检查你的提示,谢谢。
Public Function MyArr(lastrow As Long) As Variant

If Range("A3:A" & lastrow).Cells.Count = 1 Then
    ReDim MyArr(1, 1)
    MyArr(1, 1) = Range("A3")
Else
    MyArr = Range("A3:A" & lastrow).Value
End If

End Function