|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 ning84 于 2018-9-20 23:26 编辑
最常用的方法是
If Rng(ii, 1) = Rr(ii1, 1) And Rng(ii, 2) = Rr(ii1, 2) And Rng(ii, 3) = Rr(ii1, 3) Then
- Sub Del3()
- Dim Rr As Range, Rr1 As Range
-
- Set Rr = Range("U4:W40")
- Set Rr1 = Range("X4:Z40")
- Rr1.ClearContents
- Dim ArrRng(2) As Range
- Set ArrRng(0) = Range("D4:D15")
- Set ArrRng(1) = Range("H4:I19")
- Set ArrRng(2) = Range("M4:O26")
- Dim Rng As Range
- For i = 0 To 2
- Set Rng = ArrRng(i)
- Debug.Print Rng.Address
- Select Case Rng.Columns.Count
- Case 1
- For ii = 1 To Rng.Rows.Count
- For ii1 = 1 To Rr.Rows.Count
- Debug.Print Rng(ii, 1).Address, Rr(ii1, 1).Address
- If Rng(ii, 1) = Rr(ii1, 1) Then
- 'Debug.Print Rng(ii, 1), Rr(ii, 1), Rng(ii1, 1).Address, Rr(ii1, 1).Address
- Rr1(ii1, 1) = Rng(ii, 2)
- 'Exit For
- Else
- 'Rr1(ii1, 1) = ""
- End If
- Next ii1
- Next ii
- Case 2
- For ii = 1 To Rng.Rows.Count
- For ii1 = 1 To Rr.Rows.Count
- If Rng(ii, 1) = Rr(ii1, 1) And Rng(ii, 2) = Rr(ii1, 2) Then
- Rr1(ii1, 2) = Rng(ii, 3)
- 'Exit For
- Else
- 'Rr1(ii1, 2) = ""
- End If
- Next ii1
- Next ii
- Case 3
- For ii = 1 To Rng.Rows.Count
- For ii1 = 1 To Rr.Rows.Count
- If Rng(ii, 1) = Rr(ii1, 1) And Rng(ii, 2) = Rr(ii1, 2) And Rng(ii, 3) = Rr(ii1, 3) Then
- Rr1(ii1, 3) = Rng(ii, 4)
- Else
- 'Rr1(ii1, 3) = ""
- End If
- Next ii1
- Next ii
- End Select
- Next i
-
- End Sub
复制代码
采用字符串连接方法,程序简化了不少。
Str1 = ""
Str2 = ""
For jj = 1 To Rng.Columns.Count
Str1 = Str1 & Rr(ii, jj)
Str2 = Str2 & Rng(ii1, jj)
Next jj
If Str1 = Str2 Then
- Sub Del4()
- Dim Rr As Range, Rr1 As Range
-
- Set Rr = Range("U4:W40")
- Set Rr1 = Range("X4:Z40")
- Rr1.ClearContents
- Dim ArrRng(2) As Range
- Set ArrRng(0) = Range("D4:D15")
- Set ArrRng(1) = Range("H4:I19")
- Set ArrRng(2) = Range("M4:O26")
- Dim Rng As Range
- Dim Str1, Str2
- For i = 0 To 2
- Set Rng = ArrRng(i)
- For ii = 1 To Rr.Rows.Count
- For ii1 = 1 To Rng.Rows.Count
- Str1 = ""
- Str2 = ""
- For jj = 1 To Rng.Columns.Count
- Str1 = Str1 & Rr(ii, jj)
- Str2 = Str2 & Rng(ii1, jj)
- Next jj
- 'Debug.Print Str1, Str2
- If Str1 = Str2 Then
- Rr1(ii, Rng.Columns.Count) = Rng(ii1, 1 + Rng.Columns.Count)
- End If
- Next ii1
- Next ii
- Next i
-
- End Sub
复制代码
观点,只有用字符串连接方法,是实现目标需的最佳解决方案之一。
|
|