Trying to add multiple values based off of a specific text

Baijano23

New Member
Joined
Aug 15, 2024
Messages
1
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hello all!

I’m fairly new to using VBA but trying to get as much out of it as possible! I’m working on a code that will search for a specific phrase (ADD-CAS) in a cell and add a row above it. Once that is done, I then want to an add “Loaded” to a cell 6 cells above and 23 cells to the right of ADD-CAS. I have this working ok with the below code. Where I’m struggling is I also need to add “Dispensed”, “Expected”, “Reported” and “Variance” in that order, below “Loaded”. I’ve tried copying the same code but adding -5 for “Dispensed” however that just seems to overwrite the “Loaded” and then only shows “Dispensed”

What do I need to do to show all 5 of the phrases mentioned above at the same time?

The code I have so far:

Dim m As Long

Dim Lastrow2 As Long

Lastrow2 = Cells(Rows.Count, "D").End(xlUp).Row

For m = Lastrow2 To 1 Step -1

If Cells(m, "D").Value = "ADD-CAS" Then Cells(m, "D").EntireRow.Insert xlShiftDown

Next

Application.ScreenUpdating = True

With Range("D8", Range("D" & Rows.Count).End(xlUp))

.Offset(-6, 21).Value = Evaluate("if(isnumber(search(""ADD-CAS""," & .Address & ")),""Loaded"","""")")

End With


I’ve posted via my mobile so apologies if this has formatted terribly!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the MrExcel forum. Please accept my warmest greetings

I'm not sure if you need to put the first value 6 cells before or 5 cells before, since you have to consider that you inserted a row.
Anyway you can change the -5 to -6.

VBA Code:
Sub AddValues()
  Dim i As Long
  For i = Range("D" & Rows.Count).End(3).Row To 1 Step -1
    With Range("D" & i)
      If .Value = "ADD-CAS" Then
        .EntireRow.Insert xlShiftDown
        .Offset(-5, 23).Resize(5).Value = _
          Application.Transpose(Array("Loaded", "Dispensed", "Expected", "Reported", "Variance"))
      End If
    End With
  Next
End Sub

If it is not what you need, you could put 2 images, the first image with your original data and the second image with the expected result of the macro.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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