Insert a number of rows based on a cell value and insert a new string in said rows.

Lv1254

New Member
Joined
Jul 25, 2023
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi everyone,

I'm new to excel VBA/macro etc. I have experience with R and python, but can't use those for my job, sadly.

I am trying to create an 'interactive' Excel file, where a user is asked questions and depending on the answer more questions will be asked.

I have found the following code, which creates a value based on a numeric value in a column:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/19/2022  6:19:43 AM  EDT
If Target.Column = 11 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).Value = Rows(Target.Row).Value
End If
End Sub

I adapted it to specify a specific cell and not just a column:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/19/2022  6:19:43 AM  EDT
If Target.Column = 11 And Target.Row = 2 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).Value = Rows(Target.Row).Value
End If
End Sub

However, If I try to change "Rows(Target.Row).Value" to a string such as "What is the value of Room #X", it fills the entire column range. I have tried to adapt the code but my knowledge is very limited. What would I have to adapt to obtain the following result (images):
1690273490556.png
->
1690273546126.png

Thanks in advance
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the Board!

Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Exit if multiple cells updated at once
    If Target.CountLarge > 1 Then Exit Sub
    
'   Only run if cell B11 updated with a number greater than 0
    If Target.Address = "$B$11" And Target.Value >= 1 Then
        Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
        Cells(Target.Row, "A").Offset(1).Resize(Target.Value).Formula = "=""Volume of bottle #"" & ROW()-11"
    End If
    
End Sub
Note that your "COUNTLARGE" should be the first thing you check, because if it is greater than 1, than your "Target" range is not one cell, but many.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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