跳到主要內容

在Excel中更改值時如何插入分頁符?

假設我有一個單元格區域,現在,當A列中的值發生變化時,我想在工作表中插入分頁符,如左圖所示。 當然,您可以一個接一個地插入分頁符,但是有沒有一種快速的方法可以根據一列的更改值一次插入分頁符?

使用VBA代碼更改值時插入分頁符

借助功能強大的功能更改值時插入分頁符


使用VBA代碼更改值時插入分頁符

以下VBA代碼可以幫助您根據列數據的更改在下面插入分頁符,請執行以下操作:

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

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

VBA代碼:值更改時插入分頁符:

Sub insertpagebreaks()
'updateby Extendoffice
    Dim I As Long, J As Long
    J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    For I = J To 2 Step -1
        If Range("A" & I).Value <> Range("A" & I - 1).Value Then
            ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)
        End If
    Next I
End Sub

3。 然後按 F5 鍵以運行此代碼,當A列中的數據更改時,所有分頁符都已插入到工作表中。請參見屏幕截圖:

注意:在上面的代碼中, A 是您要基於其插入分頁符的列標題,可以根據需要進行更改。

借助功能強大的功能更改值時插入分頁符

如果你有 Excel的Kutools,其 區分差異 功能,您可以根據值更改快速插入分頁符,空白行,底部邊框線或背景顏色。 點擊下載Kutools for Excel!

提示:要應用此 區分差異 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請這樣做:

1。 點擊 庫工具 > 格式 > 區分差異,請參見屏幕截圖:

2。 在彈出 按關鍵列區分差異 對話框中,進行以下操作:

  • 選擇要使用的數據范圍和鍵列;
  • 選擇 分頁符 來自 選項 框。

3。 然後點擊 Ok 按鈕,分頁符已根據值更改插入到範圍中,請參見屏幕截圖:

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


更多相關文章:

  • 在Excel中單元格值更改時突出顯示行
  • 如果工作表中有重複值的列表,則需要突出顯示基於列A的行,該行的單元格值會發生變化,如下圖所示。 實際上,您可以使用條件格式設置功能快速輕鬆地完成此工作。
  • 當值在另一列中更改時增加數字
  • 假設您在A列中有一個值列表,現在想在A列中的值更改時在B列中將數字遞增1,這意味著B列中的數字遞增直到A列中的值更改,然後數字增量再次從1開始,如左圖所示。 在Excel中,可以使用以下方法解決此任務。
  • 值在另一列中更改時對單元格求和
  • 在使用Excel工作表時,有時可能需要根據另一列中的一組數據對單元格求和。 例如,在這裡,我想對A列中的數據更改時B列中的訂單求和以獲得以下結果。 您如何在Excel中解決此問題?
  • 在Excel中值更改時插入空白行
  • 假設您具有一定範圍的數據,現在您想在值更改時在數據之間插入空白行,以便可以將一列中的順序相同值分開,如下面的屏幕截圖所示。 在本文中,我將為您解決一些技巧。
  • 在Excel中單元格值更改時運行宏
  • 通常,在Excel中,我們可以按F5鍵或“運行”按鈕執行VBA代碼。 但是,您是否曾經嘗試在單元格值更改時運行特定的宏代碼? 本文中,我將介紹一些快速技巧來處理Excel中的這項工作。

最佳辦公生產力工具

🤖 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 (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bonjour,

Merci pour le code qui fonctionne très bien, seulement je souhaite ajouter un saut de page après et non avant chaque changement de valeur... que faut-il changer dans le code ?

Merci beaucoup !
This comment was minimized by the moderator on the site
Tôi không muốn ngắt trang tại những hàng bị ẩn. Thì phải làm sao?
This comment was minimized by the moderator on the site
Hello, Lê Tuấn
To solve your problem, please apply the below VBA code:
Sub insertpagebreaks()
'updateby Extendoffice
Dim I As Long, J As Long
Dim xRg As Range
On Error Resume Next
J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set xRg = Range("A1:A" & J).SpecialCells(xlCellTypeVisible)

For I = J To 2 Step -1
If Range("A" & I).Value <> Range("A" & I - 1).Value Then
If Not Intersect(xRg, Range("A" & I)) Is Nothing Then
ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)
End If
End If
Next I
End Sub


Please try, hope it can help you!
This comment was minimized by the moderator on the site
Is it possible to skip the first 20 rows of the sheet? If been trying all kinds of stuff but my knowledge of VBA is to limited to adjuist the code myself.
This comment was minimized by the moderator on the site
Hi, Wilco,
To insert the page break but skip the first 20 rows, you just need to change one parameter as below code:

Sub insertpagebreaks()

'updateby Extendoffice

Dim I As Long, J As Long

J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

For I = J To 21 Step -1

If Range("A" & I).Value <> Range("A" & I - 1).Value Then

ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)

End If

Next I

End Sub

Please try, thank you!
This comment was minimized by the moderator on the site
That is truly brilliant! I followed along on your VB example and it worked! I also am an owner of KUTools so I will play with that too.

Very impressed and thanks!

Mike
This comment was minimized by the moderator on the site
Sorry. EDIT: The first ROW is a column header.
This comment was minimized by the moderator on the site
Hello, Thank you! The thought of VBA makes me sweat. I do have one problem with the results of this code though. The first column is a column header. The code treats this as a change. My first page only shows the column header, but he following pages are fine. FYI: This is for a monthly report and the number of rows for each criteria change monthly. Any Ideas? Thank you in advance.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations