- To post as a guest, your comment is unpublished.· 1 years agoHi, Wilco,
To insert the page break but skip the first 20 rows, you just need to change one parameter as below code:
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)
Please try, thank you!
How to insert page breaks when value changes in Excel?
Supposing, I have a range of cells, and now, I want to insert page breaks into the worksheet when values in column A changes as left screenshot shown. Of course, you can insert them one by one, but are there any quick ways to insert the page breaks at once based on the changed values of one column?
The following VBA code can help you to insert page breaks below based on once column data changes, please do as follows:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Insert page breaks when the value changes:
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. Then press F5 key to run this code, all page breaks have been inserted into the sheet when data changes in column A. See screenshot:
After installing Kutools for Excel, please do as this:
1. Click Kutools > Format > Distinguish differences, see screenshot:
2. In the popped out Distinguish differences by key column dialog box, do the following operations:
- Select the data range and key column that you want to use;
- Choose Page Break from the Options box.
3. Then click Ok button, the page breaks have been inserted into the range based on the value changes see screenshot:
More relative articles:
- Highlight Rows When Cell Value Changes In Excel
- If there is a list of repeated values in your worksheet, and you need to highlight the rows based on column A which cell value changes as the following screenshot shown. In fact, you can quickly and easily finish this job by using the Conditional Formatting feature.
- Increment Numbers When Value Changes In Another Column
- Supposing, you have a list of values in column A, and now you want to increment number by 1 in column B when the value in column A changes, which means the numbers in column B increment until the value in column A changes, then the number increment starts from 1 again as left screenshot shown. In Excel, you can solve this job with the following method.
- Sum Cells When Value Changes In Another Column
- When you work on Excel worksheet, sometime, you may need to sum cells based on a group of data in another column. For example, here, I want to sum the orders in column B when the data changes in column A to get the following result. How could you solve this problem in Excel?
- Insert Blank Rows When Value Changes In Excel
- Supposing you have a range of data, and now you want to insert blank rows between the data when value changes, so that you can separate the sequential same values in one column as following screenshots shown. In this article, I will talk about some tricks for you to solve this problem.
- Run Macro When Cell Value Changes In Excel
- Normally, in Excel, we can press F5 key or Run button to execute the VBA code. But, have you ever tried to run the specific macro code when a cell value changes? This article, I will introduce some quick tricks to deal with this job in Excel.
You are guest
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 1 years agoIs 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.
- To post as a guest, your comment is unpublished.· 2 years agoThat 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!
- To post as a guest, your comment is unpublished.· 2 years agoSorry. EDIT: The first ROW is a column header.
- To post as a guest, your comment is unpublished.· 2 years agoHello, 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.