如何在单元格循环中间插入字符

时间:2016-08-26 13:45:49

标签: excel-vba vba excel

 Sub Button1_Click()

Dim Data1 As String
Dim Data2 As String
Dim sFinal As String
Dim sLeft As String
Dim sRight As String


 'For i = 1 To Range("A" & Rows.Count).End(xlUp).Row

 Data1 = Range("A1").Value
 Data2 = "-"

 sLeft = Left(Data1, 2)
sRight = Right(Data1, Len(Data1) - 2)

sFinal = sLeft & Data2 & sRight


Range("A1").Value = sFinal



 'Next i


End Sub

我试图循环通过A列的值,在每个单元格中的两个字符之后插入“ - ”...但是我的“For Next i”循环有错误,我该如何解决?

1 个答案:

答案 0 :(得分:1)

这样更好:

Sub Button1_Click()
Dim i As Long

Application.ScreenUpdating = False

i = 1
Do Until i > Range("A1048576").End(xlUp).Row
Range("A" & i).Value = Left(Trim(Range("A" & i).Value), 2) & "-" & Right(Trim(Range("A" & i).Value), Len(Trim(Range("A" & i).Value)) - 2)
i = i + 1
Loop

Application.ScreenUpdating = True


End Sub