[vba] Reducing Replace & Length of Generated Strings

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
Hey all.
So i have this really REALLY messy title generator that 1st takes out unnecessary information from data that will later be used in titles. Generally speaking this is aimed at specific products that are known to be lengthy in characters.
VBA Code:
            ary2(i, 2) = Replace(ary(i, x), " BY KMC WHEELS", "")
            ary2(i, 2) = Replace(ary2(i, 2), " TL SERIES", "")
            ary2(i, 2) = Replace(ary2(i, 2), " DA Series", "")
            ary2(i, 2) = Replace(ary2(i, 2), " XC Series", "")
            ary2(i, 2) = Replace(ary2(i, 2), " 1PC", "")
            ary2(i, 2) = Replace(ary2(i, 2), " 2PC", "")

so is there a way to simplify this code to maybe look through a collection or dictionary of words and remove those from the string located in the array?
so say for instance ary2(i, 2) = "PA SERIES BY KMC WHEELS" instead of having all of these replace functions i could have one loop through a dictionary of key phrases to remove from the string, which the only part im caught up on is the actual removing the dictionary value from the string.

and then the second part which comes in later which is actually generating a title that is less than or equal to 80 characters.
how this works is i take the data and form a title from it. So i start with a string with the maximum amount of info i can include.
If that maximum amount of info is over 80 characters then i start whittling it down,
There has to be a better method though? open to suggestions

the dats is as follows: quantity - size - brand - style - "ET" - number - finish - pattern 1 - pattern 2 - "wheel rim"
sometimes pattern 2 is blank ""
example: One (1) 20x10 Contra 101C ET 20 Gloss Black 5x100 5X1.1 Wheel Rim

VBA Code:
'determine title length
ary2(i, 16) = "One (1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicFINISH.Item(ary2(i, 4)) & " " & ary2(i, 10) & " " & ary2(i, 11) & " Wheel Rim"

If Len(ary2(i, 16)) > 80 And Len(ary2(i, 16)) <= 84 Then
    ary2(i, 16) = "One (1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicFINISH.Item(ary2(i, 4)) & " " & ary2(i, 10) & " " & ary2(i, 11) & " Wheel"
End If

If Len(ary2(i, 16)) > 80 Then
    ary2(i, 16) = "One (1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicFINISH.Item(ary2(i, 4)) & " " & ary2(i, 10) & " Wheel"
End If
    
If Len(ary2(i, 16)) > 80 Then
    ary2(i, 16) = "One (1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicSHORT.Item(ary2(i, 4)) & " " & ary2(i, 10) & " Wheel"
End If
    
If Len(ary2(i, 16)) > 80 Then
    ary2(i, 16) = "One (1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicSHORT.Item(ary2(i, 4)) & " " & ary2(i, 10) & " Wheel"
End If


If Len(ary2(i, 16)) > 80 Then
    ary2(i, 16) = "(1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicSHORT.Item(ary2(i, 4)) & " " & ary2(i, 10) & " Wheel"
End If

If Len(ary2(i, 16)) > 80 Then
    ary2(i, 16) = "(1) " & ary2(i, 7) & " " & ary2(i, 2) & " " & ary2(i, 3) & " ET " & ary2(i, 8) & " " & dicSHORT.Item(ary2(i, 4)) & " " & ary2(i, 10)
End If

ary2(i, 16) = Replace(ary2(i, 16), "  ", " ")
ary2(i, 16) = Replace(ary2(i, 16), "  ", " ")
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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