VBA: Search Used Range For Values, Return Triggered Result OUTSIDE of Used Range.

SteveOranjinSteve

Board Regular
Joined
Nov 18, 2019
Messages
78
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

Hope you're well. I have been working on some code that I would like someone to review. This code currently prints a triggered result, one cell over, to the right, when it encounters "TOTAL" within a string. I would like it to print out the same value, except outside of the range it is searching, in the next subsequent column over.
VBA Code:
Sub AddDashes()

Dim SrchRng As Range, cel As Range

Set SrchRng = ActiveSheet.UsedRange

For Each cel In SrchRng
    'InStr returns a 'variant'(long) specifying the position of the first occurrence of one string within another.
    'it contains four arguments.
        ' Instr([Numeric Expression, that sets the starting position for each search.],(String1) Required: String expression being search for.,(String2)Required: String expression being sought.)
    If InStr(1, cel.Value, "TOTAL") > 0 Then
        cel.Offset(0, 1).Value = "TOTAL"
    End If
Next cel
End Sub

I've tried substituting "cel" for "SrchRng", but that just then replaces ALL the values with "Total", and that certainly isn't what I want.



Start
fadfasd Total

RESULT

fadfasd TOTALTOTAL
fdfasdfasd
dfadfa TOTALTOTAL
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
VBA Code:
Cells(cel.Row, SrchRng.Columns.Count + 1).Value = "TOTAL"
 
Upvote 0
It takes the row of the range variable "cel" & the number of columns in the "SrchRng" +1 & uses them as the row/column index in Cells
 
Upvote 0

Forum statistics

Threads
1,215,616
Messages
6,125,860
Members
449,266
Latest member
davinroach

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