在VBA中比较多个数组值的最有效方法是什么?

时间:2016-01-08 20:26:46

标签: arrays excel vba performance search

在开始之前,请允许我说我知道这可能不是最好用excel和VBA完成的,但是我的双手在这方面都很紧张。

我有一个29列的电子表格(总是),每天有100到10,000行。

我需要一种基于多个条件自动将行从一个工作表移动到另一个工作表的方法。基本逻辑是它需要搜索第一张纸,如果C OR D等于某个文本值,并且Y包含某个数值,则需要将其添加到第二张纸并从第一张纸中删除。

问题是我已经构建了一个测试版本,它只查看三个标准中的一个,而且它已经很慢了 - 如果我有两个以上的阵列和/和/或逻辑,我不认为这将在10,000行日内使用。

我该怎么做才能加快速度?我觉得必须有比数组和循环更好/更快的方式。我非常喜欢新手,所以我确信这是一个非常糟糕的解决方案。

Option Explicit

Sub MacroSoFar()

Dim lColumnLength As Long
Dim intArrayCounter As Integer
Dim TestArray(165) As String

TestArray(0) = "4"
TestArray(1) = "5"
TestArray(2) = "6"
TestArray(3) = "7"
TestArray(4) = "8"
TestArray(5) = "9"
TestArray(6) = "10"
TestArray(7) = "11"
TestArray(8) = "12"
TestArray(9) = "14"
TestArray(10) = "19"
TestArray(11) = "20"
TestArray(12) = "21"
TestArray(13) = "25"
TestArray(14) = "30"
TestArray(15) = "35"
TestArray(16) = "36"
TestArray(17) = "37"
TestArray(18) = "41"
TestArray(19) = "42"
TestArray(20) = "43"
TestArray(21) = "46"
TestArray(22) = "47"
TestArray(23) = "48"
TestArray(24) = "51"
TestArray(25) = "52"
TestArray(26) = "53"
TestArray(27) = "60"
TestArray(28) = "63"
TestArray(29) = "65"
TestArray(30) = "66"
TestArray(31) = "67"
TestArray(32) = "68"
TestArray(33) = "69"
TestArray(34) = "70"
TestArray(35) = "71"
TestArray(36) = "72"
TestArray(37) = "73"
TestArray(38) = "74"
TestArray(39) = "75"
TestArray(40) = "76"
TestArray(41) = "77"
TestArray(42) = "78"
TestArray(43) = "79"
TestArray(44) = "80"
TestArray(45) = "81"
TestArray(46) = "82"
TestArray(47) = "83"
TestArray(48) = "84"
TestArray(49) = "85"
TestArray(50) = "86"
TestArray(51) = "87"
TestArray(52) = "88"
TestArray(53) = "89"
TestArray(54) = "90"
TestArray(55) = "91"
TestArray(56) = "92"
TestArray(57) = "93"
TestArray(58) = "94"
TestArray(59) = "96"
TestArray(60) = "97"
TestArray(61) = "98"
TestArray(62) = "99"
TestArray(63) = "101"
TestArray(64) = "102"
TestArray(65) = "103"
TestArray(66) = "106"
TestArray(67) = "107"
TestArray(68) = "108"
TestArray(69) = "109"
TestArray(70) = "111"
TestArray(71) = "112"
TestArray(72) = "113"
TestArray(73) = "115"
TestArray(74) = "116"
TestArray(75) = "117"
TestArray(76) = "118"
TestArray(77) = "121"
TestArray(78) = "122"
TestArray(79) = "125"
TestArray(80) = "129"
TestArray(81) = "130"
TestArray(82) = "131"
TestArray(83) = "132"
TestArray(84) = "133"
TestArray(85) = "134"
TestArray(86) = "137"
TestArray(87) = "138"
TestArray(88) = "142"
TestArray(89) = "143"
TestArray(90) = "144"
TestArray(91) = "145"
TestArray(92) = "146"
TestArray(93) = "147"
TestArray(94) = "155"
TestArray(95) = "156"
TestArray(96) = "157"
TestArray(97) = "158"
TestArray(98) = "159"
TestArray(99) = "161"
TestArray(100) = "162"
TestArray(101) = "169"
TestArray(102) = "170"
TestArray(103) = "173"
TestArray(104) = "174"
TestArray(105) = "175"
TestArray(106) = "176"
TestArray(107) = "180"
TestArray(108) = "181"
TestArray(109) = "182"
TestArray(110) = "183"
TestArray(111) = "184"
TestArray(112) = "185"
TestArray(113) = "186"
TestArray(114) = "187"
TestArray(115) = "188"
TestArray(116) = "189"
TestArray(117) = "190"
TestArray(118) = "192"
TestArray(119) = "193"
TestArray(120) = "194"
TestArray(121) = "195"
TestArray(122) = "196"
TestArray(123) = "201"
TestArray(124) = "202"
TestArray(125) = "205"
TestArray(126) = "206"
TestArray(127) = "207"
TestArray(128) = "208"
TestArray(129) = "211"
TestArray(130) = "212"
TestArray(131) = "214"
TestArray(132) = "215"
TestArray(133) = "217"
TestArray(134) = "218"
TestArray(135) = "220"
TestArray(136) = "221"
TestArray(137) = "222"
TestArray(138) = "223"
TestArray(139) = "224"
TestArray(140) = "225"
TestArray(141) = "226"
TestArray(142) = "228"
TestArray(143) = "229"
TestArray(144) = "230"
TestArray(145) = "235"
TestArray(146) = "236"
TestArray(147) = "237"
TestArray(148) = "240"
TestArray(149) = "241"
TestArray(150) = "242"
TestArray(151) = "244"
TestArray(152) = "249"
TestArray(153) = "250"
TestArray(154) = "251"
TestArray(155) = "255"
TestArray(156) = "256"
TestArray(157) = "259"
TestArray(158) = "260"
TestArray(159) = "262"
TestArray(160) = "263"
TestArray(161) = "264"
TestArray(162) = "265"
TestArray(163) = "266"
TestArray(164) = "267"
TestArray(165) = "269"


For intArrayCounter = 0 To 165 Step 1
    For lColumnLength = Cells(Rows.Count, 25).End(xlUp).Row To 1 Step -1 
        If InStr(Cells(lColumnLength, 25), TestArray(intArrayCounter)) > 0 Then
            Range("a" & lColumnLength & ":AC" & lColumnLength).Copy Sheet10.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            Cells(lColumnLength, 29).EntireRow.Delete
        End If
    Next
 Next
End Sub

1 个答案:

答案 0 :(得分:0)

我没有测试过这个,但我得跑了。

但它可能会给你一些思考的食物。

Option Explicit

Sub MacroSoFar()

    Dim lColumnLength As Long
    Dim intArrayCounter As Integer
    Dim TestArray(165) As String

    TestArray(0) = "4"
    TestArray(1) = "5"
    TestArray(2) = "6"
    TestArray(3) = "7"
    TestArray(4) = "8"
    TestArray(5) = "9"
    TestArray(6) = "10"
    TestArray(7) = "11"
    TestArray(8) = "12"
    TestArray(9) = "14"
    TestArray(10) = "19"
    TestArray(11) = "20"
    TestArray(12) = "21"
    TestArray(13) = "25"
    TestArray(14) = "30"
    TestArray(15) = "35"
    TestArray(16) = "36"
    TestArray(17) = "37"
    TestArray(18) = "41"
    TestArray(19) = "42"
    TestArray(20) = "43"
    TestArray(21) = "46"
    TestArray(22) = "47"
    TestArray(23) = "48"
    TestArray(24) = "51"
    TestArray(25) = "52"
    TestArray(26) = "53"
    TestArray(27) = "60"
    TestArray(28) = "63"
    TestArray(29) = "65"
    TestArray(30) = "66"
    TestArray(31) = "67"
    TestArray(32) = "68"
    TestArray(33) = "69"
    TestArray(34) = "70"
    TestArray(35) = "71"
    TestArray(36) = "72"
    TestArray(37) = "73"
    TestArray(38) = "74"
    TestArray(39) = "75"
    TestArray(40) = "76"
    TestArray(41) = "77"
    TestArray(42) = "78"
    TestArray(43) = "79"
    TestArray(44) = "80"
    TestArray(45) = "81"
    TestArray(46) = "82"
    TestArray(47) = "83"
    TestArray(48) = "84"
    TestArray(49) = "85"
    TestArray(50) = "86"
    TestArray(51) = "87"
    TestArray(52) = "88"
    TestArray(53) = "89"
    TestArray(54) = "90"
    TestArray(55) = "91"
    TestArray(56) = "92"
    TestArray(57) = "93"
    TestArray(58) = "94"
    TestArray(59) = "96"
    TestArray(60) = "97"
    TestArray(61) = "98"
    TestArray(62) = "99"
    TestArray(63) = "101"
    TestArray(64) = "102"
    TestArray(65) = "103"
    TestArray(66) = "106"
    TestArray(67) = "107"
    TestArray(68) = "108"
    TestArray(69) = "109"
    TestArray(70) = "111"
    TestArray(71) = "112"
    TestArray(72) = "113"
    TestArray(73) = "115"
    TestArray(74) = "116"
    TestArray(75) = "117"
    TestArray(76) = "118"
    TestArray(77) = "121"
    TestArray(78) = "122"
    TestArray(79) = "125"
    TestArray(80) = "129"
    TestArray(81) = "130"
    TestArray(82) = "131"
    TestArray(83) = "132"
    TestArray(84) = "133"
    TestArray(85) = "134"
    TestArray(86) = "137"
    TestArray(87) = "138"
    TestArray(88) = "142"
    TestArray(89) = "143"
    TestArray(90) = "144"
    TestArray(91) = "145"
    TestArray(92) = "146"
    TestArray(93) = "147"
    TestArray(94) = "155"
    TestArray(95) = "156"
    TestArray(96) = "157"
    TestArray(97) = "158"
    TestArray(98) = "159"
    TestArray(99) = "161"
    TestArray(100) = "162"
    TestArray(101) = "169"
    TestArray(102) = "170"
    TestArray(103) = "173"
    TestArray(104) = "174"
    TestArray(105) = "175"
    TestArray(106) = "176"
    TestArray(107) = "180"
    TestArray(108) = "181"
    TestArray(109) = "182"
    TestArray(110) = "183"
    TestArray(111) = "184"
    TestArray(112) = "185"
    TestArray(113) = "186"
    TestArray(114) = "187"
    TestArray(115) = "188"
    TestArray(116) = "189"
    TestArray(117) = "190"
    TestArray(118) = "192"
    TestArray(119) = "193"
    TestArray(120) = "194"
    TestArray(121) = "195"
    TestArray(122) = "196"
    TestArray(123) = "201"
    TestArray(124) = "202"
    TestArray(125) = "205"
    TestArray(126) = "206"
    TestArray(127) = "207"
    TestArray(128) = "208"
    TestArray(129) = "211"
    TestArray(130) = "212"
    TestArray(131) = "214"
    TestArray(132) = "215"
    TestArray(133) = "217"
    TestArray(134) = "218"
    TestArray(135) = "220"
    TestArray(136) = "221"
    TestArray(137) = "222"
    TestArray(138) = "223"
    TestArray(139) = "224"
    TestArray(140) = "225"
    TestArray(141) = "226"
    TestArray(142) = "228"
    TestArray(143) = "229"
    TestArray(144) = "230"
    TestArray(145) = "235"
    TestArray(146) = "236"
    TestArray(147) = "237"
    TestArray(148) = "240"
    TestArray(149) = "241"
    TestArray(150) = "242"
    TestArray(151) = "244"
    TestArray(152) = "249"
    TestArray(153) = "250"
    TestArray(154) = "251"
    TestArray(155) = "255"
    TestArray(156) = "256"
    TestArray(157) = "259"
    TestArray(158) = "260"
    TestArray(159) = "262"
    TestArray(160) = "263"
    TestArray(161) = "264"
    TestArray(162) = "265"
    TestArray(163) = "266"
    TestArray(164) = "267"
    TestArray(165) = "269"

    Dim oSheet As Variant, nSheet As Variant, oList As New Collection, nList As New Collection

    oSheet = Range("A1:AC" & Cells(Rows.Count, 25).End(xlUp).Row).Value
    For intArrayCounter = 0 To 165 Step 1
        For lColumnLength = Cells(Rows.Count, 25).End(xlUp).Row To 1 Step -1
            If InStr(oSheet(lColumnLength, 25), TestArray(intArrayCounter)) > 0 Then
                ' Add to list in order
                nList.Add Range("a" & lColumnLength & ":AC" & lColumnLength).Value
                Else
                ' Add to list in reverse order
                oList.Add Range("a" & lColumnLength & ":AC" & lColumnLength).Value
            End If
        Next
    Next
    For i = oList.Count To 1 Step -1
        For j = 1 To 29
            oSheet(i, j) = oList(i)(1, j)
        Next j
    Next i
    Range("A1:AC" & Cells(Rows.Count, 25).End(xlUp).Row) = oSheet
    Range("A" & oList.Count + 1 & ":A" & Cells(Rows.Count, 25).End(xlUp).Row).EntireRow.Delete Shift:=xlUp

    nSheet = Sheet10.Range("A1:AC" & nList.Count).Offset(Sheet10.Range("A" & Sheet10.UsedRange.Rows.Count).End(xlUp).Row).Value
    For i = nList.Count To 1
        For j = 1 To 29
            nSheet(i, j) = nList(i)(1, j)
        Next j
    Next i
    Sheet10.Range("A1:AC" & nList.Count).Offset(Sheet10.Range("A" & Sheet10.UsedRange.Rows.Count).End(xlUp).Row) = nSheet

    Set nList = Nothing: Set oList = Nothing:Set oSheet = Nothing: Set nSheet = Nothing

End Sub
相关问题