當 Excel 行變更為不同字母時,如何在 VBA 中使用訊息方塊進行提示。
例如,如果我在 row1 中有 AAB,在 Row2 中有 BBA,那麼我需要在 row1 之後有一個提示框。
答案1
試試這個小宏,它是為列設定的C
Sub DataCheck()
Dim MyCol As String, rCheck As Range, r As Range
MyCol = "C"
Set rCheck = Intersect(ActiveSheet.UsedRange, Range(MyCol & ":" & MyCol))
For Each r In rCheck
If r.Row = 1 Then
Else
If r.Text <> r.Offset(-1, 0).Text Then
r.Select
MsgBox "Data changed in row #" & r.Row
End If
End If
Next r
End Sub
例如:
編輯#1:
此版本的程式碼將檢測列中的更改C並在它們之間插入一個“間隔”行:
Sub DataCheck2()
Dim MyCol As String, rCheck As Range, r As Range
Dim rInsert As Range
MyCol = "C"
Set rCheck = Intersect(ActiveSheet.UsedRange, Range(MyCol & ":" & MyCol))
Set rInsert = Nothing
For Each r In rCheck
If r.Row = 1 Then
Else
If r.Text <> r.Offset(-1, 0).Text Then
If rInsert Is Nothing Then
Set rInsert = r
Else
Set rInsert = Union(rInsert, r)
End If
End If
End If
Next r
If rInsert Is Nothing Then
Else
rInsert.EntireRow.Insert
End If
End Sub
前:
之後: