Macro Find and Search

SH Harbour

New Member
Joined
Aug 22, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a worksheet which I need a Macro to search through column E2:E and find the info between #~ and 954#N and then put the relevant info in the corresponding cell F, an example of the info is as follows:
1692722273107.png

I would like it to appear as follows:
1692722370925.png

I can produce a formula which produces the result as follows:
= Mid(E2, Search("#~~", E2) + 1, Search("#954#N", E2, Search("#~", E2) + 1) - Search("#~~", E2) - 1)

However would rather have a Macro that could perform the operation, any assistance would be greatly appreciated?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Welcome to the Board!

Since you are using Excel 365, you can do this really easily with some of the new functions, i.e.
Excel Formula:
=TEXTBEFORE(TEXTAFTER(E2,"#~"),"954#N")

Do you still need/want VBA?
If so, perhaps look at using the SPLIT function in VBA: MS Excel: How to use the SPLIT Function (VBA)
 
Upvote 0
Welcome to the Board!

Since you are using Excel 365, you can do this really easily with some of the new functions, i.e.
Excel Formula:
=TEXTBEFORE(TEXTAFTER(E2,"#~"),"954#N")

Do you still need/want VBA?
If so, perhaps look at using the SPLIT function in VBA: MS Excel: How to use the SPLIT Function (VBA)
Hi

Thankyou

Yes the macro / VBA option would be best as the the report generated changes repeatedly and would rather not have to enter each time as i have a personal workbook open in the background so can run the relevant macro on a daily basis through hundreds of lines of data

Thanks
 
Upvote 0
So, are you wanting to replace the value in place, or place the result in another cell?
If in another cell, is it always the one to the right of the cell?
 
Upvote 0
Assuming that you want it to replace the current values, you can use this code.
Simply select the range you want to apply it to and run the code:
VBA Code:
Sub FixValues()
    
    Dim cell As Range
    Dim str As String
    Dim ar1() As String
    Dim ar2() As String
    
    Application.ScreenUpdating = False
    
'   Loop through all selected cells
    For Each cell In Selection
        If (InStr(1, cell, "#~") > 0) And (InStr(1, cell, "954#N") > 0) Then
            str = cell
            ar1 = Split(str, "#~")
            ar2 = Split(ar1(1), "954#N")
            cell = ar2(0)
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Good Evening

Yes to place in another cell, always to the right of the cell I am taking the data from.

Thanks
 
Upvote 0
Thanks I will give this a go, was having issues with the Tilda ~

Thanks
 
Upvote 0
Minor adjustment then:
VBA Code:
Sub FixValues()
    
    Dim cell As Range
    Dim str As String
    Dim ar1() As String
    Dim ar2() As String
    
    Application.ScreenUpdating = False
    
'   Loop through all selected cells
    For Each cell In Selection
        If (InStr(1, cell, "#~") > 0) And (InStr(1, cell, "954#N") > 0) Then
            str = cell
            ar1 = Split(str, "#~")
            ar2 = Split(ar1(1), "954#N")
            cell.Offset(0, 1) = ar2(0)
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
Note: You could simply have VBA apply the formula I posted in my first response in all those cells too.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,106
Messages
6,123,122
Members
449,096
Latest member
provoking

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