当 Excel 行更改为不同的字母时,如何在 VBA 中使用 msgbox 进行提示。
例如,如果我在第 1 行有 AAB,在第 2 行有 BBA,则我需要在第 1 行后有一个提示框。
答案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
前:
之后: