Removing a Line Feed substring without using vba - excel

I am using a Vlookup to take the content of a cell in another sheet. The problem is that this cell has 2 lines, written by VBA, seperated by chr(10) or vbLF (equivalent of alt+enter to switch lines in a cell). I only want the first line. This is what I have right now (RechercheV is Vlookup in French):
=RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;3;FAUX)
It returns "Transfert1Responsable:IC" (in the target cell, Transfert1 is on the first line, and Responsable:IC is on the next). I want to break at the line change and only retain "Transfert1".
Thanks a lot!

Turns out a line feed is called CHAR(10) in English and CAR(10) in French. So using this: http://www.gcflearnfree.org/excel-tips/how-to-use-excels-vlookup-function
I changed my formula to this:
= GAUCHE(RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;3;FAUX); TROUVE(CAR(10); RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;3;FAUX))-1)
And it worked perfectly!
Source for Char(10): http://blog.contextures.com/archives/2011/08/08/add-line-break-in-excel-formula/
EDIT: The whole goal of this was to add an IF statement that would change the text based on the language as chosen by the user. The language is chosen from a validation list in another sheet (French of English) and the VlookUp looks at column 3 for French version, and column 4 for English version. After adding the IF statement I ended up with this monster (This is aside the question, I'm posting in case anyone want to go further with their VlookUp functions):
=SI(Paramètres!cell_langue="FR"; GAUCHE(RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;3;FAUX); TROUVE(CAR(10); RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;3;FAUX))-1); GAUCHE(RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;4;FAUX); TROUVE(CAR(10); RECHERCHEV("btn_" & etape_doc;LangueEtapes!A:D;4;FAUX))-1))

Related

How do i remove blank lines from code filled list

I have a problem and have modified “jlore” code of =if(d3 = "PS588", c3, "")
which works, but is there a way to populate so I don't get blank lines? My task is to generate an Excel HV switching program on one sheet then on the next an isolation list. I am attempting to auto populate the Isolation list with the code =IF(HVSP!H30 = "APPLY ISOL LOCK & TAG", HVSP!E30, "").
However as stated if the "APPLY ISOL LOCK &TAG" isn't in the cell my isolation list creates a blank line.
Regards
Steve Mack
If i have understood your question correctly, you are getting a blank cell because of the ,"") at the end. If you change this to ,"DON'T APPLY ISOL LOCK & TAG") or wording of your choice you will not get a blank cell.
Hope this helps.

How do I reference an Excel ListObject table column if the column name has line breaks?

I'm tasked with the following:
several sheets from different workbooks have to be copied to a new workbook
each of those sheets contains an Excel table (a ListObject), named like the sheet with a postfixed T (sheet Foo, table: FooT)
the new workbook has to contain a summary sheet where each table name is listed, and various values of the respective tables are presented by referencing them with suitable formulas
This has to be done frequently for different workbooks, so the idea was to do this in VBA.
Copying the sheets is easy, listing the table names in a new sheet is easy, but referencing the values runs into problems.
The general idea is to do the following
ActiveSheet.Range("A1").Value = "FooT"
ActiveSheet.Range("B1").Formula = "=FooT[[#Totals],[Quantity]]"
ActiveSheet.Range("C1").Formula = "=FooT[[#Totals],[Total List Price]]"
and iterate over all sheets.
Setting the value for A and the formula for B works as expected and gets the expected results.
The issue with C is that instead of "Total List Price", the column header is actually formatted as
"Total
List
Price"
I can't change that, this has been a design decision.
This is also how the column name shows up in the formula if I add the formula to the cell manually.
So there's some sort of line break happening here, and I've tried cater to this in VBA with
ActiveSheet.Range("C1").Formula = "=FooT[[#Totals],[Total" & vbCrLf & _
"List" & vbCrLf & _
"Price]]"
and vb_Cr and vb_Lf and vb_Newline instead of the vbCrLf. Trying to set C's formula to any of these variations yields the infamous Error 1004.
Getting the value of the column header from one of the sheets and using it in the formula works. This could be a potential workaround, but I'd really like to know what I'm missing or how I can figure out how to build this formula string correctly.
Your formula is OK but typically, the newline character will be vbLf if the title was set from the keyboard. I also suspect there might be leading and/or trailing space characters anywhere in the title. Select your title cell and, from the VBE's Immediate Window (Ctrl+G), type Debug.Print ActiveCell.Value, then check where each printed line ends.
Are you using Option Explicit? In your question, you mention you've tried vb_Lf but this constant doesn't exist and, without Option Explicit, would have been interpreted as an empty string.
When you enter a line break in a cell, the text is actually continuous to the previous line, there being no separator character, unless you enter a space before the break. To name the column you must write it without a space between the last word of the previous line and the first of the next, like this: [TotalListPrice]

.FormulaR1C1 containing variables with values read from files

The code lines
.FormulaR1C1 = "=IF(R1C1="&Chr(34)&text&Chr(34)&",1,0)"
.FormulaR1C1 = "=IF(R[-3]C[2]="&Chr(34)&text&Chr(34)&",1,0)"
are examples of the usual manner to insert R1C1 formulas in target cells when the addresses of the target cells are already known. No problem with that.
BUT...I can't find the way to devise a general R1C1 formula that can be used to do the job when the addresses of the target cells aren't previously known, but they are read from different text files instead, and both, rows and columns, may vary from one occasion to the next, getting inserted by means of loops. The following code lines can give an idea of what I'd like to have in a text file to be read, inserted in a cell and work properly:
"=IF(R[-"&varRow&"]C["&varCol&"]="&Chr(34)&text&Chr(34)&",1,0)"
"=IF(R"&varRow&"C"&varCol&"="&Chr(34)&text&Chr(34)&",1,0)"
In other words, I can't figure out how to use variables for the numbers of rows and columns in the examples given above. I asume such a possibility exists, but I have ran out of ideas on how to implement it. I've implemented some, but I've just gotten the code to be inserted as the value for the cells or the #NOMBRE(#NAME) error.
What is a solution to this problem?
I am not able to "replay" your issue...or I understood it wrong.
If I use this code:
Range("C2").Select
Dim varRow As String
Dim varCol As String
varCol = 2
varRow = 2
Range("A1").FormulaR1C1 = "=IF(R[" & varRow & "]C[" & varCol & "]=" & Chr(34) & Chr(34) & ",1,0)"
The right formula appears in cell A1 and does its job.
To write this in a file, just put the formula string as it is in a string variable and write it to a file.
Scott, SJR, and anyone reading this post.
Apologies in advance for the length of this post, but I think that the matter deserves it.
I have managed to solve the situation regarding the FormulaR1C1 format when it is to be loaded from a text file. However, I'm not totally sure of the why and the how involved in the analysis and the solution. Maybe you can give a thorough explanation about it.
The fact is that the solution (formula format) you have given, quite similar to the one given by Microsoft and similar to the result obtained when recording the macro in entering the procedure by hand, when entered as plain text in a text file and then read from it and placed in a cell just doesn't give the result expected. An example of the original data in a text file:
22_2_|"=IF(R[" & varRow & "]C[" & varCol & "]=" & Chr(34) & varValue & Chr(34) & ",1,0)"
"22" would be the row number (varRow) and "2" the column number (varCol).
I tried every different logical possibility that came to my mind over and over, all of them with similar results: failure. Then I decided to forget logic and try random thoughts.
First thing I did on this new trail was to revise the original idea: use a general formula exclusively with variables in it. I realized that I was making a stupid mistake giving the value for the variable -for instance, the column and row number- and right next to it placing the variable in the formula at the same time. Why to do it that way? Why not to place the numbers for the row and column directly in the formula? After all, the formula should work the same all the way.
An example for this new approach could be:
"=IF(R[number]C[number]=" & Chr(34) & varValue & Chr(34) & ",1,0)"
I did it this way and tested it, with dissapointing results, similar to the ones obtained at first.
Then I thought about the quotes used for the formula format. Could it be possible that the reason for the problem might have to do with them? Well, there was only one way to find out. So I tried it this way, then this other way, an other..and other...and then...EUREKA!...This is what I got, that finally works:
=IF(R22C2="value",1,0)
As you may notice, the final formula ends up having no variables at all. That was a stupid idea on my side, considering that the data is going to be obtained from a text file. So, forget the variables. BUT...and this is a very important "BUT"...
Notice what has happened to the quotes. The only ones remaning are the ones that enclose the value that is to be compared. All the others have dissapeared. And this is the only way in which the formula, when read from the text file and inserted in a cell, ends up as a functional formula in the cell, and not as the value for the cell or as a "#NAME?" error. And now, a very important question: WHY? My answer for it: I have no idea.
I must suppose -and that's the most I can do at this moment- that the fact the formula is being read from a text file and inserted as text in the cell is handled by Excel in a "certain way" that already includes -behind curtains- the necessary quotes in the internal works of the worksheet. So, if you previously include some quotes, the formula ends up having an excess of them and the final product is not the one expected. Might that be it? Who can tell? Well, what matters to me is that, as strange as it may look, it now works as I wanted it. Give it a try yourself, and then tell me.
Well, pals, that's it. And thanks a lot to all for your kind and timely help.

Concatenate function in Excel 2010 to add single quotes and commas

I am working on a loaner laptop and have found that the concatenate function I have used to add single quotes and a comma to a column of text is no longer working. I need this to bring a series of IDs into a SQL query. The function I have used for years is =concatenate("'",A1,"',") to get a result of 'A1',. This is in Microsoft Excel 2010.
Try the following as an alternative to =concatenate():
="'"&A1&"',"
I've been using the above to do exactly what you're trying to accomplish.
Follow the below steps.
1- select all the rows.
2- right click and select format cells
3- select number tab
4- select custom in the left category tab and
5- under the type enter '#' (if column has hello after applying format it will turn into 'hello')
Use Excel Characters.
In your case it would be :
= concatenate(CHAR(39),A1,CHAR(39))
To get the result = 'A1'
similarly, you can include other special charterers as well. Like to include space you need to use CHAR(32).
Sadly textjoin() function is not in Excel 2010 but if someone is wondering how to concatenate several columns, wrapping the values in a single quote in the latest Excel versions just run this:
=TEXTJOIN(delimeter, ignore empty cells (true|false), usual range of cells)
I used something like this to create a sql script to insert values in a table:
="INSERT INTO my_table values ('" & TEXTJOIN("','", False, A2:Z2) & "')"
check the official docs for further details.
To enclose all specified cells in quotes, the following simple formulas may help you.
Method 1:
Use below formula into the blank cell:
=CHAR(34) & A1 & CHAR(34)
Considering the value that you want to enclose in quotes is present in A1 cell
Method 2:
To insert single quotes around the cell values, use this formula:
="'" & A1 & "'"
Simply put single quote inside double quotes. "'"

Subbing literal to Row() breaks INDIRECT()?

My document is much like a book where each Sheet is like a page, in fact like a chapter.
At the beginning of the doc I have a page that's similar to a table of contents. So beginning let's say at E10 I have
Topic: Page:
Apple 7
Banana 3
Cherry 2
Each topic refers to a sheet which has the exact same name, and each such page has its Page # stored in the Cell B2. To support automatic self-numbering regardless of sheet reordering, in the above table I have formulae calculate the page #, so where it says "7", the formula is
=INDIRECT($E$10 & ".B2")
So $E$10 gives me "Apple", then I get "Apple.B2" which returns the 7.
But now for Banana, the formula is
=INDIRECT($E$11 & ".B2")
I have to change the 10 to an 11. I want something like this, the same formula for every row:
=INDIRECT( "$E$" & ROW() & ".B2")
I've tried a billion things, but I just can't get it. What's weird is that I can generate "Apple.B2" just fine, but when I pass it to INDIRECT() it breaks. Thx.
You are actually checking two different cells, so each one needs INDIRECT:
=INDIRECT( INDIRECT("$E$" & ROW()) & ".B2")

Resources