if cell contains substring remove the substring

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,008
Office Version
  1. 365
Platform
  1. Windows
folks,

I need to remove the title "Dr" from a list of names prior to publishing. Not all names have this title. I can find the substring within my specific range with

Code:
pos = InStr(nm1, "Dr ")
where nm1 contains a name. nm1 refers to the cell C9 on my spreadsheet.

if nm1 does not contain the substring "Dr ", pos will be zero. if it does contain the substring, pos will be greater than zero, so remove

Code:
If pos > 0 Then .... remove

I will then be saving the file using the string (now without Dr in it) as the name. Can i reuse the nm1 variable or do i need to create another?

so far i have

Code:
Sub SavePdf()

Dim ws As Worksheet
Dim nm1 As String
Dim nm2 As String
Dim pos As Integer
 

For Each ws In ActiveWorkbook.Worksheets
    ws.Select


nm1 = Range("C9")
nm2 = Trim(Range("A2"))

'pos = InStr(nm1, "Dr ")

   ' If pos > 0 Then nm3 = Replace("Dr ", 7, "")
    
    

        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:="C:\Temp\Rerun\" & nm1 & " CHQ " & nm2 & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False

Next ws
 
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Remove the second parameter in the Replace;

Code:
Sub SavePdf()

Dim ws As Worksheet
Dim nm1 As String
Dim nm2 As String
Dim pos As Integer
 


For Each ws In ActiveWorkbook.Worksheets
    ws.Select




nm1 = Range("C9")
nm2 = Trim(Range("A2"))


pos = InStr(nm1, "Dr ")


    If pos > 0 Then nm1 = Range("C9").Replace("Dr ", "")


'        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
'        Filename:="C:\Temp\Rerun\" & nm1 & " CHQ " & nm2 & ".pdf", _
'        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
'        IgnorePrintAreas:=False, OpenAfterPublish:=False


Next ws
 
End Sub
 
  • Like
Reactions: ajm
Upvote 0
cheers Mary. I was labouring under the misapprehension that i had to specify the start position of the substring that i wanted removed.
 
Upvote 0

Forum statistics

Threads
1,215,433
Messages
6,124,863
Members
449,195
Latest member
MoonDancer

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