Use specific cell instead of ActiveCell

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
I'm trying to create a macro that will allow the user to input newrows in a protected worksheet based on a starting point and a number ofrows.
The newly entered rows will be copies of the row above where the new rows willbe entered. That row contains formulas which must copy with the rows.
I can get this working using ActiveCell but I don’t want the user to have to selectthe row in the worksheet before running the macro.
So what I want is to allow the user toenter the starting position and the number of rows and based on this informationperform the task.

This is where I’m at but I’m having aproblem assigning the starting position to a variable.
Code:
Sub InsertMultipleRows()
Dim s As Variant
Dim n As Variant
Dim r As Range
'Get the starting position
s= Application.InputBox("Enter the starting position", , , , , , , 1 +2)
If s = False Then
Exit Sub
ElseIf s = "" Then
Exit Sub
End If
r= "A" & s
'Get the number of rows to input
n= Application.InputBox("Enter the number of rows to input", , , , , ,, 1 + 2)
If n = False Then
Exit Sub
ElseIf n = "" Then
Exit Sub
End If
'Unprotect the sheet
ActiveSheet.Unprotect Password:=""
'Copy the cell
r.EntireRow.Copy
'Fill down the required numbe of times
Range(r.Offset(1, 0), r.Offset(n, 0)).EntireRow.Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
I think the problem is with r = "A" & s
I’m getting the “Object variable or Withblock variable not set” error.

Any help would be greatly appreciated.
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Thanks for the quick reply Fluff. That's perfect.
I was close a couple of times. That's about the only variation I didn't try.

How do I mark this post as solved?
 
Upvote 0
Glad to help & thanks for the feedback.
We don't mark posts as "Solved" here.
 
Upvote 0
I've just noticed one other thing that is an issue with my setup.
My macro will copy the entire row because of course I've told it to, r.EntireRow.copy.
What if I only want columns M & N to be copied when inserting the new rows?
I thought something like: r.Range("M" & s & ": N" & s).Copy. But this also produces an error.
 
Upvote 0
Do M:N contain formulas or values & are they the only columns that do?
 
Upvote 0
That is correct. M and N contain simple formulas and are the only columns populated in the sheet.
 
Upvote 0
Ok,try
Code:
Range(r.Offset(1, 0), r.Offset(n, 0)).EntireRow.Insert Shift:=xlDown
Rows(s).Resize(n).SpecialCells(xlConstants).ClearContents
 
Upvote 0
Hi Fluff,
thank a million, this works perfectly.

I hope you'll bear with me while I try to understand this.


The Range pieceis clear.

This is myunderstanding of the line:
Rows(s).Resize(n).SpecialCells(xlConstants).ClearContents

It would become:
Rows(10).Resize(2).SpecialCells(xlConstants).ClearContents


I’m assuming it’s the xlConstants, the Type in the SpecialCells method, thatprevents the formulas in the newly inserted (i.e. copied) lines getting deleted?
So, if the line had been:
Rows(s).Resize(n).SpecialCells(Variant).ClearContentsthe behaviour would have been different?
 
Upvote 0
I’m assuming it’s the xlConstants, the Type in the SpecialCells method, thatprevents the formulas in the newly inserted (i.e. copied) lines getting deleted?
That's correct.
If it had been
Code:
SpecialCells(xlformulas)
It would have removed the formulas & left the rest.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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