我試圖有一個宏來運行一列資料并為每個實體插入一行,它計數為“”,因此例如它將在 Joanne 上方插入另外 3 行

我目前在下面有這段代碼,但它不起作用,我不確定我是否在正確的軌道上,因為我認為它正在尋找“,”只是單元格中的唯一內容?任何幫助/指導將不勝感激。
Sub InsertRow()
Dim cell As Range
For Each cell In Range("E2:E9999")
If cell.Value = "," Then
cell.EntireRow.Insert
End If
Next cell
End Sub
uj5u.com熱心網友回復:
插入與逗號一樣多的行
Option Explicit
Sub InsertCommaRows()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
Application.ScreenUpdating = False
Dim cString As String
Dim CommasCount As Long
Dim r As Long
For r = lRow - 1 To 2 Step -1
Debug.Print ws.Cells(r, "E").Address(0, 0)
cString = CStr(ws.Cells(r, "E").Value)
CommasCount = Len(cString) - Len(Replace(cString, ",", ""))
If CommasCount > 0 Then
ws.Cells(r 1, "E").Resize(CommasCount).EntireRow _
.Insert xlShiftDown, xlFormatFromLeftOrAbove
End If
Next r
Application.ScreenUpdating = True
MsgBox "Comma-rows inserted.", vbInformation
End Sub
uj5u.com熱心網友回復:
此代碼計算逗號,然后在當前單元格的正下方插入相同數量的行。
Sub InsertRow()
Dim cell As Range
Dim iCommas As Integer
For Each cell In ActiveSheet.Range("E2:E9999")
iCommas = Len(cell.Value) - Len(Replace(cell.Value, ",", ""))
If iCommas > 0 Then
cell.Offset(1).Resize(iCommas).EntireRow.Insert xlDown
End If
Next cell
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439904.html
