在列的每个单元格中添加分隔符

时间:2021-07-19 08:14:46

标签: vba

我有包含数字的 A 列,我想每 9 行添加一个字符“-”,而在 A 列的整个长度上每 10 行添加一个“*”,它可以变化并且可以完成吗?谢谢

Sub Copia()

Dim lrow_copy As Long
Dim i As Long, j As Long

lrow_copy = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row 

i = 1 


For j = 1 To lrow_copy Step 1 
    Sheets(2).Range(Sheets(2).Cells(j, "A"), Sheets(2).Cells(j + lrow_copy, "A")).copy 
    Sheets(2).Range(Sheets(2).Cells(i, 3), Sheets(2).Cells(i, 3)).PasteSpecial Paste:=xlPasteValues  'Paste range and transpose the copied range
    
   i = i + 1 & "-" 

Next j

Application.CutCopyMode = False

End Sub

enter image description here

1 个答案:

答案 0 :(得分:-1)

请尝试下一个代码:

Sub testMarkerAtTheEnd()
  Dim sh As Worksheet, lastR As Long, i As Long, j As Long, arr
  Set sh = Sheets(2)
  lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
  arr = sh.Range("A1:A" & lastR).value
  i = 1
  For j = 1 To UBound(arr)
     If i > 1 Then arr(j, 1) = arr(j, 1) & "-"
     i = i + 1
     If i = 8 Then i = 1
  Next j
  sh.Range("C1").Resize(UBound(arr), 1).value = arr
End Sub