Append a String to a bolded String in VBA - excel

I have a cell that contains the text "Collar Lot #" and I would like to append a string onto the end of it that is not bold. An example would be "Collar Lot # 23456\34567\45678". My code attempt below makes the entire string bold, resulting in "Collar Lot # 23456\34567\45678"
With Workbooks(ThisWorkbook.name).Worksheets(1)
...
.Cells(7, 5).value = "Collar Lot # " & "23456\34567\45678"
End With
How would I make sure the "23456\34567\45678" was not bold?

You would need to specify the formatting based on the position of the characters by using something like:
.Cells(7, 5).Characters(Start:=1, Length:=13).Font.FontStyle = "Bold"

.Cells(7, 5).Characters(Start:=14).Font.FontStyle = "Regular"
The Record Macro button on the Developer Tab of the Ribbon is very useful for finding out something like this.

Related

Odd behaviour by automatically changing color of certain characters in a cell

I'm facing a odd behavior by applying different colours within one cell via VBA.
In my case there are hundrets of cells within one column, showing different work-packages.
My vba code exaclty does what it should do, by coloring identified strings (respecively work packages) via looping through the cells and identifiying each work package via RegExp.
Here there is one extract that is doing the coloring job:
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = suchmuster
If .test(objWks_myTable.Cells(row_myTable, 20).Value) Then
Set RegMC = .Execute(objWks_myTable.Cells(row_myTable, 20).Value)
For Each RegM In RegMC
objWks_myTable.Cells(row_myTable, 20).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = vbOrange
Next
End If
End With
The issue appears as soon as I double click the cell after my makro run.
Then without any recognizable pattern, some characters are shown in a different color (mostly not only one character but a connected bunch of). In the picutre, the first cell shows the colours after my vba run, the second cell shows how it will immediately look like, if i double click it.
If I leave the edit mode via Escape, the original vba set colors will stay, If I leave the edit mode via Return, the undefined changes happen.
There are no formats nor format conditions set within the cells.
I really need somebodys help here. Would be great to find a solution!
Many Thanks !
This picture should show the issue:
Picture of the issue
I've found the issue.
First I tried also Instr instead of using a RegExp but the issue didn't disappear.
So I was investigating in my code that writes the strings into the cells.
And within that code I did the following:
dummy = dummy & " # " & z_trim(ctrl.Caption) & vbCrLf
ActiveCell.Value = dummy
The issue is because of vbCrLf
If I write the strings into the cells the following way, the changes within my coloring run are fixed, there is no change by entering the cell in edit mode:
dummy = dummy & " # " & z_trim(ctrl.Caption) & Chr(10)
ActiveCell.Value = dummy
Picture of fixed issue
It works, so I'm fine. But still interessted, why vbCrLf is causing such confusing thing?

How to Visual Basic recognize small lattin leter "č"

I'm working with Visual basic in Excel and I have a cell with the value "Gotovinski račun".
Another cell should be an English version of that value.
My code is:
If Range("A2").Value = "Gotovinski račun" Then Range("D2").Value = "Cash account"
Problem is that VBA can't recognize the small Lattin letter "č".
Any ideas?
Problem is that the VBA Editor only supports ANSI characters. You could try setting your "language for programs that do not support unicode" setting to Croation (or whichever language) (in administrative language settings).
Note that with that setting, the test string is properly displayed in the VBE:
If that is not an option, then you need to insert any characters with a code > 255 using the Chrw$ function. See the comment by #AlexK for an example.
A simple workaround consists in using a Like comparison with one (ore more) ? wildcards replacing the c plus caron character č:
With Sheet1.Range("A2")
If .Value Like "Gotovinski ra?un" Then
.Offset(0, 3).Value = "Cash account"
Else
.Offset(0, 3).Value = "'" & .Value & "' not found"
End If
End With

Extracting Data with .selectitem VBA-SAP

I am trying to extract the text from a topdown analysis in SAP using VBA. I basically need to move down the column, evaluate if the extracted text contains a certain character string, then extract the cell adjacent to it.
First: Does anyone know how to change this line of code (the script for that specific cell) so that I can extract its text to a variable rather than just select it?
Second: Does anyone know how to make this line variable using i as the variable?
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").selectItem " 5", "C 35"
In similar cases, I proceeded as follows:
myRow = right(space(10) & cstr(i) , 11)
myText = session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").getItemText(myRow ,"C 35")
Regards,
ScriptMan

VBA dots from database get loaded into textbox as comma

I know the Headline sounds odd so I will start off with a screenshot:
As you can see, the problem is that the point suddenly changes to a comma when I look up an ID in the UserForm.
Before recalling Infos, I am saving all Information rather straightforward:
with ws
Range("BH" & lastRow).value = Me.payinfoOnTime
Range("BI" & lastRow).value = Me.payinfo30
Range("BJ" & lastRow).value = Me.payinfo60
Range("BK" & lastRow).value = Me.payinfo90
Range("BL" & lastRow).value = Me.payinfo90more
End with
Recalling the respective info for a searched ID is done by:
Set FoundRange = ws.Range("D4:D500").Find(What:=Me.SearchSuppNo, LookIn:=xlValues)
With ws
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
Me.SEpayinfo30 = FoundRange.Offset(0, 57)
Me.SEpayinfo60 = FoundRange.Offset(0, 58)
Me.SEpayinfo90 = FoundRange.Offset(0, 59)
Me.SEpayinfo90more = FoundRange.Offset(0, 60)
end with
The Problem is that later calculations for scores are depending on those textboxes and I constantly get an error, unless I always manually change the commas back to points.
Any ideas how I can fix this?
The line:
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
is in fact:
Me.SEpayinfoontime.Value = FoundRange.Offset(0, 56).Value
When you populate an MSForms.TextBox using the .Value property (typed As Variant), like you implicitly do, and providing a number on the right side, the compiler passes the value to the TextBox as a number, and then the value is automatically converted to string inside the TextBox.
Exactly how that conversion happens does not appear to be documented, and from experiment, it would appear there is a problem with it.
When you freshly start Excel, it would appear assigning .Value will convert the number using the en-us locale, even if your system locale is different. But as soon as you go to the Control Panel and change your current locale to something else, .Value begins to respect the system locale, and changes its result depending on what is currently selected.
It should not be happening and I would see it as an Excel bug.
But if you instead assign the .Text property, the number is converted to string using the current system decimal dot, and that conversion happens outside of the TextBox, because the compiler knows .Text is a string, so it converts the right-hand side number to string beforehand.
So in your situation I would:
Make sure I always use the .Text property explicitly:
Me.SEpayinfoontime.Text = ...
Make sure I explicitly use the correct kind of functions to convert between text and numbers:
Me.SEpayinfoontime.Text = CStr(FoundRange.Offset(0, 56).Value)
MsgBox CInt(Me.SEpayinfoontime.Text) / 10
although this step is optional and represents my personal preference. Given that it's a string on the left side of the assignment, VB will use CStr automatically.
Go to Excel's settings to make sure the "Use system separators" tick is set.
Check what locale is selected in the Control Panel - Language and Regional settings.
If it is not En-Us, I would select En-Us to make sure the decimal separator is a dot there.
Restart Excel.

VBA: Making specific text bold or italic

I am very new to the macro. I am looking forward for the code for printing some part of string in italic and rest in bold.
e.g.
Hi There, congratulation for new assignment.
I am able to print the entire string in either bold or in Italic but not able to find the code which can fulfil my requirement.
Also splitting the text in to the dummy variable and joining those post applying the formatting seems to be a lengthy option.
Play around with this, it will do what you used for your example.
Sub SetBoldAndItalicInCell()
ActiveCell.FormulaR1C1 = "Hi There, congratulation for new assignment."
With ActiveCell.Characters(Start:=1, Length:=3).Font.FontStyle = "Regular"
ActiveCell.Characters(Start:=4, Length:=5).Font.FontStyle = "Bold Italic"
ActiveCell.Characters(Start:=9, Length:=24).Font.FontStyle = "Regular"
ActiveCell.Characters(Start:=33, Length:=11).Font.FontStyle = "Bold"
ActiveCell.Characters(Start:=44, Length:=1).Font.FontStyle = "Regular"
End Sub

Resources