My problem: Converting an Excel range from a currency format (USD) to another (EUR) and vice versa.
USD format in Excel looks like 123,456.00
EUR format in Excel looks like 123 456,00
where in USD values comma is the thousand separator and . is the decimal point
and
in EUR values space is the thousands separator and comma , is the decimal point.
I've tried some coding in VBA but I'm a newbie in this area.
Any help to build a VBA macro code for using in Excel spreadsheets?
This UDF will do the job. Copy and paste into a module:
Function ConvertUSDToEUR(Amount As Range) As String
Dim TempStr As String: TempStr = Amount.Value
TempStr = Replace(TempStr, ",", " ")
TempStr = Replace(TempStr, ".", ",")
ConvertUSDToEUR = TempStr
End Function
Output:
With a value in A1, In B1 enter:
=TEXT(A1,"#,##0.00")
and in C1 enter:
=SUBSTITUTE(SUBSTITUTE(B1,","," "),".",",")
For example:
NOTE:
It is also possible to transform the values "in place", but this would require VBA.
EDIT#1:
Here are two macros that will change formatting "in place". Thus the results will still be numbers, but the appearance will change. Say we have a list of numbers (not formulas):
Select the values and run this macro:
Sub MakeUS()
Dim r As Range
For Each r In Selection
r.NumberFormat = "#,##0.00"
Next r
End Sub
to produce:
and this macro:
Sub MakeEUR()
Dim r As Range, v1 As String, v2 As String, DQ As String
DQ = Chr(34)
For Each r In Selection
v1 = Format(r.Value, "#,##0.00")
v2 = Replace(Replace(v1, ",", " "), ".", ",")
r.NumberFormat = DQ & v2 & DQ & ";;;"
Next r
End Sub
to produce:
If the column contained formulas, the approach would be slightly different.
Related
I want to convert a date in a cell to the date function so it is a formula. How do I get the date (using VBA), any date, say, 13 Jun 2020 to =DATE(2020, 6, 13) using variables for the year, month, and day. My code I have tried but won't work. The activecell shows 13-Jun-2020 as a date but appears in the function box as 13/06/2020
Sub ConvertDateToDateFunction()
Dim mvDay, mvMth, mvYr As Integer
mvDay = Left(ActiveCell, 2)
mvMth = Mid(ActiveCell, 4, 2)
mvYr = Right(ActiveCell, 4)
ActiveCell.Value = "=DATE(mvYr, mvMth, mvDay)"
End Sub
You have two problems. Here is the solution to the smaller one. The code below would do what you intend. It would convert a text string in the ActiveCell to a function of similar value and insert it in the cell below the ActiveCell.
Sub ConvertDateToDateFunction()
' if you don't say what it's supposed to be it'll be a Variant
Dim mvDay As String, mvMth As String, mvYr As String
mvDay = Left(ActiveCell.Value, 2)
mvMth = Mid(ActiveCell.Value, 4, 2)
mvYr = Right(ActiveCell.Value, 4)
ActiveCell.Offset(1).Formula = "=DATE(" & mvYr & "," & mvMth & "," & mvDay & ")"
End Sub
It's not entirely easy to insert a date as a text string in Excel because Excel will try to recognize a date for a date. Observe that any part of a string is a string, not an integer.
Now about your much bigger problem which is that you don't understand how Excel handles dates. It is such a big problem because you are trying to create a date in Excel in various ways and you run into all sorts of trouble. Read up on the subject here.
To give you a taste of what you will learn: what you see displayed in a cell isn't what the cell contains. There might be a formula in it and you see a number. And there might be a date and you see a string. What you see is determined by the cell's format. I think Chip Pearson's article will cover that topic. If you need to know more, look for "Cell formatting" on the web.
Your macro won't work because the date is a "real date" and not a string.
Try the following to convert the contents of cells containing a real date to a formula which will return the same date:
Option Explicit
Sub dtToFormula()
Dim R As Range, C As Range
Dim vDateParts(2)
Set R = [a1:a10]
'Set R = ActiveCell 'or Selection whatever range you want to convert
For Each C In R
If IsDate(C) And Not C.HasFormula Then
vDateParts(0) = Year(C.Value2)
vDateParts(1) = Month(C.Value2)
vDateParts(2) = Day(C.Value2)
C.Formula = "=DATE(" & Join(vDateParts, ",") & ")"
End If
Next C
End Sub
I have a sample data (1,2,3,4) on A2:D2 columns and on F1, I have (1,3), and on G1 (2,3,4).
I was suggested to use this formula
=INDEX($A2:$D2,AGGREGATE(15,6,ROW($A2:$D2)/ISNUMBER(SEARCH(TEXT($A2:$D2,"000,"),F$1&",")),1))
on F2, and G2 which is supposed to give the matching values on F2 and G2 respectively. Since both values (1,3) match, the result should be (1,3) on F2, and (2,3,4) on G2.
So basically this formula is supposed to work like index/match formula but instead of single values, it works on comma separated 2 or 3 values.
When I try to use the formula I keep getting #NUM! error. Can I get a little help to make this formula work, please?
Thanks.
Note: I am open to alternative solutions, but if it requires VBA, it has to be with a user defined formula. Without UDF, it is not useful for me.
A UDF should look like this
Option Explicit
Function MultFind(r As Range, s As String) As String
Dim c As Range
Dim result As String
For Each c In r
If InStr("," & s & ",", "," & c & ",") <> 0 Then result = result & c & ","
Next c
If Len(result) > 0 Then result = Left(result, Len(result) - 1)
MultFind = result
End Function
Called as
=MultFind($A2:$D2,F1)
Edit
Here it is with optional parameter for delimiter:
Option Explicit
Function MultFind(r As Range, s As String, Optional delimiter As String = ",") As String
Dim c As Range
Dim result As String
' Check each cell in range against search string
For Each c In r
If InStr(delimiter & s & delimiter, delimiter & c & delimiter) <> 0 Then result = result & c & delimiter
Next c
' Delimiter may be more than one character
If Len(result) > 0 Then result = Left(result, Len(result) - Len(delimiter))
MultFind = result
End Function
I have below text in Excel cell:
$abcd.$efghijk.$lmn.$op.$qrst.
I want above text in following format in Excel cell using Excel formula only:
abcd$abcd.efghijk$efghijk.lmn$lmn.op$op.qrst$qrst.
Here's what I will suggest based on discussion.
In a general module, insert following code.
Public Function RepeatCustom(strInput As String) As String
Dim varInput As Variant
Dim i As Long
If Len(strInput) = 0 Then
RepeatCustom = ""
Else
varInput = Split(strInput, ".")
For i = LBound(varInput) To UBound(varInput)
RepeatCustom = RepeatCustom & " " & Mid(varInput(i), 2, Len(varInput(i))) & varInput(i)
Next
RepeatCustom = Replace(Trim(RepeatCustom), " ", ".") & "."
End If
End Function
And then assuming cell containing original data is A2, you can use above UDF:
=RepeatCustom(A2)
Just note that the code is minimum and is based on sample posted.
I want to change the following
123456789A1
to
123-456-789 A1
Background:
In Format Cells, I used this:
000-0000-00 00
And that works if everything in the cell is a number,
12345678911
will become
123-4567-89 11
But as soon there is a letter, it breaks it.
How can I change the type to ignore letters?
this is an example :
Sub test()
a = [A1]
b = Left(a, 3) & "-" & Mid(a, 4, 3) & "-" & Mid(a, 7, 3) & " " & Right(a, 2)
[A2] = b
End Sub
But want you 3 or 4 numbers in the seconde position?
if A1=123456789A1 then in A2 : 123-456-789 A1
Cell number formatting only works on numbers.
If you don't mind changing the values into text strings, you could format them in VBA using the Format function. For example:
Option Explicit
Sub FMT()
Dim R As Range, C As Range
Const sFMT As String = "###-####-## ##"
Set R = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each C In R
C.Value = Format(C.Text, sFMT)
Next C
End Sub
You will need to change the range arguments to match what you need. And you could also use this in an event-triggered macro to do it automatically.
I have a one column filled with values like 3 days, 6 days, etc.
How can I strip out the text and force convert these to integers?
If you want to convert them "in-place", then select the cells and run this little macro:
Sub fixData()
Dim r As Range, v As String, i As Long
For Each r In Intersect(Selection, ActiveSheet.UsedRange)
v = r.Text
i = InStr(1, v, " ")
If i <> 0 Then
r.Value = Mid(v, 1, i - 1)
End If
Next r
End Sub
You could try substitute on a column next to you data:
=substitute(A1," days","") +0
Substitute replaces the string part " data" with nothing (zls "") and adding zero returns a number. This assumes your data is in col A. Drag down the formula and voila
If you don't mind changing the data you could also just select it all hit ctrl+F choose find and replace find field is " days", leave replace with blank and hit "Replace all"
If its VBA, you could actually use the Val function to do this.
If A1 has the data and you want this integer value in A2
So something like:
Range("A2").value = Val(Range("A1").value)
The returned value is actually a double. If a double isn't good enough,
Range("A2").value = CInt(Val(Range("A1").value))
Hope that helps.