Excel 如何在不相同的文本行之间添加空格

Excel 如何在不相同的文本行之间添加空格,excel,vba,Excel,Vba,我正在寻找一种在行之间添加空格的方法。我唯一希望它添加空格的时候是当文本与上面的行不相同时。我有点麻烦,不能完全弄明白。以下是我到目前为止得到的信息: Sub Spaces() Dim cell As Range Dim Text1 As String Dim Text2 As String For Each cell In Selection Text1 = Cells(cell, 1).Text Text2 = Cells(cell -

我正在寻找一种在行之间添加空格的方法。我唯一希望它添加空格的时候是当文本与上面的行不相同时。我有点麻烦,不能完全弄明白。以下是我到目前为止得到的信息:

Sub Spaces()
    Dim cell As Range
    Dim Text1 As String
    Dim Text2 As String
        For Each cell In Selection
    Text1 = Cells(cell, 1).Text
    Text2 = Cells(cell - 1, 1).Text
    If InStr(1, cell, "-", 1) Then
        If Cells(cell, 1) <> Cells(cell - 1, 1) Then
        Else: Cells(cell + 1, 1).EntireRow.Insert
        End If
    End If
    Next    
End Sub
子空间()
暗淡单元格作为范围
Dim Text1作为字符串
Dim Text2作为字符串
对于选择中的每个单元格
Text1=单元格(单元格,1)。Text
Text2=单元格(单元格-1,1)。Text
如果InStr(1,单元格“-”,1),则
如果单元格(单元格,1)单元格(单元格-1,1),则
其他:单元格(单元格+1,1).EntireRow.Insert
如果结束
如果结束
下一个
端接头

如果您能提供任何关于我的错误的线索,我们将不胜感激。

在不做太多更改的情况下,您可以尝试以下方法:

Sub Spaces()
    Dim cell As Range
    Dim Text1 As String
    Dim Text2 As String
    For Each cell In Selection

        ' initialize first string
        Text1 = cell.Text

        ' initialize second string for first time through
        If Text2 = vbNullString Then Text2 = Text1

        ' perform test
        If InStr(1, Text1, "-", 1) Then
            If Text1 <> Text2 Then

                ' insert row
                cell.EntireRow.Insert
            End If
        End If

        ' set second string for next time through loop
        Text2 = Text1
    Next
End Sub
子空间()
暗淡单元格作为范围
Dim Text1作为字符串
Dim Text2作为字符串
对于选择中的每个单元格
'初始化第一个字符串
Text1=单元格。Text
'第一次通过初始化第二个字符串
如果Text2=vbNullString,则Text2=Text1
"试验",
如果指令(1,文本1,“-”,1)则
如果Text1 Text2那么
'插入行
cell.EntireRow.Insert
如果结束
如果结束
'通过循环为下一次设置第二个字符串
Text2=Text1
下一个
端接头

这是我用来分隔容纳不同管道尺寸的行的代码

Sub blastListSort()

Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", SearchOrder:=xlByColumns, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select


For rown = Selection.Rows.Count To 2 Step -1

Size = Cells(rown, 3).Value
Size2 = Cells(rown, 3).Offset(-1).Value
lastRow = Range("A" & Rows.Count).End(xlUp).Row

If Size2 <> Size Then
Cells(rown, 6).EntireRow.Select
Cells(rown, 6).EntireRow.Insert
End If


Next
End Sub
Sub-blastListSort()
范围(“A1”)。调整单元格大小。查找(内容:=“*”,搜索顺序:=xlRows_
SearchDirection:=xlPrevious,LookIn:=xlValues)。行_
Cells.Find(What:=“*”,SearchOrder:=xlByColumns_
SearchDirection:=xlPrevious,LookIn:=xlValues)。列)。选择
对于rown=Selection.Rows.Count到2步骤-1
大小=单元格(行,3)。值
Size2=单元格(行,3).Offset(-1).Value
lastRow=范围(“A”和Rows.Count).End(xlUp).Row
如果尺寸为2,那么
单元格(rown,6)。EntireRow.Select
单元格(rown,6).EntireRow.Insert
如果结束
下一个
端接头
希望有帮助!:)