VBA : Number format = String Variable + "#,##0" - vba

Very basic question - somehow can't solve this :-(
Can you please help me to set the number format on a cell to be a combination of a string variable and this number format : "#,##0"?
The objective is to have results appear as EUR #,##0 (if string variable = EUR) or GBP #,##0 (if string variable = GBP) etc.
So far, this is how I was trying to code it:
Dim Currency As String
Set Currency = "EUR"
Cells(1,1).NumberFormat = Currency & " #,##0"
Thank you in advance for your help!
Kind regards,
Claudia

Related

Multiply every record in Double column with a decimal number

I have a column 'Price' of type Double.
I also have a form with a button that should open an Inputbox to let the user enter the amount of discount they want, e.g. The user enters 0,6 (60% discount) and a calculation is done, 1 - 0,6 = 0,4. 0,4 is the number to multiply every record in the Price column with.
I don't seem to get this to work.
One of my tries when entering 0,5 only multiplied by 5, it skipped the 0, before the 5. which was weird.
Dim strDiscount As String
Dim discount As Double
Dim holder As Double
Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = CurrentDb
strDiscount = InputBox("Enter how much discount you want:", "Amount of discount")
holder = Val(strDiscount)
discount = (1-holder)
strSQL = "UPDATE Prislista SET Listpris = Listpris *" & _
discount
dbs.Execute strSQL
One of the tries gave an error messages telling me I have the wrong location set on my windows OS. Something about using comma instead of dot. Well, I have my windows and ms-access on swedish and it doesn't matter to me whether I should use comma or dot when entering the discount.
First, always store amounts and quantities as Currency if four decimals is adequate.
Next, use CCur to convert your localised text from InputBox to a number.
Third, use Str to convert a decimal to its neutral string expression.
Then:
Dim strDiscount As String
Dim discount As Currency
Dim holder As Currency
Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = CurrentDb
strDiscount = InputBox("Enter how much discount you want:", "Amount of discount")
holder = CCur(strDiscount)
discount = (1 - holder)
strSQL = "UPDATE Prislista SET Listpris = Listpris * " & Str(discount) & ""
dbs.Execute strSQL
Example:
strDiscount = "0,6"
holder = CCur(strDiscount)
discount = 1 - holder
? Str(discount)
.4
While the user interface in access will use your local settings, VBA uses US settings only.
So you'll need to convert 0,5 to 0.5.
The below should work:
holder = Val(Replace(strDiscount,",","."))

Error "The Remaining Text Does Not Appear To Be Part Of The Formula" Crystal Reports 11.5.8.826

I'm receiving the error stated in the question title in Crystal Reports. I've been troubleshooting for a while and I'm drawing blanks. I have some simple If..Then..Else statements and Select..Case statements in separate formulas, but I'm receiving the same errors for all of them. I'm writing the formulas in VB.Net and I suspect the issue is stemming from how I'm declaring my variables.
An example of a formula is:
Dim xyz As Number = {VALUE1}
Dim array1 = New Integer() {1111, 1112, 1214, 1215}
Dim array2 = New Integer() {1211, 1212, 1213, 1414, 1415}
Dim array3 = New Integer() {1311, 1312, 1514, 1515}
Dim array4 = New Integer() {1911}
If Array.IndexOf(array1, xyz) >= 0 Then
{VALUE2} & "_001"
ElseIf Array.IndexOf(array2, xyz) >= 0 Then
{VALUE2} & "_002"
ElseIf Array.IndexOf(array3, xyz) >= 0 Then
{VALUE2} & "_003"
ElseIf Array.IndexOf(array4, xyz) >= 0 Then
{VALUE2} & "_004"
Else
{VALUE2}
End If
When I enter that formula into the selected field it highlights everything after the = sign on line 1 (Dim xyz As Number =...).
After searching I've tried the use of colons with equal signs (:=) and semi-colons to end if statement, but couldn't find much more online to guide me. I did find threads suggesting a .dll may be missing, but I don't currently have write permissions to a lot of areas on the system I'm working with so any fix like that is out of question for me.
My question in short is why am I receiving this error? Is it my If..Then..Else syntax or how I'm declaring variables? Or am I just not supposed to use VB.Net in crystal reports like I am doing?
Crystal does support a Basic Syntax, but it is more akin to VBA or VB6 than .Net, and also has its own peculiarities and limitations.
No initalization on declaration
Return value is "Formula =", it is not implied as in Crystal Syntax.
Most functions and operators are more like the Crystal Syntax than regular basic.
All return values need to be of the same type.
Here is what I think you were going for:
Dim xyz As Number
Dim array1() as number
Dim array2() as number
Dim array3() as number
Dim array4() as number
xyz = {VALUE1}
array1 = array(1111, 1112, 1214, 1215)
array2 = array(1211, 1212, 1213, 1414, 1415)
array3 = array(1311, 1312, 1514, 1515)
array4 = array(1911)
If (xyz in array1) Then
Formula = {VALUE2} & "_001"
ElseIf (xyz in array2) Then
Formula = {VALUE2} & "_002"
ElseIf (xyz in array3) Then
Formula = {VALUE2} & "_003"
ElseIf (xyz in array4) Then
Formula = {VALUE2} & "_004"
Else
Formula = totext({VALUE2})
End If
Your syntax is wrong to use in Crystal report, it always start with datatype then variable name like
numbervar pos;
stringvar cpu;
numbervar pos := 0;
stringvar cpu := 'abc';
NumberVar k := {#Counting_Data}/{#Counting_Fail}
This link for you similar question have crystal reports error : remaining text does not appear to be part of the formula
If you more learn about variable, there are 3 types of variable in terms of scope
Local
Global
Shared
https://www.tutorialspoint.com/crystal_reports/crystal_reports_creating_variables.htm

VBA : How to define a large set of string variables?

I am trying (in VBA) to define a large number of string variables. The brute force would be :
Dim Port1 as String
Dim Port2 as String
etc…
Unpleasant for say 100 variables. There must be a more intelligent solution.
I have tried :
Dim n As Integer
For n = 1 To 100
Dim "Port" & n as String
Next n
and variations of it without success.
I would greatly appreciate if someone could point me in the right direction or share an example.
Arrays do exactly this! See this example:
Dim Port(1 to 100) As String
Dim i As Long
Port(1) = "String"
For i = 2 to 100
Port(i) = "String " & i
Next i
It sets Port(1), the first array element to the word "String". Everything else Port(2) and on, contain "String 2", "String 3", etc. Up to Port(100).
I hope that helps!

How to find a letter in between two letters in visual basic?

Sorry for the awkwardly worded question. In visual basic I have a prompt asking the user to "Enter a letter between A and D:"
If ValidChar(chrLetter) Then
Me.lblLetterResult.Text = chrLetter & " is a valid letter"
Else
Me.lblLetterResult.Text = chrLetter & " is not a valid letter"
End If
Function ValidChar(ByVal chrLetter As Char) As Boolean
Dim chrLowChar As Char = "D"
Dim chrHighChar As Char = "A"
If chrLetter >= chrLowChar And chrLetter <= chrHighChar Then
Return True
Else
Return False
End If
End Function
Obviously this isn't correct, but I'm not sure what the correct code should be. If the user were to enter the character "A" then it should display "A is a valid number". If the user were to enter "X" then it should display "X is not a valid number". Any help is appreciated!
Don't you just need to reverse your logic? D is greater than A.
Dim chrLowChar As Char = "A" ' ascii decimal value of 65
Dim chrHighChar As Char = "D" ' ascii decimal value of 68
Review decimal values for ASCII characters for more information on character values
http://www.asciitable.com/
Just create a string object "ABCD". Execute string.contains(). If using a wider range just convert to ASCII equivalent and use code similar to your example

VB.net incremented number concatenate with texbox value

I'm learning vb.net. I'm trying to create an incremental number that starts at 00000 and concatenate that number with a value from a textbox (eg. JH00001), then insert it into the database.
Please can someone kindly help me with this as I'm totaly new to vb.net.
Thank you all for your assistance in advance. And I'm sorry for my bad English.
Dim number as Integer = 1
Dim text as String = textbox1.text &= number.toString().padLeft(5, "0"c)
Use D5 precision specifier to indicate that the number should be at least 5 digits including leading zeros:
Dim valueFromTextBox As String = "JH"
Dim value As String = ""
For i = 0 To 99
value = valueFromTextBox & i.ToString("D5")
'Insert value to database
Next
Check MSDN for more formatting methods
A for loop should be what you need:
Something like:
Dim text As String = textbox1.text
Dim DBtext As String
For value As Integer = 0 To 5
DBtext = text & value.ToString()
'Insert anything else you need to do. Such as insert into DB.
Next
Just replace the 5 with however many times you need it to run.
I personally prefer using String.Format ...
For i = 0 to 1e6-1
Dim FormattedString = String.Format("{0}{1:00000}", Textbox1.Text, i)
Next