Yes, these are not complex transformations at all, the size of the macro has increased slightly, mainly due to the quote from Pitonyak’s book, which I mentioned in the very first comment. The whole module might look like this:
REM ***** BASIC *****
Option Explicit
Dim moneyFormat As Integer, percentFormat As Integer
Sub replaceTextWithNumbers()
Const SHEET_NAME = "Extended TIKR"
Dim oSheets As Variant, oSheet As Variant
oSheets = ThisComponent.getSheets()
If Not oSheets.hasByName(SHEET_NAME) Then Exit Sub
oSheet = oSheets.getByName(SHEET_NAME)
moneyFormat = FindCreateNumberFormatStyle("0.00")
percentFormat = FindCreateNumberFormatStyle("0.00%")
replaceInRange(oSheet.getCellRangeByName("B3:N71"))
replaceInRange(oSheet.getCellRangeByName("Q3:AC71"))
replaceInRange(oSheet.getCellRangeByName("AF3:AR71"))
replaceInRange(oSheet.getCellRangeByName("AU3:CQ71"))
trimWhitespace(oSheet.getCellRangeByName("A1:A71"))
trimWhitespace(oSheet.getCellRangeByName("P1:P71"))
trimWhitespace(oSheet.getCellRangeByName("AE1:AE71"))
trimWhitespace(oSheet.getCellRangeByName("AT1:AT71"))
End Sub
Sub replaceInRange(aRange As Variant)
Dim oRDescriptor As Variant, percentCells As Variant
Dim oldBkColor As Long, i As Long
oldBkColor = aRange.getCellByPosition(0, 0).CellBackColor
aRange.clearContents(com.sun.star.sheet.CellFlags.HARDATTR)
oRDescriptor = aRange.createReplaceDescriptor()
oRDescriptor.SearchRegularExpression=True
oRDescriptor.SearchWords = False
oRDescriptor.setSearchString("\((.*)\)")
oRDescriptor.setReplaceString("-$1")
aRange.replaceAll(oRDescriptor)
oRDescriptor.setSearchString(".+")
oRDescriptor.setReplaceString("&")
aRange.replaceAll(oRDescriptor)
oRDescriptor.setSearchString("%")
percentCells = aRange.findAll(oRDescriptor)
aRange.NumberFormat = moneyFormat
If Not IsNull(percentCells) Then
For i = 0 To percentCells.getCount()-1
percentCells.getByIndex(i).NumberFormat = percentFormat
Next i
EndIf
aRange.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
aRange.CellBackColor = oldBkColor
End Sub
Sub trimWhitespace(aRange As Variant)
Dim oRDescriptor As Variant
oRDescriptor = aRange.createReplaceDescriptor()
oRDescriptor.SearchRegularExpression=True
oRDescriptor.SearchWords = False
oRDescriptor.setSearchString("^\s*")
oRDescriptor.setReplaceString("")
aRange.replaceAll(oRDescriptor)
oRDescriptor.setSearchString("\s*$")
aRange.replaceAll(oRDescriptor)
End Sub
Rem @Author: Andrew Pitonyak from Listing 5.38: Create a number format style.
Function FindCreateNumberFormatStyle (_
sFormat As String, Optional doc, Optional locale)
Dim oDoc As Object
Dim aLocale As New com.sun.star.lang.Locale
Dim oFormats As Object
Dim formatNum As Integer
oDoc = IIf(IsMissing(doc), ThisComponent, doc)
oFormats = oDoc.getNumberFormats()
'If you choose to query on types, you need to use the type
'com.sun.star.util.NumberFormat.DATE
'I could set the locale from values stored at
'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
'I use a NULL locale and let it use what ever it likes.
'First, see if the number format exists
If ( Not IsMissing(locale)) Then
aLocale = locale
End If
formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
' MsgBox "Current Format number is" & formatNum
'If the number format does not exist then add it
If (formatNum = -1) Then
formatNum = oFormats.addNew(sFormat, aLocale)
If (formatNum = -1) Then formatNum = 0
' MsgBox "new Format number is " & formatNum
End If
FindCreateNumberFormatStyle = formatNum
End Function
I thought it would be better if we removed spaces and line breaks (it turns out there were cells with such values!) not only at the beginning of the line, but also at the end. I hope your VLOOKUP() does not contain search strings with spaces at the end?
But in any case, the behavior of your Calc is surprising - the fact that the macro is still run in the third case is confusing. Honestly, this is the first time I’ve encountered such an error!
Update. O! So it stopped running! Well, that’s more natural behavior for the program… Try restarting it in safe mode.