thomassamoth
New Member
- Joined
- Mar 2, 2023
- Messages
- 13
- Office Version
- 365
- Platform
- Windows
Hello, this is my first time posting here.
My goal is as follows: I want to insert a specific amount of rows after I searched for a specific cell value and merge some of the columns of those inserted cells. I also want to add specific values to another column in the inserted cell. I know this sounds very vague, but at the end I added some pictures which will hopefully make things more clear. And before you comment that I shouldn't merge the cells, it's what my boss want so rip .
The process the of system (Inputboxes) would go as follows:
1. Which value are you looking for?
2. Select the range in where you want to look.
3. How many rows would you like to add?
4. Which value would you like to have in row [array#]?
5. Which columns would you like to merge with the inserted rows?
In excel, the process will look like this before:
and like this after:
As you can see, there are now 3 more rows inserted after "Car". The column "type", "Tag number" and "Cost" are now merged, while "tire number" has 3 new added values, namely "1", "2" and "3".
I already wrote a bit of code, which helps me to insert a dynamic amount of rows after I tell Excel for which cell value it needs to look. The problem is that I don't understand how I can set the range for the merge of the cells and how I can add specific values in specific cells for each row I add. Basically, I don't understand the "Range" function in Excel, because my brain is 90% Jelly.
My goal is as follows: I want to insert a specific amount of rows after I searched for a specific cell value and merge some of the columns of those inserted cells. I also want to add specific values to another column in the inserted cell. I know this sounds very vague, but at the end I added some pictures which will hopefully make things more clear. And before you comment that I shouldn't merge the cells, it's what my boss want so rip .
The process the of system (Inputboxes) would go as follows:
1. Which value are you looking for?
2. Select the range in where you want to look.
3. How many rows would you like to add?
4. Which value would you like to have in row [array#]?
5. Which columns would you like to merge with the inserted rows?
In excel, the process will look like this before:
and like this after:
As you can see, there are now 3 more rows inserted after "Car". The column "type", "Tag number" and "Cost" are now merged, while "tire number" has 3 new added values, namely "1", "2" and "3".
I already wrote a bit of code, which helps me to insert a dynamic amount of rows after I tell Excel for which cell value it needs to look. The problem is that I don't understand how I can set the range for the merge of the cells and how I can add specific values in specific cells for each row I add. Basically, I don't understand the "Range" function in Excel, because my brain is 90% Jelly.
VBA Code:
Private Sub ToggleButton1_Click()Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
inputSearch = InputBox("After which value would you like to add a row?")
inputAmountOfRows = InputBox("How many Rows would you like to add?")
inputColumnRangeCopy1 = InputBox("From which column would you like to start copying?")
inputColumnRangeCopy2 = InputBox("From which column would you like to end copying?")
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Select Range", WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.Columns(1)
xLastRow = WorkRng.Rows.Count
Application.ScreenUpdating = False
For xRowIndex = xLastRow To 1 Step -1
Set Rng = WorkRng.Range("A" & xRowIndex)
If Rng.Value = inputSearch Then
For i = inputAmountOfRows To 1 Step -1
Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown
With Range(????)
.Merge
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
End With
Next i
End If
Next xRowIndex
Application.ScreenUpdating = True
End Sub