Fat fingered decimal entry VBA for excel [duplicate] - vba

This question already has answers here:
Decimal TryParse in VBA
(2 answers)
Closed 4 years ago.
I made a simple vba application that allows a user to enter data into excel. The data that's being entered had to be >= 1.99 ohms and can be 3.00 or higher. Everything works great and it's been in use for over a year now. However, recently a user fat fingered a decimal point and didn't catch it until they entered the number. VBA didn't like that and errored out. The format they entered looked like so: '.2.34'. I can't seem to figure out how to avoid this. Any suggestions?
I've tried using:
If Res.value <> Format(Number, "#.##") Then
Msgbox ("blah blah")
Res.Value = ""
Exit Sub
End if
This blocks those entries, but it also blocks correct entries.

Maybe you could add Data Validation rules on the typical ranges. Try this: Data > Data Tools > Data Validation

Related

Clipboard data sort - Colon sorting issue

I asked a question on Stack Overflow a few days ago asking for a solution for copying email support forms and pasting the data into the appropriate text fields on a form. I got an answer which solved my problem - until I went to go use the tool and realized a problem.
I am using this code:
'Service Plan Description
For i = 0 To lines.Count - 1
If lines(i).StartsWith("Service Plan Desc. :") Then
StartLine = i
Exit For
End If
Next
tbx_ServicePlanDescription.Text = lines(StartLine).Split(":"c)(1).Trim 'Put sorted data into textbox
And when I copy the following text into the clipboard to test the Service Plan textbox:
Maint:AbloEnterprise S/W AddOn (5)
It only pastes 'Maint' in the textbox because the code purposely removes the colons.
So my question is: What would be a way to overcome this?
I used the Split(Char[], Int32) method to limit how many parts the split would split the string into, as suggested in the comments, like this:
tbx_ServicePlanDescription.Text = lines(StartLine).Split({":"c}, 2)(1).Trim()

Why does Round produce a different result than Application.Round [duplicate]

This question already has an answer here:
Round function in Excel, worksheet function vs VBA
(1 answer)
Closed 6 years ago.
I am finding that the VBA Round function returns the wrong result. I found a working alternative which is to use the Application.Round instead.
Why doesn't the VBA Round function work as expected?
Sub roundingResults()
Range("A1") = 123456.705
Debug.Print "Approach #1 " & Round(Range("A1").Value, 2)
Debug.Print "Approach #2 " & Application.Round(Range("A1").Value, 2)
End Sub
Output
Approach #1 123456.7
Approach #2 123456.71
There is no entirely correct answer for how to round. The differences have to do with how to handle values that fall exactly on the midpoint. Application.Round will always round up in that instance. e.g. 0.5 will round to 1.0. VBA.Round will tie break to the closest even number
1.5-->2
2.5-->2
3.5-->4
and so forth. This is one way of several ways of reducing the bias inherent in always rounding up.
Also, if interested, there is a fairly extensive discussion of Rounding in this Wikipedia article

VBA code 3 columns to 1, using a specific order [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a list of data, distributed with a non-homogeneous pattern, on 3 columns. The challenge is to write a vba code "smart enough" to copy and paste all these numbers on one column, putting them in order, one below the other one: 1, 2, 3, 4, 4.1, 4.2 etc.. without missing any of them.
Could someone help me in this task? Because I see a lot of complexity and I have no idea how to manage it. Thank you!
As I understand it, you're looking to order this in a specific way that isn't necessarily how Excel would sort by default. The values look like version numbers or nested task IDs.
Stack Overflow isn't really a "solve your problem for you" kind of place, but I can definitely get you started. Here's what you'll need to do:
Get all the values, preferably into a Collections object. Make sure to omit blank cells.
Convert each value into a new format which a) is sortable, and b) can be reverted to the original format. For example, let's say these are version numbers, and any given number can be as high as 999, and there can be up to 4 items (e.g. 123.10.15.9 is valid, as is 9.5). Convert these to the form 000000000000 (that's 12 0s). You can do this by using Split() to divide the values using the "." delimiter, then padding the value out. This might look something like this:
.
'Converts 1 => 001000000000
' 1.1 => 001001000000
' 2.4.7 => 002004007000
' 65.339.1 => 065339001000
Function ConvertToSortableVersionNumber(value As String) As String
'Add all the parts of the value (. delimited) to a collection
Dim vnPart As Variant
Dim error As Boolean: error = False
Dim Parts As Collection
Set Parts = New Collection
For Each vnPart In Split(value, ".")
'Make sure this can actually be formatted as needed.
If Len(vnPart) > 3 Then
error = True
Exit For
End If
'Add this part to the Parts collection
Parts.Add vnPart
Next vnPart
'Now make sure there are 4 parts total
If Parts.Count > 4 Then error = True
'Check for errors
If error Then
'There is an error. Handle it somehow
End If
'If there are less than 4 items in the collection , add some
While Parts.Count < 4
Parts.Add ("000")
Wend
'Now, assemble the return value
Dim retVal As String
Dim item As Variant
For Each item In Parts
retVal = retVal & Right(String(3, "0") & item, 3)
Next item
'All set!
ConvertToSortableVersionNumber = retVal
End Function
Sort the collection (this page has a good example of sorting a collection in memory).
Create an array with new values converting back to the original format (much easier since the inputs will be highly standardized).
Write the array to a new range--and you're done!
Take a stab at it. I think you'll find that Stack Overflow is much more helpful once you can show the work you've already done, what you've tried, and what specifically is giving you trouble.
Good luck!
If you have 2010 or later the following formula will do it:
=IFERROR(SUBSTITUTE(SUBSTITUTE(AGGREGATE(15,6,--SUBSTITUTE($A$1:$C$10,".","000")*--(1 & REPT("0",9-LEN(SUBSTITUTE($A$1:$C$10,".","000")))),ROW(1:1)),"0000",""),"000","."),"")
If you have 2007 or earlier than it will need to be an array formula:
=IFERROR(SUBSTITUTE(SUBSTITUTE(SMALL(IF($A$1:$C$10<>"",--SUBSTITUTE($A$1:$C$10,".","000")*--(1 & REPT("0",9-LEN(SUBSTITUTE($A$1:$C$10,".","000"))))),ROW(1:1)),"0000",""),"000","."),"")
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when leaving edit mode.
Column E is the first formula and Column F is the second.

IF Without End IF in VBA [duplicate]

This question already has answers here:
IF statements in VBA
(5 answers)
Closed 7 years ago.
see the code below.
IF Weight=0 Then CallFunction
IF Weight=100 Then
Heavy=True
Else
Heavy= False
End If
How is the program working when I have not ended a if statement?
its VBA's functionality to end up if statements in a single line..
If Weight is equal to zero, then CallFunction is executed (if Weight is not equal to zero, a CallFunction is not executed), then the code is going to another if statement. That's all! Debug the program to find out ;)

apply user-defined format in excel 2007 vba

The excel 2007 format i am using is: #,##0.00,,,_);[Red](#,##0.00,,,);"-"
if is is possitive, i want the number to display in billions; if negative, in billions and in parentahis; if missing, "-".
It works fine with excel 2007. But when i tried to apply the format in vba, it did not work.
Please use the following numbers as example:
-11467478
224785.66
-5579046
1904770.9
-14916968
The data type i used is variant. shall i use long?
my initial code is something like:
......
with worksheet
'cells(1,1) would be any of the above numbers
.Cells(1, 1).NumberFormat = "#,##0.00,,,_);[Red](#,##0.00,,,);" - ""
end with
.....
I got an erros message run-time error 13, type mismatch
I even tried to decompose the format. but it still did not work.
i am quite new to vba. could anyone help me?
......
This will also work, the dash is one of the characters that don't need to be escaped...
"#,##0.00,,,_);[Red](#,##0.00,,,);-"
You need to use double " for the minus sign. I tested your values and I think it should be:
.NumberFormat = "#,##0.00,,,_);[Red](#,##0.00,,,);""-"""