This question already has an answer here:
Excel VBA - How to add dynamic array formula
(1 answer)
Closed 2 years ago.
I was playing around with Excel to try and help out a friend with a problem, and wrote the following macro
Sub test()
Worksheets("Sheet3").Cells(1, 4) = "=SUM(A1:A2)"
End Sub
The idea was to try and figure out if I could create a macro that writes formulas in a specific cell (which we know we could) but also be able to change the range of the formula.
For example, if instead of just 2 values to sum up, I had 4 values, we want the macro to the able to count all the values and then set a range for all the formulas.
The problem here is that when I write the piece of code shown above, for some reason in the cell it appears " =#SUM(A1:A2) ". I have no idea what the "#" symbol is supposed to do nor why it is showing up. As a result though, I get a "name?" error and the function doesn't work. However, if I manually delete the "#" symbol, it works perfectly.
Can anyone explain why the "#" symbol is showing up and how not make it show up ?
This should work :)
As the .Formula is made for excel to recognize that you want to print a formula.
Worksheets("Sheet3").Cells(1, 4).Formula = "=SUM(A1:A2)"
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have to paste data from several sources into Excel on daily basis. The usual way is that you paste the data, then start the text conversion wizzard (translated from german) and make several settings for the delimiter and for the columns. You have to do this, because Excel assumes that 3.4 has to be a date-Format or anything starting with + must be a formula. But in most cases it is not! So I have to pick the columns and set the format from standard to text. Additional, in Germany we do not write 1,223,443.44€ but we write 1.223.443,44€, so each time I have to make this setting also in the wizzard.
So: I want this done with VBA. What I have tried so far:
Somehow execute the text conversion wizzard via VBA. But the macro recorder does not record it properly.
Selection.TextToColumns: Works fine but the pasted text mus be in one column. When text ist for example tab-delimited, Excel automatically puts it in many columns.
Insert in many columns and then by vba collect into one column to use (2). Does not work because at this point the data is already damaged.
The desired behavior is that i can specify the delimiter already before pasting the data in a user friendly dialog, and then paste all data without any poor attempts of excel to interpret it as numbers, formulas, date/time or anything else.
Furthermore I want it to be able to choose number format as 1.000,0 or 1,000.0 in a user friendly dialog, but htis part can also be done after pastinng the data, as long as excel does not already destroy the data with its format assumptions.
Example Data to paste can be:
Part_Number Version Quantity
+213324443 2.3 1,000.00
AZ38643892 1.0 71.11
11.1.89 7.2 0.03
which shows that the + would be interpreted as formula, the version probably as a date and the Quantity at least in german excel would not be the correct numbers.
You can read the content directly from the clipboard using DataObject, but wou would need to figure out exactly what format you wanted for each "column" in that data, and it would also require you to be able to specify the delimiter.
Here's a super-basic example:
Sub tester()
Dim MyData As New DataObject, arrLines, arrVals, txt, ln, v, c As Range
Set c = ActiveCell
'get the content from the clipboard
MyData.GetFromClipboard
txt = MyData.GetText
arrLines = Split(txt, vbCrLf) 'split to lines
For ln = 0 To UBound(arrLines) 'loop array of lines
arrVals = Split(arrLines(ln), vbTab) 'split to values
For v = 0 To UBound(arrVals) 'loop all values in this line
With c.Offset(ln, v)
.NumberFormat = "#" 'eg. setting all value cells to "Text" format
.Value = arrVals(v) 'populate the value
End With
Next v
Next ln
End Sub
This question already has an answer here:
Excel VBA Formula German/French/Italian/Russian/Dutch/Foreign Function
(1 answer)
Closed 3 years ago.
I'm making a bot that erases certain columns and fills them in with new data. By erasing the data, the formula becomes
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!#REF!,'AP query'!L:L,0)),"Non PO")
So I tried making a macro and filling the cell with the formula but it says there's an error. Is there a fix or another way to enter the formula into the the cell using macros?
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),"Non PO")"
srcWorkbook.Worksheets("Findings").Range("B3").Copy
srcWorkbook.Worksheets("Findings").Range("B3:B" & LR).PasteSpecial xlPasteFormulas
When you wish to use a string in a formula =IF(A1="Hi",... in VBA, just "double up" the quotes:
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),""Non PO"")"
Because, as VBA compiles that line, it'll hit the first quote at "Non Po" and think that's the end of the line. So (...trying to visualize it), it'd be reading that line like your formula is:
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),
Tangiental tip - sometimes when I hit similar issues, even just pasting the code here on SO, and formatting it as code, helps because as you can see in your post, the Non PO is a different color, but all formulas (strings maybe?) should be the same (all red, as in the answer I posted above).
This question already has answers here:
Pulling data from big excel datatable with incremental column in Vlookup or IndexMatch without zeros
(2 answers)
Closed 6 years ago.
I'm not using a numerical formula. All I have is a simple reference to another cell.
=(Sheet1!D8)
If that cell is blank, I want to have the cell on this page blank too, not a 0. How do I do that? I want it to appear empty to anyone, not just me, so I don't think simply editing excel settings would work? None of my google search results, including MS office site, have been helpful.
My suggestion:
=IF(ISBLANK(Sheet1!D8),"",Sheet1!D8)
You can force a formula to evaluate as text instead of as a number by adding '& ""' to the end like below, but if you are dealing with numbers this isn't great.
=(Sheet1!D8)&""
See this answer for other ideas.
I wrote this VBA macro for Excel code many years ago and it has suddenly stopped working. I'm trying to find the row index in one sheet that exactly matches an entry in the current sheet. The line of code that has stopped working is:
Cells(rr, cc + 1).Value = "=MATCH(RC[-1], Num, 0)"
I'm not sure what Num is, since it isn't referenced anywhere in the macro earlier. In particular, I don't see how it references the worksheet i'm interested in.
If you were trying to put a formula into the cell defined by row:=rr, column:=cc+1 then the code really should be,
Cells(rr, cc + 1).FORMULAR1C1 = "=MATCH(RC[-1], Num, 0)"
(sorry - all caps for emphasis) That formula would make use of a Named Range called Num. Check Formulas ► Defined Names ► Name Manager for its existence. It will refer to a single row or column of cells where the lookup for the value of one cell to the left is performed.
If you try and put an xlR1C1 style formula in as the .Value, it may be correctly interpreted by the worksheet overhead as a formula. However, without a defined Num range, this would produce the #NAME? error on the worksheet.
The problem turned out to be a bad data entry in the worksheet I was trying to MATCH to. I don't know why it didn't just return a FALSE value. However, I have solved my problem thanks to the help from this site.
Thanks.
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Last not empty cell (column) in the given row; Excel VBA
(2 answers)
Closed 8 years ago.
I have a range of data that will be between columns B and D inclusive.
I can get the top left most range as this will be constant, the only thing that varies is the bottom right cell.
I tried obtaining the bottom right cell via using xltoright and then xltobottom. However I am having difficulties obtaining the cell address of this bottom right cell.
Has anyone come across this problem and if so how did you over come the issue?
change xltoright and then xltobottom, to : xltoleft and then xlUp.
Check the answers you get from it in the immédiate window of VB editor (Ctrl+G)
if still doesn't work maybe use:
Range(Range_Adddress).cells.(Range(RangeAddress).cells.count).address
to get to the last cell's address of your range (I named it Range_Address, which has to be a string in the example).
Does this help you?