I'm trying to create a simple calculator that shows the result of the calculations in a simple userform based on what the user enter in specific cells.
I'm using the
x.caption = Range("X").value
to display the value calculated.
So far so good, everything works.
I want also the label to show the corresponding unit for each field, say Kg. I tried adding:
And x.caption = "Kg"
to give:
x.caption = Range("X").value And x.caption = "Kg"
, but it neither gives me an error or actually works. How does one actually go about doing this?
String concatenation in VB is done with the "&" operator.
E.g.
x.caption = Range("X1").value & " Kg"
Related
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?
I programmed a communication tool for the production floor. This tool will register what they have done, who has done it and on what time.
The following should check whether the textbox value equals the value in the worksheet or if the textbox (textbox is TextTools1) is empty. If this is true, then nothing should happen and the thus the value of the textbox is gonna stay the same.
If the textbox is not empty or is not equal to what has been previously saved in the worksheet (thus the value has changed), then it should be registered which operator has done it and what date and what time.
It works when the textbox is empty, but when the value of the textbox has stayed the same (thus TextTools.value=ActiveCell.Offset(0,23).value (Correct)) it still adds the operators name, date and time.
Something is going wrong when trying to compare the textbox value and the cell value, but cant put my finger on it.
Sheets("Checklist & overdracht").Visible = True
Sheets("Checklist & overdracht").Select
If TextTools1.Value = Range("AZ1").Value Or TextTools1.Value = Empty Then
Sheets("Checklist & overdracht").Select
rowloc1.Value = ActiveCell.Row
ActiveCell.Offset(0, 23).Value = TextTools1.Value
Else
Sheets("Checklist & overdracht").Select
rowloc1.Value = ActiveCell.Row
ActiveCell.Offset(0, 23).Value = TextTools1.Value & " " & "(" & cboOperator.Value & " " & Format(DateValue(CStr(Now)), "short date") & " " & Format(TimeValue(CStr(Now)), "hh:mm") & ")"
End If
Edit; changed it to the code above. I tested this in another userform (and used f8) and it works brilliantly, but when I put in the userform that will actually run this code, than it doesnt have the same result...
Edit2; So apparently something goes wrong with Range(AZ1).Value reference. Because when I enter a random value instead of the range and then run the code, it does work. Is there a different way of referencing?
Ok based on your comments
Stop using active cell when code from a user form is communicating to the compiler what sheet is what. You need to fully qualify what sheet you are using. Im not entirely sure where in the code the active sheet is being set but I am fairly certain the answer is never. Another reason selecting and referencing .ActiveWhatever is bad is a cardinal sin of vba is interacting with the actual application object instead of doing everything in memory. It bogs everything done and performance suffers considerably. When you start writing pretty dense stuff then you will inevitably suffer from issues where the compiler gets confused as to what thing it should be looking at and you'll have a grand ol' time of troubleshooting that nonsense.
Also, it might be a good idea to check for more than just "=Empty". What if there is a null or empty string? I tend to check for:
.value = "" OR ISNULL(.Value)=True OR .Value = vbNullstring
this isnt real feedback though - tons of people have different ways of doing the same thing.
Try:
Thisworkbook.Sheets("YOURSHEETNAME").Range("YOURRANGE").Offset(0,23).Value = Someothervalue.
Let me know if youre still facing issues.
All of the following is happening within an If Statement
I have a variable, Difference, that is the result of a subtraction of two monetary values.
I then have a another variable, Message, which looks as follows:
If AssessedValue < ProposedValue Then
Difference = Format(ProposedValue - AssessedValue, "Currency")
Message = "Average value is " & Difference & " more than the current
appraised value. Do NOT recommend negotiations."
Else
Difference = Format(AssessedValue - ProposedValue, "Currency")
Message = "Average value is " & Difference & " less than the current
appraised value. Recommend negotiations."
End If
MY issue is that in the message that displays, the difference does not display as currency but just an un-formatted number instead of currency (Example of the message below)
Average value is 21587 more than the current appraised value. Do NOT
recommend negotiations.
How can I get the 21587, in this example, to appear in the message as $21,587.00?
Thanks in advance for any help with this issue.
Kirk
There are a couple of ways to fix your issue:
Format Difference using a "Currency" format prior to including it in the string, e.g.
Message = "Average value is " & Format(Difference, "Currency") & " more than the current appraised value. Do NOT recommend negotiations."
Declare Difference to be a String rather than a numeric type. (I'm guessing you have declared it currently as a Currency type.)
Dim Difference As String
This will ensure that the result of Format(ProposedValue - AssessedValue, "Currency") is not converted back to a numeric value in order to be stored in your Difference variable.
I am very new to VB macro.In sheet 3 i have three columns named "Country " "Provinces" and "Risk"
For "Provinces", values present are NB,NS,NF,PE. IN sheet5, i have written a code like
`(val) Like "*[HH,HHJ,qqw,www]" Then
to check if user has provided input like NB or like NB, NS or like PE only then corresponding values from Risk will get displayed. Now with the above code , if user is entering value as "," also results is getting displayed as , is present in the Like statement.
Kindly guide me.
You can't use VBA Like that way. Try:
X = "abc, NB"
If X Like "*NB" Or _
X Like "*NS" Or _
X Like "*NF" Or _
X Like "*PE" Then
Debug.Print "X is in the Maritimes"
End If
If you set Option Compare Text at the beginning of your macro, you don't have to Ucase the string being tested.
For complex comparisons, you can use Regular Expressions in VBA. You need to set the appropriate reference.
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.