VBA 代码
Sub loopchange()
'select the blank column
Dim level As String
Range("AF2").Select
Do
'move to A2 cell'
ActiveCell.Offset(0, -31).Select
'find out which is level2
If (ActiveCell.Value = "..2") Then
'move to column AD
ActiveCell.Offset(0, 28).Select
'Store the first 4 digit for level 2
level = Left(ActiveCell.Value, 4)
'Move to the column A3
ActiveCell.Offset(1, -28).Select
End If
MsgBox Left(ActiveCell.Value, 4)
'compare whether it's level 3(VBA don't detect this)
If (ActiveCell.Value = "...3") Then
'move to the column AD
ActiveCell.Offset(0, 28).Select
'compare the stored first 4 digit in level 2 known as level to first
'4 digit of current cell
If (level = Left(ActiveCell.Value, 4)) Then
'move to column AF
ActiveCell.Offset(0, 2).Select
'input the word NO CTH
ActiveCell.Value = "No CTH"
End If
End If
Loop Until IsEmpty(ActiveCell.Offset(0, -31))
End Sub
我的理由是将级别 2(..2) 的前四位数字与级别 3(...3) 的前四位数字进行比较,然后使用 Do 循环将世界“无 CTH”输入到 AF3 列中。
(PS:我的代码卡在“If (ActiveCell.Value = “...3”) Then”处,vba 代码不会运行该代码,而是直接进入 End if 语句)
答案1
答案2
Sub loopchange()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim level As Integer
Range("AF3").Select
Do
ActiveCell.Offset(0, -31).Select
If ((Right(ActiveCell.Value, 1)) = 2) Then
ActiveCell.Offset(0, 28).Select
level = Left(ActiveCell.Value, 4)
ActiveCell.Offset(1, -28).Select
End If
If ((Right(ActiveCell.Value, 1)) = 3) Then
ActiveCell.Offset(0, 28).Select
If (level = Left(ActiveCell.Value, 4)) Then
ActiveCell.Offset(0, 3).Select
ActiveCell.Value = "No CTH"
ActiveCell.Offset(0, -3).Select
ElseIf (Levels <> Left(ActiveCell.Value, 4)) Then
ActiveCell.Offset(0, 3).Select
ActiveCell.Value = ""
ActiveCell.Offset(0, -3).Select
End If
ElseIf ((Right(ActiveCell.Value, 1)) <> 2 Or (Right(ActiveCell.Value, 1)) <> 3) Then
ActiveCell.Offset(0, 28).Select
End If
ActiveCell.Offset(1, 3).Select
Loop Until IsEmpty(ActiveCell.Offset(0, -31))
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
@ejbytes 我已经解决了。非常感谢你这几天的指导:D 祝你有美好的一天
答案3
子循环改变() '选择空白列 暗淡级别作为字符串 范围("AF2").选择 做 '移至 A2 单元格' ActiveCell.Offset(0, -31).选择 '找出哪个是 level2 MsgBox ("单元格 = ..2?" & (ActiveCell.Value = "..2")) 如果 (ActiveCell.Value = "..2") 那么 '移至 AD 列 ActiveCell.偏移(0,28)。选择 '存储 2 级的前 4 位数字 级别 = 左(ActiveCell.Value,4) MsgBox ("级别设置为:" & level) '移至 A3 列 **向下移动?** ActiveCell.Offset(1,-28).选择 万一 MsgBox ("ColA = " & (Left(ActiveCell.Value, 4))) '比较它是否为 3 级(VBA 不会检测这一点) MsgBox ("单元格 = ...3?" & (ActiveCell.Value = "...3")) 如果 (ActiveCell.Value = "...3") 那么 '移至 AD 列 ActiveCell.偏移(0,28)。选择 '将级别 2 中存储的前 4 位数字与第一个 '当前单元格的 4 位数字 MsgBox (level & "=" & Left(ActiveCell.Value, 4) & "? " & (level = Left(ActiveCell.Value, 4))) 如果 (level = Left(ActiveCell.Value, 4)) 那么 '移至 AF 列 ActiveCell.偏移(0,3)。选择 ActiveCell.Value = “无 CTH” 万一 万一 '向下移动一行。 ActiveCell.偏移(1,0)。选择 循环直到 IsEmpty(ActiveCell.Value) 子目录结束