我所寫的程式如下:
' Macro11 巨集表
'
Dim i As Single
Dim j As Single
Dim k As Single
Dim h As Single
Dim x As Range, Y As Range, rng As Range, r As Long '宣告變數
For j = 1 To 3
For i = 1 To 300
If Cells(1 + i, 38) = Cells(1, 52 + j) Then
Cells(1 + i, 52 + j) = Cells(1 + i, 8)
End If
Next i
Next j
Call tt
End Sub
Public Sub tt()
Dim x As Range, Y As Range, rng As Range, r As Long '宣告變數
Dim y1 As Range, y2 As Range
For h = 1 To 3
r = Cells(65536, 52 + h).End(xlUp).Row '取的A欄最大列位
Select Case h
Case 1
Set rng = Range("ba2:ba" & r) '取得資料範圍
For Each x In rng '資料範圍內每一儲存格
If x = "" Then '若儲存格值為""
If Y Is Nothing Then 'Y若不存在
Set Y = x '將Y設定為x
Else '若Y存在
Set Y = Union(Y, x) '結合Y及x成為新的範圍Y
End If
End If
Next
If Not Y Is Nothing Then Y.Delete (xlUp) '若Y存在將Y刪除
Case 2
Set rng = Range("bb2:bb" & r) '取得資料範圍
For Each x In rng '資料範圍內每一儲存格
If x = "" Then '若儲存格值為""
If y1 Is Nothing Then 'Y若不存在
Set y1 = x '將Y設定為x
Else '若Y存在
Set y1 = Union(y1, x) '結合Y及x成為新的範圍Y
End If
End If
Next
If Not y1 Is Nothing Then y1.Delete (xlUp) '若Y存在將Y刪除
Case 3
Set rng = Range("bc2:bc" & r) '取得資料範圍
For Each x In rng '資料範圍內每一儲存格
If x = "" Then '若儲存格值為""
If y2 Is Nothing Then 'Y若不存在
Set y2 = x '將Y設定為x
Else '若Y存在
Set y2 = Union(y2, x) '結合Y及x成為新的範圍Y
End If
End If
Next
If Not y2 Is Nothing Then y2.Delete (xlUp) '若Y存在將Y刪除
End Select
Next h
End Sub
回復 1#jntseng
Public Sub tt()
Dim x As Range, Y As Range, rng As Range, r As Long '宣告變數
Dim y1 As Range, y2 As Range
Dim Ar, h%
Ar = Array("ba2:ba", "bb2:bb", "bc2:bc")
For h = 0 To UBound(Ar)
r = Cells(65536, 52 + h+1).End(xlUp).Row '取的A欄最大列位
Set rng = Range(Ar(h) & r) '取得資料範圍
For Each x In rng '資料範圍內每一儲存格
If x = "" Then '若儲存格值為""
If Y Is Nothing Then 'Y若不存在
Set Y = x '將Y設定為x
MsgBox Y.Address
Else '若Y存在
Set Y = Union(Y, x) '結合Y及x成為新的範圍Y
End If
End If
Next
If Not Y Is Nothing Then Y.Delete (xlUp) '若Y存在將Y刪除
Next
End Sub作者: jntseng 時間: 2010-8-6 22:07