我正在尝试自动化插入行并复制,例如,如果单元格在单元格A中有“(4 SHEETS)”。我不知道如何开始这个。
如果aCell包含“(x SHEETS)”,则在aCell下面插入x行。复制aCell。将变量粘贴到x行中。下一步
最终我希望“(x SHEETS)”也可以重命名。所以“(4 SHEETS)”会变成4行,“(4 SHEETS)”被修改为“(SHEET 1)”,“(SHEET 2)”等。
感谢您的帮助
答案 0 :(得分:2)
假设aCell位置在A1(单元格地址[1,1])中,则获取aCell值,从aCell值中取出数字,然后根据aCell值创建行数。
Sub Main()
Dim aCell As String
Dim ws As Worksheet
Dim i As Long
Dim noOfSheet As String
Set ws = ThisWorkbook.Sheets("Sheet1")
aCell = ws.Cells(1, 1).Value
'Extract the no. of sheets from the cell
For i = 1 To Len(aCell)
If Mid(aCell, i, 1) >= "0" And Mid(aCell, i, 1) <= "9" Then
noOfSheet = noOfSheet + Mid(aCell, i, 1)
End If
Next
'Insert no. of row based on the no. of sheets
If CInt(noOfSheet) > 0 Then
For i = 1 To CInt(noOfSheet)
ws.Cells(1, 1).Offset(1, 0).EntireRow.Insert
ws.Cells(1, 1).Offset(1, 0).Value = "(SHEET " & CInt(noOfSheet) + 1 - i & ")"
Next i
End If
End Sub