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.
Related
Could someone give me some pointers on how to write this formula right?
Sub WriteFormulaTextAndNumbersDependentOnI()
Dim xNumber As Integer
xNumber = InputBox ("Choose I")
For I = 1 To xNumber
Worksheets("Sheet1").Cells(1, 1 + I).Formula = "= & 2,10 + 0,01 * (I - 1)'" & Sheets(I + 1).Name & "'!B12"
Next I
End Sub
The point here is to write a name in different cells, with a calculated number (2.10 + 0.01*(I - 1)) and text from another sheet:
Lets say we have in Sheet2:
B12 = Hello World
Lets say we have in Sheet3:
B12 = You are cool!
We should then get in Sheet1:
I = 1 would give a value/text in cell B2, with the value/text: "2.10+0.01*(1-1) B12" = "2.10 Hello World"
I = 2 would give a value/text in cell C2, with the value/text: "2.10+0.01*(2-1) B12" = "2.11 You are cool!"
An so on.
Any suggestions?
Appreciate any suggestions
//GingerBoy
use:
Concatenate() worksheet function to mix numbers and text in the same string
Text() worksheet function to format a number to a string
as follows:
Worksheets("Sheet1").Cells(2, 1 + I).Formula = "=concatenate(TEXT(2.10 + 0.01*(" & I & "-1),""0.00""), "" "", " & Sheets(I + 1).Name & "!B12)"
see that:
I used Cells(2, 1 + I) to write in in B2, C2 ... as per your narrative
I used dots (.) as decimal separator: you may want to change all dots into commas as per your decimal separator conventions
i have a cell A, B, and C.
A=16
B=01
C=0001
my question is i want a value of the cell D is like this D = 16-01-0001.
Note cell D is dynamic sometimes in cell D the value is 0021 or 0321 . .
Just concatenate the strings from the cells with the "-" text string. The concatenation operator is the ampersand sign &
Sub test()
Range("D1") = Range("A1") & "-" & Range("B1") & "-" & Range("C1")
End Sub
Or use a formula instead of VBA:
=A1&"-"&B1&"-"&C1
Range("D1").Value = Range("A1").Value - Range("B1").Value - Range("C1").Value
Note: Here, the values within the brackets refer to the cells the numbers are located in, these are just examples.
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.
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.
Let's say I have a series of cells like so:
A
1 Foo
2 Bar
3 Hello
4 World
5 Random Text
What I'd like to do is have the result of my formula populate another cell with:
Foo, Bar, Hello, World, Random Text
Now, I know how to concatenate two cells with:
=A1&", "&A2
but how can I do the same thing with the entire series?
Here's a function you might be able to use. Simply put this in your workbook code module, then you can enter it in cells like:
=JoinRange(A1:A6) or =JoinRange(A2:D15), etc.
Public Function JoinRange(ByVal rng As Range) As String
Dim dlmt As String: dlmt = ","
Dim multiRow As Boolean: multiRow = rng.Rows.Count > 1
Dim r As Long, c As Long
Select Case rng.Columns.Count
Case 1
If multiRow Then
JoinRange = Join(Application.WorksheetFunction.Transpose(rng), dlmt)
Else:
'a single cell
JoinRange = rng
End If
Case Is > 1
If multiRow Then
'a 2d range of cells:
For r = 1 To rng.Rows.Count
For c = 1 To rng.Columns.Count
JoinRange = JoinRange & rng(r, c) & dlmt
Next
Next
JoinRange = Left(JoinRange, Len(JoinRange) - 1)
Else:
JoinRange = Join(Application.WorksheetFunction.Transpose( _
Application.WorksheetFunction.Transpose(rng)), dlmt)
End If
Case Else
End Select
End Function
Put a comma and a space in cell B1, then use this formula:
=CONCATENATE(A1,B1,A2,B1,A3,B1,A4, B1, A5)
There are several answers to the following question that you can try as well, including VBA options and a formula:
Need to concatenate varying number of cells...
With =A1 in B1 then =B1&", "&A2 in B2 and copied down would seem to work.