(VBA) How to account for variables in text?

moddddmyexcel

New Member
Joined
Mar 6, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
A wonderful mod helped me out with the code below to remove duplicates but make an exception for fields starting with VNA. I now modified the code to include other locations that are variable. I was wondering if there was anyway I could shorten this codes to account for *TRN* and *EVN* where the asterisks are variable text.

VBA Code:
Sub RemoveDups()
 With Sheets("Replen Data")
    With .Range("A4", .Range("L" & Rows.Count).End(xlUp))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,3)=""VNA"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""1TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""2TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""3TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""4TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""5TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""6TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""1EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""2EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""3EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""4EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""5EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .Columns(12).Value = .Worksheet.Evaluate(Replace("if(left(#,4)=""6EVN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))
      .RemoveDuplicates Columns:=Array(1, 4, 12), Header:=xlNo
      .Columns(12).Replace What:="||*", Replacement:="", LookAt:=xlPart
      .RemoveDuplicates Columns:=Array(1, 4, 10), Header:=xlNo
      .RemoveDuplicates Columns:=Array(1, 5), Header:=xlNo
    End With
    End With
Exit Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The MID() function should work for you. For example:

Excel Formula:
.Worksheet.Evaluate(Replace("if(mid(#,2,3)=""TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))

Where 2 is the starting character in the string to check and 3 is the number of characters to compare. Just change TRN for EVN for the second Evaluate.
 
Upvote 1
Solution
The MID() function should work for you. For example:

Excel Formula:
.Worksheet.Evaluate(Replace("if(mid(#,2,3)=""TRN"",#&""||""&ROW(#),#)", "#", .Columns(12).Address))

Where 2 is the starting character in the string to check and 3 is the number of characters to compare. Just change TRN for EVN for the second Evaluate.
I need to familiarize myself with the MID() function. Such a simple fix, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,814
Members
449,339
Latest member
Cap N

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