VBA putting special character in string - excel

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"), "&")

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 Evaluate - Match

I have a problem with my codes below,
As you can see, I am trying to find the row value of a search. I have 3 different criteria ( they are defined before ) and 3 different ranges ( also defined before ). But I cannot find the rows unfortunately.
Here you can see the codes;
Gorev = Worksheets(WS_All).Cells(p, o).Value
SlideNo = Worksheets(WS_All).Cells(p, 34).Value
Egitim_Adi = Worksheets(WS_All).Cells(2, 3).Value
Check1 = Worksheets(j_WS).Range("A:A") 'Egitim_Adi Kontrolü için'
Check2 = Worksheets(j_WS).Range("B:B") 'SlideNo Kontrolü için'
Check3 = Worksheets(j_WS).Range("C:C") 'Gorev Kontrolü için'
Satir_bul = Evaluate("=Match(" & Egitim_Adi & " & " & SlideNo & " & " & Gorev & ", Check1 & Check2 & Check3, 0)")
I am open to any suggestion..
You have a whole heap of problems there. WorksheetFunction.Match is not going to work because you are trying to replicate an array formula so Evaluate is definitely a better bet, but you need to construct the correct formula string for it. Unless you know whether the contents of the three cells will always be text or numbers, it is easier to just use cell addresses than to worry about enclosing the values in quotes:
Gorev = Cells(p, o).address
SlideNo = Cells(p, 34).address
Egitim_Adi = Cells(2, 3).address
Satir_bul = Worksheets(WS_All).Evaluate("=Match(" & Egitim_Adi & "&" & SlideNo & "&" & Gorev & ", '" & j_WS & "'!A:A&'" & j_WS & "'!B:B&'" & j_WS & "'!C:C, 0)")
If you can limit the ranges rather than using entire columns, you'll get better performance too.
Try it like this:
Satir_bul = WorksheetFunction.Match ( Egitim_Adi & " & " & SlideNo & " & " & Gorev & ", Check1 & Check2 & Check3, 0)
Here is a bit more about Match.
It will not work. You should include Union of the ranges in Match. Read more about Unions here.
You have three strings str1, str2, str3. You have three search areas: rng1, rng2 and rng3. Now, on every range you have to do the following (cell will be range variable):
For Each cell In rng1
'here you determine if one of your strings is contained uisng InStr function
'if the above condition is satisfied, then get the number of a row
Next cell
You'll do this then with rng2 and rng3.

Removing text and autosumming in VBA

I am attempting to sum up the numbers in a certain row by adding a formula in vba but It doesn't seem to like this Formula.
Each Cell Looks like this [ Vertical Lines are not included in cells ]:
30 (Gold) | 25 (Silver) | 20 (Gold) | 13 (Green)
So it needs to remove everything after "("
Worksheets("Season 2014-2015").Cells(lastSeasonRow, 14).Formula = "=SUM(LEFT(B" & lastSeasonRow & ":L" & lastSeasonRow & ",InStr(1, B" & lastSeasonRow & ":L" & lastSeasonRow & ","" (""))"
Try an array formula involving the SUMPRODUCT function using the IFERROR function to discard non-valid/blank entries,
With Worksheets("Season 2014-2015").Cells(lastSeasonRow, 14)
.FormulaArray = "=SUMPRODUCT(IFERROR(VALUE(LEFT(TRIM(B" & lastSeasonRow & ":L" & lastSeasonRow & "), FIND(CHAR(32), TRIM(B" & lastSeasonRow & ":L" & lastSeasonRow & "&CHAR(32))))), 0))"
End With
I used CHAR(32) (CHAR function) because I avoid dealing with quotes in quoted strings. The TRIM function can be left out if you do not have any leading spaces in the actual data.
    

VBA Excel formatting numbers to strings

In sheet 1 the value is 45 so I want in sheet RESULTADO the format like this: 0045
Worksheets("RESULTADO").Range("B" & row_counter) = Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
Why this doesn't work?
I've tried also this, but neither it works:
Worksheets("RESULTADO").Range("B" & row_counter) = CStr(Format(Worksheets(1).Range("B" & row_counter).Value, "0000"))
You can do that in two(2) ways:
Set the number format first
With Worksheets("RESULTADO").Range("B" & row_counter)
.NumberFormat = "#"
.Value = Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
End With
Use ' apostrophe
Worksheets("RESULTADO").Range("B" & row_counter) = "'" & _
Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
this doesn't work because in the first case you're trying to format a number n to 00n wich always return n, in the second case, you do exactly the same then you transtype the result to a string.
You have to convert n to a string first. So in your case :
Worksheets("RESULTADO").Range("B" & row_counter) = Format(CStr(Worksheets(1).Range("B" & row_counter).Value), "0000")

Applescript - Escaping Quotes When Setting Excel Formula

I've searched extensively for the answer to my question and I'm at an impasse.
I'm trying to pass a formula to excel with applescript. The problem is the quotes around the spaces that I want to place between fields. I've used "\" to escape the quotes but it throws an error. To make it more complicated the row number is a variable "i". Here is the formula in excel format:
=CONCATENATE(A2," ",B2," ",C2," ",D2)
Here is the formula in applescript-ese (works but does not produce spaces in data):
set rowCount to (((count of rows of used range of active sheet)) + 1)
repeat with i from rowCount to 2 by -1
set formula of row i of column 15 to "=CONCATENATE(A" & i & "," & ",B" & i & "," & ",C" & i & "," & ",D" & i & ")"
Here is the formula with escaped quotes to add the spaces (gives error):
set formula of row i of column 15 to"=CONCATENATE(A" & i & ","\" \" & ",B" & i & ","\" \" & ",C" & i & ","\" \" & ",D" & i & ")"
I get a sytax error on the first slash; Expected end of line etc. but found unknown token. I have a feeling I'm either missing a few sets of double quotes or I'm making this way more complicated than it needs to be. Any ideas? Should I be going about this differently?
Thanks in advance!
EDIT
Taking another look at it I think you have too many quotes when you use the \ character to escape the double quote. Instead of ","\" \" & ",B" try ",\" \",B" so your line would look like this:
set formula of row i of column 15 to "=CONCATENATE(A" & i & ",\" \",B" & i & ",\" \",C" & i & ",\" \",D" & i & ")"
In excel VBA you can double quote a double quote to get it to show up in a string like this:
"=CONCATENATE(A" & i & "," & """ """ & ",B" & i & "," & """ """ & ",C" & i & "," & """ """ & ",D" & i & ")"
Or you can try using ascii character for space which is Chr(32)

Resources