Use cell value as string VBA - string

Cell A2 contains value "25-86".
I need cell B2 to have value "AJ 2586%"
This code reveals Syntax error:
Range("B2").Value = "AJ & Left(Range("A2"), 2) & Right(Range("A2"), 2) & %"
If I write it like that
Range("B2").Value = "AJ & Left(A2, 2) & Right(A2, 2) & %"
The functions Left and Right treat "A2" as string.
How could I extract a part of text from the cell and enter it in another cell?

The issue is that everything between the " " defines a string.
You can type your code like this:
Range("B2").Value = "AJ " & Left(Range("A2"), 2) & Right(Range("A2"), 2) & "%"

The following formula will return the value as a real percentage that you can use in calculations.
It just removes the - from the figure.
=SUBSTITUTE(A2,"-","")/100
Initially it will display the value as 25.86.
When you apply the custom number format of:
\AJ 0%
It will then display as AJ 2586%.
If that formula is in A1 then =A1/2 will return AJ 1293%.
Edit:
Just realised you want it in VBA:
Range("B2") = Replace(Range("A2"), "-", "") / 100
Range("B2").NumberFormat = "\AJ 0%"
Or, if you just want it as text:
Range("B2") = "AJ " & Replace(Range("A2"), "-", "") & "%"

Range("B2").Value = "AJ " + Left(Range("A2"), 2) + Right(Range("A2"), 2) + "%"
Indent code 4 spaces in order for it to be in the correct format

Related

Looping through all the string values of cell by vba

So this is what I want to do:
For example, in the sheet named "Outputs", the A1 cell input is
= "abcd" & Input!A5 & "efgh",
where Input!A5 is the cell value of the different sheet in the excel file.
I want to make it change so that after executing all the methods after selecting the A1 cell as
= "abcd" & Input!A5 & "efgh", I want to change the A1 cell as, = "abcd" & Input!A6 & "efgh", and then = "abcd" & Input!A7 & "efgh" and so on. (So it's basically replacing the values as A1 to Ai)
I thought of using Replace function, by writing the replacing string as Ai, and replacing i with i +1 by starting with for loop.
But I don't think this is a right method.
Could anyone shed light on how to address this?
Thanks in advance.
Check this
InputWorksheetLastRow = 7 'place here your last row value
Set ws = Worksheets("Input")
For i = 5 To InputWorksheetLastRow
Worksheets("Outputs").Range("A" & i - 4).Formula = "=" & Chr(34) & "abcd" & Chr(34) & "&Input!" & ws.Cells(i, 1).Address(0, 0) & "&" & Chr(34) & "efgh" & Chr(34)
Next i

VBA putting special character in string

Range("E2").Formula = "=" & column1name & "2" & "" | "" & column2name & "2"
I am trying to make formula in VBA. I want to set E2 =( A2 | B2) . I already used function to convert column A into variable column1name already. So VBA will read it as A. but I am getting error that highlight my "|" and saying
invalid character.
How can I solve this problem?
Range("E2").Formula = "=" & column1name & "2 | " & column2name & "2"
will return
=A2 | B2
// Edit according comments
Range("E2").Formula = "=" & column1name & "2 & ""|"" & " & column2name & "2"
will output
=A2 & "|" & B2
If your intent is to join A2 and B2 cell through formula using PIPE (|) character then you can try below code.
Range("E2").Formula = "=" & column1name & "2&""|""&" & column2name & "2"
Alternative via Join()
In order not to loose a clear view over multiple quotation-mark sequences,
I'd suggest to separate into tinier parts as follows:
Const PIPE As String = """|""" ' pipe character with quotation-marks
Sheet1.Range("E2").Formula = _
"=" & Join(Array(column1name & "2", PIPE, column2name & "2"), "&")

Remove comma from concatenated string for last cell in Excel

I have a table in Excel that has 10 columns. For each row there are different numbers of cells populated e.g.
My concatenated string is as follows for HTML emails
(" + Machine1 + ", " + Machine2 + ", " + Machine3 + ", " + Machine4 + ", " + Machine5 + ", " + Machine6 + ")
However I want the comma to not show after the last cell in the row (whether that be column 3,4,5,6).
This is what I get
(TEST1, TEST2, TEST3 , TEST4, TEST5, , )
I want to remove the two commas at the end. Hope this makes sense!
If you have Office 365 Excel then use TEXTJOIN:
="(" & TEXTJOIN(", ",TRUE,A2:F2) & ")"
If not then you will need to use IF for each return and Mid:
=MID("(" & IF(A2 <> "",", " & A2,"") & IF(A2 <> "",", " & B2,"") & IF(C2 <> "",", " & C2,"") & IF(D2 <> "",", " & D2,"") & IF(E2 <> "",", " & E2,"") & IF(F2 <> "",", " & F2,""),3,999)
Alternative without IF
Just in addition to Scott's valid solution, I demonstrate an approach via the REPT function. Applied on a cell containing a string, it "repeats" its content & comma once (indicated by COUNTA() equalling 1), whereas an empty cell results in a zero repetition which allows to omit not only the cell content but the comma, too:
="(" & SUBSTITUTE(REPT(A1&",",COUNTA(A1))&REPT(B1&",",COUNTA(B1))&REPT(C1&",",COUNTA(C1))&REPT(D1&",",COUNTA(D1))&REPT(E1&",",COUNTA(E1))&REPT(F1&",",COUNTA(F1))&"$",",$",")")
A simple SUBSTITUTE removes the last comma, wherever it occurs before the closing bracket ")".

string text with blank cells

I am trying to string together text from cells across by row when some of the cells are blank. I also want to have commas separating the text from each cell but cannot figure out how to only have commas put in when there is data in a cell.
I have tried using variations of concatenate and stringing the text using A1&B1&C1 but nothing is returning the data in the format I want.
Is there a combination of nested formulas I can use that will return the data in the format I want?
Here's a VBA solution I had before the newer versions:
Public Function JOINCELLS(ByVal ref As Range) As String
Dim c As Range
For Each c In ref
If Len(c) > 0 Then
JOINCELLS = JOINCELLS & c & ", "
End If
Next
JOINCELLS = Left(JOINCELLS, Len(JOINCELLS) - 2)
End Function
Then, it's just =JOINCELLS(A1:D1).
The formula below could do the trick. Concatenate the val The trim would remove spaces from the ends, and the substitute would swap the space for the comma(space).
=SUBSTITUTE(TRIM(A1 & " " & B1 & " " & C1 & " " & D1), " ", ", ")
Edit from the comments below - you could update the spaces between the quotes to be any character that does not appear in your cells. You would just have to the substitute formula to match. If you don't use the | character for example:
=SUBSTITUTE(TRIM(A1 & "|" & B1 & "|" & C1 & "|" & D1), "|", ", ")

Can I add Formula to an cell from VBA without dollar symbol?

For exmaple, I want add formula to all selected cell where I click.
The Cells.Address is good for recognise the cell, but when I add the formula, and I after see the cell, the vba add lock dollars symbol and I want avoid that.
e.g.:
r = Target.Row
Cells(r, 1).Formula = "=" & Cells(r, 2).Address & "*" & Cells(r, 3).Address
Result in cell (if the target A1): =$A$2 * $A$3
But I want that result: =A2 * A3
Set the first two criteria of the .Address to 0:
Cells(r, 1).Formula = "=" & Cells(r, 2).Address(0,0) & "*" & Cells(r, 3).Address(0,0)
You have to set both the row-absolute and the column-absolute to false.
https://msdn.microsoft.com/en-us/library/office/ff837625.aspx shows in more detail.

Resources