Range.Text VBA, set data into variable - vba

I'm trying to get formatted data from an excel cell. This cell can contains special symbols like &,',",<,>, LF (End of Line). All this text content will be set inside of a xml structure. For this reason I have to get all this text and parse all special symbols and transformed to a character entity
(& &apos; " <....)
To do this, first I have to load the data from cell in my code, it looks like:
Set beforeTransformation = Sheets(langId).Range("A46").Text
As official documentation says:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-text-property-excel
It's a read only property, so when I execute the VBA macro, next error is outputted,
Compile error: Object required
The question is, which is the best way to get data from a cell and set it into a variable for future transformation?
Thank you.

Related

Put text to clipboard reliably

I added VBA code to ThisOutlookSession to put a mail's subject line and the recipient's name into the mail body. This does not work reliably.
To be variable on where I paste the text, I use a data object of MSforms and it's PutInClipboard function to move the generated text to the Windows clipboard.
This works most of the time but sometimes the clipboard only contains two undefined characters. I am sure the text to be inserted was generated correctly.
// forClip is a string that contains a text line
Set MyData = New MSForms.DataObject
MyData.Clear
MyData.SetText forClip
MyData.PutInClipboard
// the code ended here but as described the clipboard then contains ��
// I added the following lines only to find out where it all goes wrong
MyData.GetFromClipboard
forClip = MyData.GetText(1)
// the string going back to "forClip" only contains the two characters mentioned.
Set MyData = Nothing
The code works most of the time - but if it fails, it will keep failing.
Could it be, that the clipboard interprets the text not as text but as something different? Is there a way to force it to interpret the data as text?

Error in formula with predefined variable

I want to add a formula for this range inside my macro and include inside it the variable percentage that I have set in the beginning of the macro. When I run it, it says there is an error "Application or object defined error" in this line. The entire macros runs perfectly, but today I want to add this line and it wont, so obviously the error is in the syntax of the formula. I am providing it below.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(1- & percentage),"""")"
Everything inside the quotes will appear as a literal string so you will get the word "percentage" appearing in your formula. The spreadsheet doesn't know what this is as you have defined it in your macro (there is no doubt a proper technical term for this).
Amended as per #Peh's suggestion.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(" & 1-percentage & "),"""")"

Excel VBA: How to search and copy a value of a certain pattern found within large texts in a column to another column?

As shown in the image above, I need to get the values following a pattern like for example TK AK15590 (TK AK followed by a series of numbers as shown) into cell B2. The same with the rest of the cells in the description column using vba macro in excel. Appreciate if anyone could give me a sample vba code or some information on how I could do this.
VBA is not requiered here. You should use formulas that manipulate text. In this particular case, the following would work:
=IFERROR(MID(A2,FIND("TK AK",A2),FIND(" ",A2,FIND("TK AK",A2)+4)-FIND("TK AK",A2)), "ERROR MESSAGE HERE")
When the length of the string you want to extract is fixed (10 in this case), you could simply use this:
=IFERROR(MID(A2,FIND("TK AK",A2),10), "ERROR MESSAGE HERE")

How to link files in excel with a text in the path changing dynamically?

I have a file which needs to fetch data accordingly every week.
Let us say the formula to get data in a cell is
=\T:\Datafile\weekdata\2015\Week01\[Summary.xlsx]Sales'!D4
I have a cell in the sheet C3 which changes every week accordingly.It will change to week02 next week and i wish the path to change too..to
=\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4"
I tried doing a concatenation to make the path dynamic
="\T:\Datafile\weekdata\2015\"&C3&"\[Summary.xlsx]Sales'!D4"
but it doesn't seem to work out.I checked evaluate formula and it resolves C3 to Week02 but the value doesn't come in the cell.In stead just the below text
\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4
appears in the cell instead of any number.
Let me know where am i going wrong and how to resolve it.
Try =INDIRECT():
=INDIRECT("\T:\Datafile\weekdata\2015\"&C3&"[Summary.xlsx]Sales'!D4")
This function does exactly what you are trying to do, takes a built string and makes it a cell reference.
The problem with =INDIRECT() is that it only works if the source workbook is open:
If the source workbook is not open, INDIRECT returns the #REF! error value.
(That quote is from https://support.office.com/en-sg/article/INDIRECT-function-21f8bcfc-b174-4a50-9dc6-4dfb5b3361cd)
By googling "excel convert text to formula" I found this SO Q&A: How to turn a string formula into a "real" formula
If you don't like the VBA solution, I had success with
iDevlop's =EVALUATE() solution.

Excel VBA Run-time error 1004 when inserting or value formula into cell

I got the run-time 1004 error when It try to insert a formula into a cell
Range("B64").Value = "=INDEX(AK7:AK123;G74;1)"
//I also tried
Range("B64").Formula = "=INDEX(AK7:AK123;G74;1)"
//And
Range("B64").FormulaR1C1 = "=INDEX(AK7:AK123;G74;1)"
But this gives the error. If I try inserting a number or a regular string like "test" it does work, but like this it doesn't. Im new to VBA and im wondering why this would give a problem since it would never in languages im used to.
Inserting a formula with VBA requires that you use EN-US standards like,
Range("B64").Formula = "=INDEX(AK7:AK123, G74, 1)"
... or use the regional formula attribute like,
Range("B64").FormulaLocal = "=INDEX(AK7:AK123; G74; 1)"
You may have to also change INDEX to the regional equivalent. The latter is necessary when you have a system with regional settings that do not use the EN-US standard of a comma for a list separator.
see Range.FormulaLocal Property (Excel) for more information.