Error with Character Replacement VBA Function - excel

Consider the following VBA Function:
Sub RemoveLetters()
Application.ScreenUpdating = False
Dim str As String: str = "abcdefghijklmnopqrstuvwxyz0123456789 "
Dim ltr As String
For i = 1 To 37
ltr = Mid(str, i, 1)
Sheet9.Range("A2:A1800").Replace _
What:=ltr, Replacement:="", MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next
Application.ScreenUpdating = True
End Sub
This function gets rid of all letter and number characters in a given string. However, the error I encounter is that whenever a string begins with a non-letter, non-number string it fails to execute on that string and does not execute on any further strings. That is, running into such a string stops the execution. Is there an obvious reason why this is happening? How might I modify this code to fix it?

For matching patterns, the best option is regular expressions or more commonly known as regex. By defining a set pattern to follow, you can extract or replace almost anything you want.
To replace all non-number and non-letter characters as well as spaces, a small function like the following works:
Function NoNormalChar(StrTarget As String) As String
Dim oRegEx As Object
Set oRegEx = CreateObject("vbscript.regexp")
With oRegEx
.Pattern = "[a-zA-Z0-9\s]+"
.Global = True
NoNormalChar = .Replace(StrTarget, "")
End With
End Function
Calling it inside a sub is simple enough:
Sub RemoveLetters()
Dim Rng As Range, Cell As Range
Application.ScreenUpdating = False
Set Rng = Sheet9.Range("A2:A1800")
For Each Cell In Rng
Cell.Value = NoNormalChar(Cell.Value)
Next Cell
Application.ScreenUpdating = True
End Sub
Copy and paste both to a single module. Run on a back-up copy and let us know of the results.

Related

How to Break a Long Line of Code Into Multiple Lines in VBA

I have a long of code that I would like to break up in two lines. I know we usually use _ and go to the next line, but as I am breaking up a list in a nested function I get the error:
Compile error: Expected: list separator or )
The code works fine otherwise.
The line I want to split up is the second one: If Not Intersect(Target, Range [...])
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("F6:F1000000, G6:G1000000, K6:K1000000, L6:L1000000, P6:P1000000, Q6:Q1000000, U6:U1000000, V6:V1000000, Z6:Z1000000, AA6:AA1000000, AE6:AE1000000, AF6:AF1000000")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
End Sub
Thank you.
Instead of
Range("F6:F1000000, G6:G1000000, K6:K1000000, L6:L1000000, P6:P1000000, Q6:Q1000000, U6:U1000000, V6:V1000000, Z6:Z1000000, AA6:AA1000000, AE6:AE1000000, AF6:AF1000000")
you can also use
Range("F6:G1000000, K6:L1000000, P6:Q1000000, U6:V1000000, Z6:AA1000000, AE6:AF1000000")
This is just the same range
Another approach might be
Dim s1 As String
Dim s2 As String
s1 = "F6:F1000000, G6:G1000000, K6:K1000000, L6:L1000000, P6:P1000000, Q6:Q1000000, "
s2 = "U6:U1000000, V6:V1000000, Z6:Z1000000, AA6:AA1000000, AE6:AE1000000, AF6:AF1000000"
and then use
Range(s1 & s2)
And, of course, one could use the approach mentioned in the comments
Dim s As String
s = "F6:F1000000, G6:G1000000, K6:K1000000, L6:L1000000, P6:P1000000, Q6:Q1000000, " & _
"U6:U1000000, V6:V1000000, Z6:Z1000000, AA6:AA1000000, AE6:AE1000000, AF6:AF1000000"

In VBA, how to extract the string before a number from the text

From ActiveWorkbook.name, I would like to extract the strings that are before (left side of ) the numbers. Since I want to use the same code in multiple workbooks, the file names would be variable, but every file name has date info in the middle (yyyymmdd).
In case of excel file, I can use the below formula, but can I apply the same kind of method in VBA?
=LEFT(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},ASC(A1)&1234567890))-1)
Example: MyExcelWorkbook_Management_20200602_MyName.xlsm
In above case, I want to extract "MyExcelWorkbook_Management_".
The most basic thing you could do is to replicate something that worked for you in Excel through Evaluate:
Sub Test()
Dim str As String: str = "MyExcelWorkbook_Management_20200602_MyName.xlsm"
Debug.Print Evaluate(Replace("=LEFT(""X"",MIN(FIND({0,1,2,3,4,5,6,7,8,9},ASC(""X"")&1234567890))-1)", "X", str))
End Sub
Pretty? Not really, but it does the job and got it's limitations.
You could use Regular Expressions to extract any letters / underscores before the number as well
Dim str As String
str = "MyExcelWorkbook_Management_20200602_MyName.xlsm"
With CreateObject("vbscript.regexp")
.Pattern = "^\D*"
.Global = True
MsgBox .Execute(str)(0)
End With
Gives:
MyExcelWorkbook_Management_
So basically you want to use the Midfunction to look for the first numerical character in your input string, and then cut your input string to that position.
That means we need to loop through the string from left to right, look at one character at a time and see if it is a digit or not.
This code does exactly that:
Option Explicit
Sub extratLeftText()
Dim someString As String
Dim result As String
someString = "Hello World1234"
Dim i As Long
Dim c As String 'one character of your string
For i = 1 To Len(someString)
c = Mid(someString, i, 1)
If IsNumeric(c) = True Then 'should write "If IsNumeric(c) = True AND i>1 Then" to avoid an "out of bounds" error
result = Left(someString, i - 1)
Exit For
End If
Next i
MsgBox result
End Sub
Last thing you need to do is to load in some workbook name into your VBA function. Generally this is done with the .Name method of the workbookobject:
Sub workbookName()
Dim wb As Workbook
Set wb = ActiveWorkbook
MsgBox wb.Name
End Sub
Of course you would need to find some way to replace the Set wb = ActiveWorkbook line with code that suits your purpose.

How to remove the last character of a word in a text string and insert to another cell using VBA in Excel?

Everything is working except for that little comma in the 5th word. How to remove that? My code is as follows.
The text looks like this: The data as of 20.12.2019, and so on.
I only want 20.12.2019 without that comma. Any clue? Thanks.
Public Function FindWord(Source As String, Position As Integer)
Dim arr() As String
arr = VBA.Split(Source, " ")
xCount = UBound(arr)
If xCount < 1 Or (Position - 1) > xCount Or Position < 0 Then
FindWord = ""
Else
FindWord = arr(Position - 1)
End If
End Function
subroutine calls the function.
Sub InsertDate()
Sheets("Sheet1").Range("B3").Value = FindWord(Sheets("Sheet2").Range("A2"), 5)
End Sub
So just for fun, a short introduction to regular expressions (which, by no means, I am an expert in):
Sub Test()
Dim str As String: str = "The data as of 20.12.2019, and so on."
Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b(\d{2}.\d{2}.\d{4})"
regex.Global = True
Debug.Print regex.Execute(str)(0)
End Sub
This would be good practice if your string won't follow that same pattern all the time. However when it does, there are some other good alternatives mentioned in comments and answers.
One option is to Replace:
Sub InsertDate()
With Sheets("Sheet1").Range("B3")
.Value = FindWord(Sheets("Sheet2").Range("A2"), 5)
.Value = Replace(.Value, ",", "")
End With
End Sub
This is still text-that-looks-like-a-date, so you can call DateValue to convert it.
.Value = Replace(.Value, ",", "")
.Value = DateValue(.Value) '<~ add this line

Removing particular string from a cell

I have text in a range of cells like
Manufacturer#||#Coaster#|#|Width (side to side)#||#20" W####Height (bottom to top)#||#35" H#|#|Depth (front to back)#||#20.5" D####Seat Depth#||#14.25"**#|#|Material & Finish####**Composition#||#Wood Veneers & Solids#|#|Composition#||#Metal#|#|Style Elements####Style#||#Contemporary#|#|Style#||#Casual
From this cell i need to remove strings between #|#|"needtoremove"#### only without affecting other strings.
I have tried find and replace, finding #|#|*#### and replacing it with #|#|. However its not giving the exact result.
Can anyone help me?
The other solution will remove anything between the first #|#| and ####, event the #||# etc.
In case you only need to remove the text between #|#| and #### only if there is no other ##|| inbetween, I think the simplest way is to use a regex.
You will need to activate the Microsoft VBScript Regular Expressions 5.5 library in Tools->References from the VBA editor.
Change range("D166") to wherever your cell is. The expression as it is right now ("#\|#\|[A-Za-z0-9& ]*####")matches any text that starts with #|#|, ends with #### and has any number of alphanumerical character, & or space. You can add other caracters between the brakets if needed.
Sub remove()
Dim reg As New RegExp
Dim pattern As String
Dim replace As String
Dim strInput As String
strInput = Range("D166").Value
replace = ""
pattern = "#\|#\|[A-Za-z0-9& ]*####"
With reg
.Global = True
.MultiLine = True
.IgnoreCase = False
.pattern = pattern
End With
If reg.test(strInput) Then Range("D166").Value = reg.replace(strInput, replace)
End Sub
Something like this.
If that value is in cell A1
Dim str As String
Dim i As Integer
Dim i2 As Integer
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
str = ws.Range("A1").Value
i = InStr(str, "#|#|")
i2 = InStr(str, "####")
str = Left(str, i) & Right(str, Len(str) - i2)
ws.Range("A1").Value = str

Excel VBA - increase accuracy of Match function

I am using a Match function in my program, its main purpose is to compare an input typed by the user and then loop into a database and do something every time there is a match.
Currently, I am working with this :
Function Match(searchStr As Variant, matchStr As Variant) As Boolean
Match = False
If (IsNull(searchStr) Or IsNull(matchStr)) Then Exit Function
If (matchStr = "") Or (searchStr = "") Then Exit Function
Dim f As Variant
f = InStr(1, CStr(searchStr), CStr(matchStr), vbTextCompare)
If IsNull(f) Then Exit Function
Match = f > 0
End Function
And then when it is used :
If Match(sCurrent.Range("A" & i).Value, cell.Value) Then
Here is my problem :
This is way too inaccurate. If I have in my database "Foxtrot Hotel", this function will find a match whenever the user types "F" "Fo" "Fox" "ox" "xtro" "t hot" and so on, so whenever there is a string of character included in the complete sentence.
What I want is to make my Match function identify only complete words. So in this case, to reveal a match only for three specific cases : "Foxtrot" "Hotel" and "Foxtrot Hotel".
I have read about an attribute called "lookat" which can do this kind of stuff with the Find function (lookat:=xlwhole), do you know if something similar can be inserted in my Match function?
Thanks !
You could use a regex like so. This one runs a case-insensitive match on whole words only (using word boundaries around the word being searched).
False (fo is followed by a letter, not a word boundary)
True
True
False (FOXTROT h not followed by a word boundary)
Find wont work with xlWhole for you - that will look to match the entire cell contents, not a single word within the contents.
Function WordMatch(searchStr As Variant, matchStr As Variant) As Boolean
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\b" & matchStr & "\b"
.ignorecase = True
WordMatch = .test(searchStr)
End With
Set objRegex = Nothing
End Function
Sub B()
Debug.Print WordMatch("foxtrot hotel", "fo")
Debug.Print WordMatch("foxtrot hotel", "hotel")
Debug.Print WordMatch("foxtrot hotel", "FOXTROT")
Debug.Print WordMatch("foxtrot hotel", "FOXTROT h")
End Sub

Resources