簡體   English   中英

找不到為什么我的代碼崩潰了

[英]Can't find why my code crashes

我寫了這個代碼來比較A和B列 ,每當我有類似的值, 如果列A = B,然后在E,F和G中顯示A,B和C,如下圖所示:

在此輸入圖像描述

這是我正在使用的代碼:

Option Explicit

Sub Comparatif_Release()
Dim t1, t2, c
Dim d As Object
Dim i&, j&, l&
Dim f As Worksheet
Range("e1:g110000").Select
Selection.ClearContents

Set f = Sheets("Sheet1")
With f

    t1 = .Range("a1:a10000").Value
    t2 = .Range("b1:c10000").Value
End With

Set d = CreateObject("Scripting.Dictionary")
For i = LBound(t1) To UBound(t1)
For j = LBound(t2) To UBound(t2)
    If t1(i, 1) = t2(j, 1) Then
        d(t1(i, 1) & ":" & t2(j, 1) & ":" & t2(j, 2)) = d(t1(i, 1) & ":" & t2(j, 1) & ":" & t2(j, 2))

    End If
Next j
Next i

With f
    i = 1
    For Each c In d.Keys
        .Cells(i, "E").Resize(, 3).Value = Split(c, ":")

        i = i + 1
    Next c
End With

End Sub

使用這些公式,只需將其過濾掉。 它應該工作。 晚了,但以防萬一。

In column E1 :  =IF(A1=B1,A1,"")
In column F1 :  =IF(E1<>"",B1,"")
In Column G1 :  =If(E1<>"",C1,"")

一個簡單的VBA版本是:

Private Sub CommandButton1_Click()
Dim i As Long
lastrow = Sheet1.Range("a1048575").End(xlUp).Row
For i = 1 To lastrow
    If Sheet1.Cells(i, 2) = Sheet1.Cells(i, 1) Then
        Sheet1.Cells(i, 4) = Sheet1.Cells(i, 1)
        Sheet1.Cells(i, 5) = Sheet1.Cells(i, 2)
        Sheet1.Cells(i, 6) = Sheet1.Cells(i, 3)
    End If
Next
End Sub

當你說d()=d()時這個部分怎么樣? 哪兩個鍵看起來都一樣,可能不存在?

完整報價是:

If t1(i, 1) = t2(j, 1) Then d(t1(i, 1) & ":" & t2(j, 1) & ":" & t2(j, 2)) = d(t1(i, 1) & ":" & t2(j, 1) & ":" & t2(j, 2)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM