VBA自动切换单元格.md 887 B

虚拟按键Enter
Application.SendKeys "{ENTER}"

在工作表的任意单元格中输入完成后,自动每两个字符强制换行
Private Sub Worksheet_Change(ByVal Target As Range)
Dim str1, str2
If Target.Count = 1 Then
str1 = Target.Value
For x = 1 To Len(str1) Step 2
If str2 = "" Then
str2 = Mid(str1, x, 2)
Else
str2 = str2 & Chr(13) & Mid(str1, x, 2)
End If
Next x
Application.EnableEvents = False
Target.Value = str2
Application.EnableEvents = True
End If
End Sub

如果你只需要前面两个字符换行,后面的不换行,代码修改如下
Private Sub Worksheet_Change(ByVal Target As Range)
Dim str1
If Target.Count = 1 Then
str1 = Target.Value
str1 = Left(str1, 2) & Chr(10) & Mid(str1, 3, Len(str1))
Application.EnableEvents = False
Target.Value = str1
Application.EnableEvents = True
End If
End Sub