Insert and xlDown messed up formatting

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Ok
here's the thing
I have something like this:
1636839403016.png

When i press second button from the left it executes this code:
VBA Code:
Private Sub NewPage_Click()

    Range("A2:P57").Select
    Selection.Copy
    Range("A2:D2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
it simply takes this template and put new one above this.
Unfortunately when the "old" template gets moved down it messes up the entire cell formattting
1636839653113.png

I have no idea how to fix this. If I delete newly created template everything moves up and formattign is back to normal.
Please help
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
It seems you're having merged cells. Perhaps it's better to take all columns into account, like in:

Rich (BB code):
Private Sub NewPage_Click()

    Range("A2:P57").Copy
    Range("A2:P2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
 
Upvote 0
It seems you're having merged cells. Perhaps it's better to take all columns into account, like in:

Rich (BB code):
Private Sub NewPage_Click()

    Range("A2:P57").Copy
    Range("A2:P2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Didnt help. It seems the issue is only with height property.
In the other tab all cells have the same height and i have the same new page function implemented, it works like a charm
 
Upvote 0
I fix that
The issue was in the line where the column is very streteched in tha last cell fo the row I had leftover data which wasnt showing due to cell size but it was inside.
After that I also made all the cells autosize themselves so there wasn't any otehr issues with formatting.
From that point everything works great.
Thanks for your assitance
 
Upvote 0
Solution
Glad you worked it out and thanks for the follow-up.

In case you are still interested in a solution other than using Excel's AutoFit (which sometimes doesn't have the effect one would expect, especially when the Wrap Text feature is enabled...) you can use one of the two macros below.

The first macro (and its dependents) does the same as your post #1 code and can be used if the columns to the right of your worksheet form contain data or shapes that you do not want to shift down. Because Excel is database oriented (where the rows act as records) it actually makes sense that with a partial insertion like this only the data shifts downwards and the row heights do not. From this design might also be explained, that row heights cannot be pasted while column widths can be pasted (using PasteSpecial). Therefore when wanted we have to copy row heights ourselves. In the code below this is done through a separate procedure. After the partial insertion only the height of the rows at the bottom of the data section need to be adjusted each time. A separate function is used to determine the location of that data section.

VBA Code:
Sub mysticmario_v1()

    Dim FirstPage As Range, NewFirstPage As Range, LastPage As Range

    Application.ScreenUpdating = False

    Set FirstPage = Range("A2:P57")     ' <<< change range address to suit
    With FirstPage

        .Insert Shift:=xlDown           ' note that after insertion FirstPage's address has been changed!
        Set NewFirstPage = .Offset(-.Rows.Count)
        Set LastPage = GetLastPage(NewFirstPage)
        .Copy NewFirstPage
        CopyRowHeights NewFirstPage, LastPage

    End With
    Application.ScreenUpdating = True
End Sub


Function GetLastPage(ByVal argFirstPage As Range) As Range
    Dim PageCount As Long
    If Not argFirstPage Is Nothing Then
        With argFirstPage
            PageCount = VBA.Int(.Parent.UsedRange.Rows.Count / .Rows.Count) - 1
            If PageCount > 0 Then
                Set GetLastPage = .Offset(.Rows.Count * PageCount)
            End If
        End With
    End If
End Function


Sub CopyRowHeights(ByVal argSource As Range, ByVal argTarget As Range)
    Dim i As Long
    If Not argSource Is Nothing And Not argTarget Is Nothing Then
        With argSource
            For i = 1 To .Rows.Count
                argTarget.Rows(i).RowHeight = .Rows(i).RowHeight
            Next i
        End With
    End If
End Sub


The second macro however inserts entire rows (and thereby records are respected). That way row heights don't need te be adjusted at all, so a few lines of code will do. This macro can be used in case the columns to the right of your worksheet form don't contain data or shapes.

VBA Code:
Sub mysticmario_v2()
    With Range("2:57")         ' <<< change start and ending row to suit
        .Insert Shift:=xlDown
        .Copy .Offset(-.Rows.Count)
    End With
End Sub

Hope this helps.
 
Upvote 0
That is quite the solution aswell, well beyond my current knowledge, but i somewhat get it.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top