[Solved] Insert here ... or not

BrianDP1977

Board Regular
Joined
Nov 5, 2005
Messages
146
I don't understand why this won't work. This code is a slightly adjusted code offered by HalfAce in a different post:

Code:
Sub Insert_Row(nRange As Range) 
Dim InsrtRw As Long

InsrtRw = Range(nRange).Cells(Rows.Count, 1).End(xlUp).Row 
Range(nRange).Rows(InsrtRw).EntireRow.Copy 
Range(nRange).Rows(InsrtRw).EntireRow.Insert 
Application.CutCopyMode = False 
End Sub

What I'm trying to do is create an Insert_Row code that will be passed a named range of cells (through the nRange variable). Within this selected range of cells, I want a whole row added to the last line within the specified range of cells. I need this added row to become part of the named range (expressing the range of cells as a dynamic name should accomlish this) and also maintain the equations contained in the cells (thus the reason for the copy call). Thanks for the help.
 
That seems to work. Thanks for not giving up. I would still kind of like anything not containing an equation to clear from the row (to empty cells for entry) but this is not a major issue. Now that I know what that one section was doing in the other equation, I'll see if I can't tag that on somehow. Thanks again very much.
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I hate to throw one more curve ball at you but is there any way to keep this insert from screwing around with current border settings? Right now, the last row in the section has a thin top border and the bottom border is a thick line. When it does the insert, the inserted row has a thick border on both the top and the bottom. I'm sure I can find a workaround but just curious if there is a simple solution. Thanks.
 
Upvote 0
Batter up!

How about changing the code to:
Code:
Sub Insert_Row(nRange As Range)

nRange.Cells(nRange.Rows.Count, 1).EntireRow.Copy
nRange.Cells(nRange.Rows.Count, 1).EntireRow.Insert
Application.CutCopyMode = False
With nRange.Offset(nRange.Rows.Count - 1).Resize(1).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

End Sub
 
Upvote 0
I'm glad your keeping your sense of humor with me. Anyway, it worked for the first cell in the row but not all the rest. I'd say "we are so close" but you're doing all the work. Thank you again for the help.
 
Upvote 0
"Never give up...Never surrender" Trivia - what movie is that from (gotta make this fun)?

Now, on to adjusted code
Code:
Sub Insert_Row(nRange As Range)

nRange.Cells(nRange.Rows.Count, 1).EntireRow.Copy
nRange.Cells(nRange.Rows.Count, 1).EntireRow.Insert
Application.CutCopyMode = False
With nRange.Offset(nRange.Rows.Count - 1).EntireRow. _
    SpecialCells(xlCellTypeConstants).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

End Sub

How's that?
 
Upvote 0
BrianDP1977 said:
Disregard last. It had to do with how I was defining the range. It works great now. Thank you very much.

Too late, I already posted a suggested solution. Can you try it and let me know if it works?
 
Upvote 0
Actually, the other code works better. For some reason, this one formats some borders bu not others. I'm assuming it's because not all cells in the database have inputs. Sometimes they get left out. The otehr one works well though. Thank you very much.
 
Upvote 0
If you are still interested in tinking with the one set of code you gave me, I could still use a way to clear cells of their values if they do not contain equations (but leave the equation cells with their equations) thus allowing empty cells for entry. It's no biggie and I am very happy with what I have now. It works great Thank you again.

Here's the code I'm using:

Code:
Private Sub CommandButton1_Click()
  Dim Range_Names As Range
  
  Set Range_Names = Range("Ext_DP6_P1")
  Call Insert_Row(Range_Names)
End Sub

Code:
Sub Insert_Row(nRange As Range)

nRange.Cells(nRange.Rows.Count, 1).EntireRow.Copy
nRange.Cells(nRange.Rows.Count, 1).EntireRow.Insert
Application.CutCopyMode = False
With nRange.Offset(nRange.Rows.Count - 1).Resize(1).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

End Sub
 
Upvote 0
How about this then?
Code:
Sub Insert_Row(nRange As Range)

nRange.Cells(nRange.Rows.Count, 1).EntireRow.Copy
nRange.Cells(nRange.Rows.Count, 1).EntireRow.Insert
Application.CutCopyMode = False
With nRange.Offset(nRange.Rows.Count - 1).EntireRow.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
nRange.Offset(nRange.Rows.Count - 1).Resize(1). _
    SpecialCells(xlCellTypeConstants).ClearContents

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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