excel填充两个相同单元格之间的单元格

excel填充两个相同单元格之间的单元格,excel,vba,Excel,Vba,我有一个专栏,如下所示: A1 Sometext1 A2 0 A3 Sometext1 A4 Someothertext2 A5 0 A6 0 A7 Someothertext2 我想用文本本身来填补两个相同单元格之间的空白(如下所示)。我该怎么做?我尝试了一些想法,但没有一个奏效 A1 Sometext1 A2 Sometext1 A3 Sometext1 A4 Someothertext2 A5 Someothertext2 A6 Someo

我有一个专栏,如下所示:

A1 Sometext1
A2         0
A3 Sometext1
A4 Someothertext2
A5         0
A6         0
A7 Someothertext2
我想用文本本身来填补两个相同单元格之间的空白(如下所示)。我该怎么做?我尝试了一些想法,但没有一个奏效

A1 Sometext1
A2 Sometext1
A3 Sometext1
A4 Someothertext2
A5 Someothertext2
A6 Someothertext2
A7 Someothertext2

请尝试下一个代码。它在D:D列中返回,只是为了检查返回的数组是否是您所需要的:

Sub FillTheSameString()
  Dim sh As Worksheet, lastR As Long, arr, strTheSame As String, i As Long
  
  Set sh = ActiveSheet
  lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row
  arr = sh.Range("B1:B" & lastR).Value
  
  strTheSame = arr(1, 1)
  For i = 2 To UBound(arr) - 1
    If arr(i, 1) = strTheSame Then
        strTheSame = arr(i + 1, 1): i = i + 1
    Else
        arr(i, 1) = strTheSame
    End If
  Next i
  sh.Range("D1").Resize(UBound(arr), 1).Value = arr
End Sub

如果代码返回您想要的内容,请使用
sh.Range(“B1”)更改
sh.Range(“D1”).Resize…
。Resize…

请向我们展示您的尝试。它有效。谢谢您!