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.
Related
Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub
your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs
You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.
Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub
your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs
You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.
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 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 code that returns all the data I want from a query. The problem is that the csv format separates the data, even account names with commas in them. For each row, column A is the account name followed by 11 blocks of integer data (making a total of 12 cells used per row). Fortunately, for the accounts that have commas, the result is only one additional cell (making a total of 13 cells used per row).
I need an IF-THEN formula that will concatenate Column A & Column B if there are 13 used cells in that row, otherwise leaving things alone. Being new to VBA concatenate is giving me huge problems.
Any suggestions? Thanks in advance.
Well, just from a term point of view, Excel Formulas and Excel VBA are not the same thing.
In fact, there are a lot of people on here that are formula pros that know nothing about VBA and visa versa.
That being said, you could use something like:
=IF(COUNTBLANK(C1:N1)=0,A1&" "&B1,A1)
Of course, assuming your data looks like this:
It would be easier to use:
=IF(N1="",A1,A1&" "&B1)
Concatenating in Excel formulas and VBA is pretty simple.
Simply separate the strings you wish to concatenate with an amperstand &
If I want to concatenate A1 and B1, I can use =A1&B1 as shown in the formula.
If I want to add a space, or even a word, I can just add it between them as a string:
=A1&" is way cooler than "&B1
Edit:
VBA:
Sub ConcatenateCells()
Application.ScreenUpdating = False
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then c = c & c.Offset(0, 1)
'You could also add a space between the two using c = c & " " & c.Offset(0, 1)
Next c
Application.ScreenUpdating = True
End Sub
I don't know if you want to scoot the data over after you have concatenated the values, but if you do, you can use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then
c = c & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub
Still assuming I got your data format right, here is what the before and after looks like:
Final Edit:
If you absolutely insist on counting the number of used rows instead of simply looking for a coma in column A, then use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If Cells(c.row, Columns.Count).End(xlToLeft).Column = 13 Then
c = c & " " & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub