I'm working on editing a project in visual basic, and don't have a whole lot of experience with vb.
I have a text box where a user can enter a number. The number should be stored as a double. Then, I need to convert the number to it's equivalent 16-byte hexadecimal representation. Any tips on how to do this?
I have just had this kind of problem. My idea was to take the time from the function Now() and to split it by the comma. Then to convert it to Hex. After some time of researching, I came up with the idea that the input should be rounded. to 8, otherwise we may get an overflow error. This is my code:
Public Function codify_time() As String
If [set_in_production] Then On Error GoTo codify_Error
Dim dbl_01 As Variant
Dim dbl_02 As Variant
Dim dbl_now As Double
dbl_now = Round(Now(), 8)
dbl_01 = Split(CStr(dbl_now), ",")(0)
dbl_02 = Split(CStr(dbl_now), ",")(1)
codify_time = Hex(dbl_01) & "_" & Hex(dbl_02)
On Error GoTo 0
Exit Function
codify_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure codify of Function TDD_Export"
End Function
Related
I'm trying to make a simple text converter program for my first Visual Basic program. I've already written this in Python but I'm not sure how to do it in Visual Basic.
I need the program to run through the characters of a string and once it encounters a ( to then remove the bracket and ignore the rest of the text until it encounters a ).
For Example,
"This is a (not so good) sentence",
becomes
"This is a Sentence".
Previously I did this with a for loop what looked at a character in the string, then checked if it was open. If it wasn't it would append the character to an output string to the next character or if it was it would then trigger a Boolean to true what would stop the character being appended. It would then continue to stop the future characters from being appended until it found a close bracket. At that point, it would then make the Boolean false, stop the closed bracket from being appended and move back to appending characters, unless it was another bracket.
Sorry if this description is a bit rough as I'm not the best at describing things and I'm very new to visual basic. Thanks for any help given
You can achieve this by several different methods. Here is one method using Instr. Instr returns the character position (index) of a given string in another string. You can use this to determine the bounds of where to include/exclude the string chunk.
You didn't specify if there could be multiple sections encapsulated in () so I assumed there wouldn't be. However, this is a relatively easy tweak by adding either a Do...Loop or a While... loop in the Function.
Hope it helps:
Option Explicit
Public Function removeBrackets(Source As String, Optional RemoveDoubleSpaces As Boolean = False)
Dim FirstBracket As Long
Dim SecondBracket As Long
FirstBracket = InStr(1, Source, "(")
SecondBracket = InStr(1, Source, ")")
If FirstBracket >= SecondBracket Or FirstBracket = 0 Or SecondBracket = 0 Then Exit Function
removeBrackets = Left$(Source, FirstBracket - 1) & Right$(Source, Len(Source) - SecondBracket)
If RemoveDoubleSpaces Then removeBrackets = Replace$(removeBrackets, " ", " ")
End Function
'Run this
Sub Test()
Debug.Print "The value returned is: " & removeBrackets("This is a (not so good) sentence") ' Example given
Debug.Print "The value returned is: " & removeBrackets("This is a (not so good) sentence", True) ' Example given, slight revision. Remove double spaces
Debug.Print "The value returned is: " & removeBrackets("This is a (not so good sentence") ' missing ending bracket
Debug.Print "The value returned is: " & removeBrackets("This is a not so good) sentence") ' missing starting bracket
Debug.Print "The value returned is: " & removeBrackets("This is a not so good sentence") ' No brackets
End Sub
I have this code where ttlMtrs is an integer < 7000
diffFormula = "=1-(" & ttlMtrs & "/7000)*1200"
Debug.Print diffForumla
I am trying to build a string that will evaluate once printed to a cell in excel.
When I Debug.print to check the string I only get blank lines. One time I got long string of garbage characters.
I am trying to figure out what I am misunderstanding in building this string. Any ideas are appreciated.
you have mispelled the variable name in the debug statement
Debug.Print diffForumla
should be
Debug.Print diffFormula
note the changed position of the u
Public Sub Test()
Dim ttlMtrs As String
Dim diffFormula As String
diffFormula = "=1-(" & ttlMtrs & "/7000)*1200)"
Debug.Print diffFormula
End Sub
This should be the result:
=1-(/7000)*1200)
I have a declaration like number= InputBox("Number for:", "Number:"), number is declared as Dim number As Double but when I enter a double number, for example 5.4, into the Inputbox and transmit it into a cell, the cell shows me 54, it deletes the point.
How can I fix this?
THX
If you want to detect which settings your Excel uses for the Decimal seperator, try the code below:
MsgBox "Excel uses " & Chr(34) & Application.DecimalSeparator & Chr(34) & " as a decimal seperator"
if you want to change it to ., then use the line below:
Application.DecimalSeparator = "."
Unfortunately, VBA is horrible at handling differences in decimal seprators. In your case, you should probably use a comma (,), instead of a punctuation/dot (.).
Edit: Using the Application.DecimalSeparator method, it now works regardless of regional settings. Be aware though, it seems to cause some issues if you change the comma separator settings for Excel (it seems that VBA somewhat ignores this setting). If you do not change that however, the example should work in all other cas
Sub GetNumberFromInputBox()
Dim val As String
Dim num As Double
'Get input
val = InputBox("Number for:", "Number:")
Debug.Print Application.DecimalSeparator
If IsNumeric(val) Then
'Try to convert to double as usual
num = CDbl(val)
'If the dot is removed automatically, then
'you will se a difference in the length. In
'those cases, replace the dot with a comma,
'before converting to double
If Len(val) <> Len(num) Then
If Application.DecimalSeparator = "," Then
num = CDbl(Replace(val, ".", ","))
Else
num = CDbl(Replace(val, ",", "."))
End If
End If
'Pring the number
Debug.Print "You selected number: " & num
Else
'If its not a number at all, throw an error
Debug.Print "You typed " & val & ", which is not a number"
End If
End Sub
I'm finishing up a project and needed some clarification on a particular issue I'm running into.
Do While intCount < dblNumbers.Length AndAlso strInput <> String.Empty
dblNumbers(intCount) = CDbl(InputBox("Please enter number " & (intCount + 1) & "."))
intCount += 1
Loop
I'm running into issues changing that a to Double.TryParse for validating as opposed to the CDbl. Any of the ways I've entered it have caused the inputs to not store in the array. Thanks in advance.
TryParse doesn't return the converted value. It returns the validation result. You get the converted value from the parameter.
Dim number As Double
If Double.TryParse(InputBox("Please enter number " & (intCount + 1) & "."), number) Then
dblNumbers(intCount) = number
Else
'Invalid input.
End If
Hello i've a little problem with my VBA code i'm trying to select the right gps number (double) which matches to the string Name in table tblpersonal and the string in the textbox tabletbesitzerbox. The GPS number should be displayed in the textbox fkgps:
Private Sub SP_Besitzersuche_Click()
DoCmd.OpenForm "F-Tablet-Hinzufuegen-Neu"
Dim Sim As Double
Sim = Nz(DLookup("[GPS]", _
"tblPersonal", _
"Name = " & Forms![F-Tablet-Hinzufuegen-Neu]![tabletbesitzerbox]), "")
FKGPS.Value = Sim
End Sub
The error shows me: Syntaxerror (missing Operation) in query expression 'Name = XY'
I'm thankful for every help :)
String parameters must be enclosed in quotes. When building the criteria in VBA, it is easiest to use single quotes:
Sim = Nz(DLookup("[GPS]", _
"tblPersonal", _
"Name = '" & Forms![F-Tablet-Hinzufuegen-Neu]![tabletbesitzerbox] & "'"), 0)