跳到主要內容

如何在Excel中以固定間隔插入特定數量的行?

在Excel工作表中,可以使用插入功能在現有行之間插入空白行。 但是,如果您有大量數據,並且需要在每第三個或第n行之後插入兩個空白行,那麼如何快速便捷地完成此工作?


使用VBA代碼以固定的間隔將特定數量的空白行插入數據范圍

以下VBA代碼可以幫助您在現有數據中的第n行之後插入特定數量的行。 請執行以下操作:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:以固定的間隔將特定數量的行插入數據

Sub InsertRowsAtIntervals()
'Updateby Extendoffice
Dim Rng As Range
Dim xInterval As Integer
Dim xRows As Integer
Dim xRowsCount As Integer
Dim xNum1 As Integer
Dim xNum2 As Integer
Dim WorkRng As Range
Dim xWs As Worksheet
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
xRowsCount = WorkRng.Rows.Count
xInterval = Application.InputBox("Enter row interval. ", xTitleId, 1, Type:=1)
xRows = Application.InputBox("How many rows to insert at each interval? ", xTitleId, 1, Type:=1)
xNum1 = WorkRng.Row + xInterval
xNum2 = xRows + xInterval
Set xWs = WorkRng.Parent
For i = 1 To Int(xRowsCount / xInterval)
    xWs.Range(xWs.Cells(xNum1, WorkRng.Column), xWs.Cells(xNum1 + xRows - 1, WorkRng.Column)).Select
    Application.Selection.EntireRow.Insert
    xNum1 = xNum1 + xNum2
Next
End Sub

3. 粘貼此代碼後,請按 F5 鍵以運行此代碼,將彈出提示框,提醒您選擇要插入空白行的數據范圍,請參見屏幕截圖:

4。 點擊 OK 按鈕,將彈出另一個提示框,請輸入行間隔的數量,請參見屏幕截圖:

5。 繼續點擊 OK 按鈕,在下面彈出的提示框中,請輸入要插入的空白行數,請參見屏幕截圖:

6。 然後點擊 OK,並且空白行已定期插入到現有數據中,請參見屏幕截圖:


使用VBA代碼根據單元格值將特定數量的空白行插入數據范圍

有時,您可能需要根據單元格值列表插入空白行,在這種情況下,下面的VBA代碼可以幫您一個忙,請這樣做:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:根據數字列表插入特定數量的空白行:

Sub Insertblankrowsbynumbers ()
'UpdatebyExtendoffice
Dim xRg As Range
Dim xAddress As String
Dim I, xNum, xLastRow, xFstRow, xCol, xCount As Long
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select the spefic number column to use(single column):", "Kutools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
xLastRow = xRg(1).End(xlDown).Row
xFstRow = xRg.Row
xCol = xRg.Column
xCount = xRg.Count
Set xRg = xRg(1)
For I = xLastRow To xFstRow Step -1
xNum = Cells(I, xCol)
If IsNumeric(xNum) And xNum > 0 Then
Rows(I + 1).Resize(xNum).Insert
xCount = xCount + xNum
End If
Next
xRg.Resize(xCount, 1).Select
Application.ScreenUpdating = True
End Sub

3。 粘貼此代碼後,按 F5 鍵以運行此代碼,在彈出的對話框中,選擇要基於其插入空白行的數字列表,請參見屏幕截圖:

4。 然後,點擊 OK,您將獲得所需的結果,如以下屏幕截圖所示:


使用方便的功能以固定的間隔將特定數量的空白行插入數據范圍

如果您不熟悉上述VBA代碼, Excel的Kutools 也可能對您有幫助 插入空白行和列 該功能可以按固定的間隔快速輕鬆地將特定數量的行或列插入現有數據。

注意:要應用此 插入空白行和列 ,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請執行以下操作:

1。 選擇要間隔插入空白行的數據范圍。

2。 點擊 庫工具 > 插入 > 插入空白行和列,請參見屏幕截圖:

3。 在 插入空白行和列 對話框中選擇 空白行 來自的選項 插入類型,然後指定要使用的間隔和空白行的數量,如下所示的屏幕截圖:

4。 然後點擊 OK 按鈕,並且空白行已以特定間隔插入到選定範圍內,如以下屏幕截圖所示:

立即下載和免費試用Excel的Kutools!


使用VBA代碼根據特定編號多次復制和插入行

假設您有一個tada範圍,現在,您想複製每行並將其基於數字列表多次粘貼到下一行,如下面的屏幕截圖所示。 如何在Excel工作表中解決此任務?

為了處理此工作,我將為您介紹一個有用的代碼,請執行以下步驟:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:根據特定數字多次復制和插入行:

Sub CopyRows()
'UpdatebyExtendoffice
Dim xRg As Range
Dim xCRg As Range
Dim xFNum As Integer
Dim xRN As Integer
On Error Resume Next
SelectRange:
xTxt = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select the list of numbers to copy the rows based on: ", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub

If xRg.Columns.Count > 1 Then
MsgBox "Please select single column!"
GoTo SelectRange
End If
Application.ScreenUpdating = False
For xFNum = xRg.Count To 1 Step -1
Set xCRg = xRg.Item(xFNum)
xRN = CInt(xCRg.Value)
With Rows(xCRg.Row)
.Copy
.Resize(xRN).Insert
End With
Next
Application.ScreenUpdating = True
End Sub

3。 粘貼此代碼後,按 F5 鍵以運行此代碼,在彈出的對話框中,選擇要復制的數字列表並基於其插入數據行,請參見屏幕截圖:

4。 然後,點擊 OK 按鈕,並且已將特定的行數複製並粘貼到每個原始行下,請參見屏幕截圖:


具有特定功能,可根據特定數字多次復制和插入行

如果你有 Excel的Kutools,其 根據單元格值複製行/列 功能,您可以根據數字列表快速輕鬆地插入行或列。

注意:要應用此 根據單元格值複製行/列,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請執行以下操作:

1。 點擊 庫工具 > 插入 > 根據單元格值複製行/列,請參見屏幕截圖:

2。 在 複製並插入行和列 對話框中選擇 複製並插入行 在選項 類別 部分,然後選擇要復制的數據范圍,然後指定要基於其複制行的值列表,請參見屏幕截圖:

4。 然後,點擊 Ok or 登記 按鈕,您將根據需要獲得以下結果:

立即下載和免費試用Excel的Kutools!

更多相關文章:

  • 多次復制和插入行或複制X次
  • 在日常工作中,您是否曾經嘗試過複製一行或每一行,然後在工作表中當前數據行的下方多次插入? 例如,我有一個單元格區域,現在,我想複製每一行並將它們粘貼3次到下一行,如下圖所示。 您如何在Excel中處理這項工作?
  • 在Excel中值更改時插入空白行
  • 假設您具有一定範圍的數據,現在您想在值更改時在數據之間插入空白行,以便可以將一列中的順序相同值分開,如下面的屏幕截圖所示。 在本文中,我將為您解決一些技巧。
  • 根據條件將多個工作表中的行複製到新工作表中
  • 假設您有一個包含三個工作表的工作簿,這些工作表的格式與下面的屏幕快照相同。 現在,您想要將這些工作表中的C列包含“已完成”文本的所有行複製到新工作表中。 您如何快速,輕鬆地解決此問題,而又不手動一一複制和粘貼它們?

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (39)
Rated 5 out of 5 · 2 ratings
This comment was minimized by the moderator on the site
Marvelous vba script!
I had over 5000 rows that i need to add new rows to in between. All other guides told me to make "helper" column it would take me good part of my life to add 1,2 copy paste over and over again just to add new rows.
So, Thanks for this!
Rated 5 out of 5
This comment was minimized by the moderator on the site
hola, hay algun codigo que me permita copiar los datos, pero que en la primera columna que son fechas puedan ser consecutivas.

ejemplo

en vez de que quede asi

10/01/2022 19.258.369-4 Juan Ramirez
10/01/2022 19.258.369-4 Juan Ramirez
10/01/2022 19.258.369-4 Juan Ramirez

quede asi

10/01/2022 19.258.369-4 Juan Ramirez
11/01/2022 19.258.369-4 Juan Ramirez
12/01/2022 19.258.369-4 Juan Ramirez

gracias
This comment was minimized by the moderator on the site
hola, hay algun codigo que me permita copiar los datos, pero que en la primera columna que son fechas pueda ser consecutivo.

ejemplo

en vez de que quede asi

01/10/2022 19.258.369-4 Juan Ramirez
01/10/2022 19.258.369-4 Juan Ramirez
01/10/2022 19.258.369-4 Juan Ramirez

quede asi

01/10/2022 19.258.369-4 Juan Ramirez
02/10/2022 19.258.369-4 Juan Ramirez
03/10/2022 19.258.369-4 Juan Ramirez

gracias
This comment was minimized by the moderator on the site
Name Email Phone Address
0 Name Email Phone Adress
address line 2 Name Phone 0
Name Email Phone Adress
0 Name Email Phone Adress
address line 2 0


How could I edit this to start a new row at every empty value or 0 value without having phone numbers with 0 start a new row?
This comment was minimized by the moderator on the site
Hello, Jarrod

Sorry, I can't get your problem clearly.
Could you explain your problem more detailed? Or you can insert a screenshot or file here.
Thank you!
This comment was minimized by the moderator on the site
Вот выручили так выручили!
Сидел, ломал голову как добавить строки по заданному количеству.
Ваш макрос мне очень помог.
Rated 5 out of 5
This comment was minimized by the moderator on the site
can you tell me how to insert column like this way , what is the code
This comment was minimized by the moderator on the site
Hello friend,
You can use this VBA code:

Sub InsertColumnsAtIntervals()

'Updateby Extendoffice

Dim Rng As Range

Dim xInterval As Integer

Dim xColumns As Integer

Dim xColumnsCount As Integer

Dim xNum1 As Integer

Dim xNum2 As Integer

Dim WorkRng As Range

Dim xWs As Worksheet

xTitleId = "KutoolsforExcel"

Set WorkRng = Application.Selection

Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)

xColumnsCount = WorkRng.Columns.Count

xInterval = Application.InputBox("Enter column interval. ", xTitleId, 1, Type:=1)

xColumns = Application.InputBox("How many columns to insert at each interval? ", xTitleId, 1, Type:=1)

xNum1 = WorkRng.Column + xInterval

xNum2 = xColumns + xInterval

Set xWs = WorkRng.Parent

For i = 1 To Int(xColumnsCount / xInterval)

    xWs.Range(xWs.Cells(WorkRng.Row, xNum1), xWs.Cells(WorkRng.Row, xNum1 + xColumns - 1)).Select

    Application.Selection.EntireColumn.Insert

    xNum1 = xNum1 + xNum2

Next

End Sub


Sincerely,
Mandy
This comment was minimized by the moderator on the site
Muito obrigado, salvou meu trabalho, eu não tinha ideia de como fazer. Muito obrigado mesmo!
This comment was minimized by the moderator on the site
Hello,
You are welcome. Glad it helps. Any questions, please feel free to contact us. Have a great day.
Sincerely,
Mandy
This comment was minimized by the moderator on the site
This is so Cool!! Thanks a lot
This comment was minimized by the moderator on the site
Can I get the VBA code for deletion of rows based on duplicate values in a selected column keeping all unique values?
This comment was minimized by the moderator on the site
Hello, Roy,If you want to remove rows based on duplicate values, normally, you can use the Remove Duplicates feature in Excel to remove the rows.Of course, if you need a VBA code, please use the below code: (First, you should select the data range that you want to remove, and then run this code, the rows based on the duplicate values in the first column of your selection will be removed at once. )<div data-tag="code">Sub Delete_duplicate_rows()
Dim Rng As Range
Set Rng = Selection
Rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End SubPlease try, hope it can help you!
This comment was minimized by the moderator on the site
Thank you author! You deserve the best commendation for these! But please could you help me out with the code to put a constant value into all blank rows I created with your code above? To make myself more clearer, I need to insert a constant value into all blank rows (this solved already with your code above) then I need to insert a constant value into all of the blank rows (this is my problem). Thank you as I expect your kind response.
This comment was minimized by the moderator on the site
Hello, Do you mean to fill blank rows with specific value? If so, mabe the following article cna help you:https://www.extendoffice.com/documents/excel/772-excel-fill-blank-cells-with-0-or-specific-value.html
Please try it.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations