Inserting whole row based on Two User Inputs - VBA prompt macro

BuzzOffSweetheart

New Member
Joined
Feb 27, 2009
Messages
18
I am trying to make a macro that will:

  1. Ask the user to select a Tag number (one cell in column 5 of the ws)
  2. Then ask the user to select the amount of detail tags to add (to copy the entire row based on their input)
  3. Then take that original Tag in column 5 and Select the entire row and copy it whatever amount of times was inputted from the user on number 2 above.

Then as far as the advanced part:

I need to add to the code some way to take the newly pasted rows from no. 3 above and change the value of Column 4 to "new".

Here is my first attempt at the code:
Code:
[LEFT][COLOR=#333333]
[/COLOR][/LEFT]
<code style="margin: 0px; padding: 0px; font-style: inherit; ">Sub MasterTagSelect()
Dim rRange As Range
Dim lngRows As Long
lngNextRow As Long    
On Error Resume Next        
Application.DisplayAlerts = False            

Set rRange = Application.InputBox(Prompt:= _                
"Please select a Master Tag to Split.", _                    
Title:="SPECIFY MASTER TAG", Type:=8)    

On Error GoTo 0        

Application.DisplayAlerts = True        

If rRange Is Nothing Then            
Exit Sub        
Else
rRange.Select   
ActiveCell.EntireRow.Select

lngRows = CLng(InputBox("How many Detail Tags do you wish to insert? (Must be at least 2)"))    
lngNextRow = rRange                

Selection.Copy Rows(lngNextRow & ":" & lngNextRow + lngRows - 1)    
On Error GoTo Finish    
Finish:    
If Err.Number <> 0 Then MsgBox Prompt:="Please ensure you only enter numeric values!"        
End If</code>[LEFT][COLOR=#333333]
[/COLOR][/LEFT]


The problem I am running into with the above code is it is not pasting the rows directly under the selected row - now matter how I have tried it, it wants to paste elsewehere as rRange isn't working in my code - How do i specify the rRange so it copy and inserts directly below the select cell/row?

So then I tried another approach:

Code:
[LEFT][COLOR=#333333]Sub MasterTagSelect()[/COLOR][/LEFT]
<code style="margin: 0px; padding: 0px; font-style: inherit; ">Dim rRange As Range
Dim lngRows As Long    
On Error Resume Next        
Application.DisplayAlerts = False         
Application.ScreenUpdating = True            
Set rRange = Application.InputBox(Prompt:= _                
"Please select a Master Tag to Split.", _                    
Title:="SPECIFY MASTER TAG", Type:=8)    

On Error GoTo 0        

Application.DisplayAlerts = True        
If rRange Is Nothing Then            

Exit Sub        

Else
rRange.Select   
ActiveCell.EntireRow.Select    
userInput = Application.InputBox(Prompt:="How many Detail Tags do you wish to insert? (Must be at least 2)", Type:=1) t = Int(Val(userInput))  
If userInput = False Then Exit Sub    

With Selection    
.Copy .Resize(.Rows.Count * t, .Columns.Count).Offset(0)End With    
Finish:    If Err.Number <> 0 Then MsgBox Prompt:="Please ensure you only enter numeric values!"        
End If        
End Sub</code>
Now it is pasting over data already in the worksheet instead of inserting it.. and it seems to be only pasting 2 if the user specifies 3.. 4 if user specifies 5.. etc. etc..

And I am still having trouble figuring out how to select the row newly pasted to change a column's value..

Any help please?

My code is very convoluted I am sure and I am a beginner
frown.gif
.. My apologies.. any help would be swell!

 
Last edited:

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
Changing the pasted rows..

....
Then as far as the advanced part:

I need to add to the code some way to take the newly pasted rows from no. 3 above and change the value of Column 4 to "new".


I managed to fix my code and it works!

But I can't seem to figure out how to select the rows I inserted (based on user input) to change the value automatically.. since the active cell will change often, I can't do it based on a range.. it has to be whatever rows were inserted by the user prompt..

i.e.

1.1.1.1 I0127955 29.4 m
10.1.1.1 TAG-01 12 m

becomes this with macro:

1.1.1.1 I0127955 29.4 m
1.1.1.1 I0127955 29.4 m
1.1.1.1 I0127955 29.4 m
10.1.1.1 TAG-01 12 m

i need it to become this:
1.1.1.1 I0127955 29.4 m
new I0127955 29.4 m
new I0127955 29.4 m
10.1.1.1 TAG-01 12 m

Here is my new and improved code!

Code:
Sub MasterTagSelect()


Dim rRange As Range
Dim lngRows As Long




    On Error Resume Next


        Application.DisplayAlerts = False
         Application.ScreenUpdating = True


            Set rRange = Application.InputBox(prompt:= _
                "Please select a Master Tag to Split.", _
                    Title:="SPECIFY MASTER TAG", Type:=8)


    On Error GoTo 0


        Application.DisplayAlerts = True






        If rRange Is Nothing Then


            Exit Sub


        Else
rRange.Select
   ActiveCell.EntireRow.Select
    userInput = Application.InputBox("How many Detail Tags do you wish to insert? (Must be at least 2)", "Insert Rows", , , , , , 1)


 t = Int(Val(userInput))
 
 If userInput = False Then Exit Sub
    With Selection
    .Copy
    ActiveCell.Offset(1).Resize(userInput).EntireRow.Insert
 End With
    Application.CutCopyMode = False
    
Finish:
    If Err.Number <> 0 Then MsgBox prompt:="Please ensure you only enter numeric values!"
        End If


        


End Sub

Thanks for any help, everyone:)
 
Upvote 0

Forum statistics

Threads
1,206,815
Messages
6,075,014
Members
446,114
Latest member
FadDak

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