Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 使用UsedRange的最后一行。如果使用A1:B10范围,则为UsedRange。但是如果您现在在C24中输入某个内容并再次删除它,则UsedRange仍然是A1:C24。因此,如果可能,我建议将最后一行定义为列中的非空白单元格。但这要求在列末尾之前不应有空白单元格_Arrays_Vba_Excel_Arraylist - Fatal编程技术网

Arrays 使用UsedRange的最后一行。如果使用A1:B10范围,则为UsedRange。但是如果您现在在C24中输入某个内容并再次删除它,则UsedRange仍然是A1:C24。因此,如果可能,我建议将最后一行定义为列中的非空白单元格。但这要求在列末尾之前不应有空白单元格

Arrays 使用UsedRange的最后一行。如果使用A1:B10范围,则为UsedRange。但是如果您现在在C24中输入某个内容并再次删除它,则UsedRange仍然是A1:C24。因此,如果可能,我建议将最后一行定义为列中的非空白单元格。但这要求在列末尾之前不应有空白单元格,arrays,vba,excel,arraylist,Arrays,Vba,Excel,Arraylist,在新设置的帮助下,五行代码复制范围A1:BI2可以压缩为一行。但后来我在理解你想做什么时遇到了麻烦。看起来您正在查找列标题“Cust Bill To ID”,但使用Last表示您正在查看行。此外,当找到匹配项时,您似乎打算选择下一个单元格,但ActiveCell.Offset(i,0)。select将选择活动单元格下的一行单元格,该行可以是比Last小的任何数字,并指定远低于Last的单元格 因此,我的循环对于C=2持续时间没有任何用处。也许你可以用简单的语言描述你的意图,然后我们就可以找到它的

在新设置的帮助下,五行代码复制范围A1:BI2可以压缩为一行。但后来我在理解你想做什么时遇到了麻烦。看起来您正在查找列标题“Cust Bill To ID”,但使用Last表示您正在查看行。此外,当找到匹配项时,您似乎打算选择下一个单元格,但
ActiveCell.Offset(i,0)。select
将选择活动单元格下的一行单元格,该行可以是比Last小的任何数字,并指定远低于Last的单元格


因此,我的循环
对于C=2持续时间
没有任何用处。也许你可以用简单的语言描述你的意图,然后我们就可以找到它的代码。

免责声明:我还没有测试代码,但这个概念应该是有效的:

Sub Mismatch()

Dim authSht As Worksheet ' Renamed this variable
Dim misSht As Worksheet ' Added a worksheet variable
Dim i As Integer
Dim k As Integer
Dim last As Integer
Dim BTID(1 To 1) As String ' We'll dim the arrays 1-based to avoid ninja arithmetics in the loop later
Dim CMF(1 To 1) As String
Dim rng1 As Range ' Added this variable
Dim rng2 As Range ' Added this variable

Set authSht = ThisWorkbook.Worksheets("Authorizations Issued")
Set misSht = ThisWorkbook.Worksheets("Mismatch")

''find Mismatch
authSht.Range("A1:BI1").Copy
misSht.Range("A1").Paste

last = authSht.UsedRange.Rows.Count

Set rng1 = authSht.Range("A2")
Set rng2 = rng1

For Each c In Range(rng1, authSht.Range(rng1.Row, authSht.Cells(1, Columns.Count).End(xlToLeft).Column))
    If c.Value = "Cust Bill To ID" Then Set rng1 = c
    If c.Value = "SAP CMF#" Then Set rng2 = c
Next c

For i = 1 To last
    BTID(i) = rng1.Offset(i, 0).Value
    CMF(i) = rng2.Offset(i, 0).Value

    If i < last Then ' If the loop will be executed at least 1 more time :
        ReDim Preserve BTID(1 To i + 1) ' .. resize the arrays to make room for the next entry
        ReDim Preserve CMF(1 To i + 1)
    End If
Next i


Dim l As Integer
l = 2

For k = 3 To last
    If BTID(k) <> CMF(k) Then
        authSht.Range("$A$" & k & ":$BH$" & k).Copy
        misSht.Range("$A$" & l).Paste
        l = l + 1
    Else
        l = l
    End If
Next k

misSht.UsedRange.EntireColumn.AutoFit

End Sub
子不匹配()
Dim authSht As WORKEY'重命名了此变量
“作为工作表的Dim MISHT”添加了一个工作表变量
作为整数的Dim i
将k变为整数
最后变暗为整数
将BTID(1到1)设置为字符串“我们将基于1对数组进行调暗,以避免稍后循环中的忍者算法
作为字符串的尺寸CMF(1到1)
Dim rng1作为范围“添加了此变量
Dim rng2 As Range“添加了此变量
Set-authSht=ThisWorkbook.Worksheets(“已颁发的授权”)
Set misSht=此工作簿。工作表(“不匹配”)
''找到不匹配
授权范围(“A1:BI1”)。复制
错误范围(“A1”).粘贴
last=authSht.UsedRange.Rows.Count
设置rng1=authSht.Range(“A2”)
设置rng2=rng1
对于范围内的每个c(rng1,authSht.Range(rng1.Row,authSht.Cells(1,Columns.Count).End(xlToLeft.Column))
如果c.Value=“客户账单到ID”,则设置rng1=c
如果c.Value=“SAP CMF#”,则设置rng2=c
下一个c
对于i=1到最后
BTID(i)=rng1.偏移量(i,0).值
CMF(i)=rng2.偏移量(i,0).值
如果i
简言之:

  • 使用限定范围来消除所有各种
    选择
    用法
  • 将两个循环(
    i
    j
    )合并为一个,根据数据集的不同,应该可以节省20-40%的宏运行时间
  • 阵列现在已重新定尺寸
进一步优化:

  • 所有的搜索、添加、复制等都可以在内存中完成,而不是在工作表上。对于大量的工作表,这将是速度上的重大改进。特别是
    k
    循环-改为添加到内存中的数组,完成后将数组推到工作表中
  • 从定义“Cust Bill”和“SAP CMF”列开始,就不需要找到正确的范围

调暗阵列时,或者如果您事先不知道,则必须使用添加的
Redim
@SJR
Redim BTID(0)
在代码中声明阵列的大小,但结果保持不变:(这将只保存一个条目,因为数组是基于零的(默认)。您还需要仔细阅读如何避免
Select
。是否有更好的方法或示例使此代码工作?请…顺便说一句,当我将
BTID(I)=Selection.Value
更改为
MsgBox Selection.Value
显然错误来自BTID()array.SOP:1.从单元格(“A2”)进行搜索,以找到“Cust Bill to ID”和“CMF#”的确切位置2.使用这两列的数据分配这两个数组。3.比较这两个数组,如果不匹配,则将整行数据从工作表(“已颁发的授权”)复制到工作表(“不匹配”)。@Variatus
BTID(i) = Selection.Value
CMF(j) = Selection.Value
Sub Mismatch()


Dim sht As Worksheet
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim last As Integer
Dim BTID() As String
Dim CMF() As String

''find Mismatch
Sheets("Authorizations Issued").Select
Range("A1:BI1").Copy
Sheets("Mismatch").Select
Range("A1").Select
ActiveSheet.Paste

Sheets("Authorizations Issued").Select
last = ActiveSheet.UsedRange.Rows.Count


For i = 1 To last
    Range("A2").Select
    Sheets("Authorizations Issued").Select
    Do While Selection.Value <> "Cust Bill To ID"
        ActiveCell.Offset(0, 1).Select
    Loop
    If Selection.Value = "Cust Bill To ID" Then
        ActiveCell.Offset(i, 0).Select
redim preserve BTID(i)
        BTID(i) = Selection.Value
    End If
Next i


For j = 1 To last
    Range("A2").Select
    Do While Selection.Value <> "SAP CMF#"
        ActiveCell.Offset(0, 1).Select
    Loop
    If Selection.Value = "SAP CMF#" Then
        ActiveCell.Offset(j, 0).Select
redim preserve CMF(J)
        CMF(j) = Selection.Value
    End If
Next j

 Dim l As Integer
 l = 2

 For k = 3 To last
    If BTID(k) <> CMF(k) Then
       Range("$A$" & k & ":$BH$" & k).Copy
       Sheets("Mismatch").Select
       Range("$A$" & l).Select
       ActiveSheet.Paste
       l = l + 1
    Else: l = l
    End If
  Next k
 Sheets("Mismatch").Select
 ActiveSheet.UsedRange.Select
 Selection.EntireColumn.EntireColumn.AutoFit
End Sub
Option Explicit

Sub Mismatch()


    Dim AuthIssuedsheet As Worksheet
    Set AuthIssuedsheet = ThisWorkbook.Sheets("Authorizations Issued")

    Dim MismatchSheet As Worksheet
    Set MismatchSheet = ThisWorkbook.Sheets("Mismatch")

    'Copying the header of AuthIssuedsheet to MismatchSheet
    AuthIssuedsheet.Range("A1:BI1").Copy MismatchSheet.Range("A1")

    Dim AuthShtLastRow As Long

    'Finding last row of used data in Authorizations Issued sheet
    With AuthIssuedsheet
        AuthShtLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim AuthShtLastCol As Long

    'Finding last row of used data in Authorizations Issued sheet
    With AuthIssuedsheet
        AuthShtLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With



    Dim CustBillCol As Range
    Dim SAPCMF As Range
    Dim CustBillArray As Variant
    Dim SAPCMFArray As Variant
    Dim CustBill_SAPCMF_Array As Variant

    With AuthIssuedsheet

    'Finds the header column with text Cust Bill To IF
    Set CustBillCol = .Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Find( _
                What:="Cust Bill To ID", After:=AuthIssuedsheet.Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    'Finds the header column with text SAP CMF#
    Set SAPCMF = .Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Find( _
                What:="SAP CMF#", After:=AuthIssuedsheet.Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

    CustBillArray = .Range(.Cells(2, CustBillCol.Column), .Cells(AuthShtLastRow, CustBillCol.Column))
    SAPCMFArray = .Range(.Cells(2, SAPCMF.Column), .Cells(AuthShtLastRow, SAPCMF.Column))

    End With

    Dim ArrayPos As Long
    Dim LastRowMismatch As Long

    'deletes old data from Mismatch sheet before copying the mismatch data over
    MismatchSheet.Range(MismatchSheet.Rows(2), MismatchSheet.Rows(Rows.Count)).ClearContents

    'Looping through every value in the two arrays to check for mismatchs
    For ArrayPos = 1 To UBound(CustBillArray)

        If CustBillArray(ArrayPos, 1) <> SAPCMFArray(ArrayPos, 1) Then

            'Finding last row of used data in Mismatch sheet
            With MismatchSheet
                LastRowMismatch = .Cells(.Rows.Count, "A").End(xlUp).Row
            End With
            'Copy the information acorss to mismatch sheet
                AuthIssuedsheet.Range(AuthIssuedsheet.Cells(ArrayPos + 1, 1), _
                                      AuthIssuedsheet.Cells(ArrayPos + 1, AuthShtLastCol)).Copy _
                    MismatchSheet.Range("" & "A" & LastRowMismatch + 1)

        End If


        'becasue you might be running through a large data set i just put a msgbox every 20% to
        'let user know it will be a while
        'Ideally you would have a Userform with progress for large data sets

        If ((ArrayPos / UBound(CustBillArray)) * 100) = 20 Or _
            ((ArrayPos / UBound(CustBillArray)) * 100) = 40 Or _
             ((ArrayPos / UBound(CustBillArray)) * 100) = 60 Or _
                ((ArrayPos / UBound(CustBillArray)) * 100) = 80 Then

            MsgBox "Currently on " & Fix((ArrayPos / UBound(CustBillArray)) * 100) & "%"

        End If



    Next ArrayPos

    MismatchSheet.UsedRange.EntireColumn.AutoFit

    MsgBox "Complete!"

End Sub
Sub Mismatch()
    ' 07 Apr 2017

    Dim WsAut As Worksheet, WsMis As Worksheet
    Dim Last As Long                                ' WsAut.LastRow
    Dim C As Long                                   ' WsAut.Column

    Set WsAut = Sheets("Authorizations Issued")
    Set WsMis = Sheets("Mismatch")

    With WsAut
        Last = .UsedRange.Rows.Count
        .Range("A1:BI1").Copy Destination:=WsMis.Cells(1, 1)
'        consider this instead:-
'        Last = .Cells(.Rows.Count, "A").End(xlUp).Row

        'find Mismatch
        For C = 2 To Last
            If .Cells(2, C).Value = "Cust Bill To ID" Then
    '            ActiveCell.Offset(i, 0).Select
                BTID(i) = Selection.Value
            End If
        Next i
    End With
End Sub
Sub Mismatch()

Dim authSht As Worksheet ' Renamed this variable
Dim misSht As Worksheet ' Added a worksheet variable
Dim i As Integer
Dim k As Integer
Dim last As Integer
Dim BTID(1 To 1) As String ' We'll dim the arrays 1-based to avoid ninja arithmetics in the loop later
Dim CMF(1 To 1) As String
Dim rng1 As Range ' Added this variable
Dim rng2 As Range ' Added this variable

Set authSht = ThisWorkbook.Worksheets("Authorizations Issued")
Set misSht = ThisWorkbook.Worksheets("Mismatch")

''find Mismatch
authSht.Range("A1:BI1").Copy
misSht.Range("A1").Paste

last = authSht.UsedRange.Rows.Count

Set rng1 = authSht.Range("A2")
Set rng2 = rng1

For Each c In Range(rng1, authSht.Range(rng1.Row, authSht.Cells(1, Columns.Count).End(xlToLeft).Column))
    If c.Value = "Cust Bill To ID" Then Set rng1 = c
    If c.Value = "SAP CMF#" Then Set rng2 = c
Next c

For i = 1 To last
    BTID(i) = rng1.Offset(i, 0).Value
    CMF(i) = rng2.Offset(i, 0).Value

    If i < last Then ' If the loop will be executed at least 1 more time :
        ReDim Preserve BTID(1 To i + 1) ' .. resize the arrays to make room for the next entry
        ReDim Preserve CMF(1 To i + 1)
    End If
Next i


Dim l As Integer
l = 2

For k = 3 To last
    If BTID(k) <> CMF(k) Then
        authSht.Range("$A$" & k & ":$BH$" & k).Copy
        misSht.Range("$A$" & l).Paste
        l = l + 1
    Else
        l = l
    End If
Next k

misSht.UsedRange.EntireColumn.AutoFit

End Sub