Named ranges won't work - vba

Whenever I try to make a named range in Excel I keep getting an error. I believe my formula is correct:
=OFFSET($B$2,0,0,COUNTA($B$2:$B$200),1)
However when I press OK I keep getting the dialog screen which states Excel found a problem with my formula.
Then it highlights the following part of the formula: $B$2,0,0,COUNTA.
I looked through various tutorials where this formula should be correct.
Can someone help me out on this?

One way to see this error message is if you are using the incorrect argument separators. For example, many locale's use the semicolon ;.
Try replacing the commas in your formula with semicolons
=OFFSET($B$2;0;0;COUNTA($B$2:$B$200);1)
or whatever your locale argument separator is.

Thanks, it were indeed the seperators which caused the problem: I have to use ; instead of a , - this tricked me as all of the tutorials I watched used the comma as well.

Related

How do I apply bullets to a table column in word using VBA?

I have applied bullets to a row without difficulty using
myDoc.Tables(1).Rows(3).Range.ListFormat.ApplyBulletDefault
but when I try something similar with
myDoc.Tables(1).Columns(4).Range.ListFormat.ApplyBulletDefault
I get the error method or data member not found with "Range" highlighted in vba.
I'm sure I'm missing something straightforward, but any suggestions would be great.
Okay, I've settled on two lines of code as follows in case this come up for anyone else:
myDoc.Tables(1).Columns(4).Select
myDoc.ActiveWindow.Selection.Range.ListFormat.ApplyBulletDefault

Why does Excel stop a macro when using Application.Evaluate?

I'm trying to use the Application.Evaluate function to test if a conditional formatting condition is true. However, what is happening is that the macro just stops - no error message, and the cell in which the UDF is referenced returns #VALUE.
The value of the conditional formula Formula1 property in this instance is "=A1<>VLOOKUP($A1,actWOrders1!$A:$EF,COLUMN(A1),FALSE)"
I've tried replacing Application.Evaluate with ActiveWorksheet.Evaluate, in case it is the Application form is struggling with the context, but the same happens.
Any ideas what might be causing the issue?
Thanks! Screwtape.
If your macro doesn't contain any on error handing statements it would certainly provide you with an error message if at any point it was unable to execute. It could be that your macro is executing completely however is not achieving the result you expect.
Try running it step by step using F8 and observing what is happening on each line to find the culprit, as you step through you can hover your cursor over a number of items such as variables to see what their value is. You might also find reading this webpage will help you to use the tools in VBA to debug your macros.
You created a circular reference.
If you try and type the formula into a cell Excel will tell you that has a circular reference.
You can also step through a formula in the formula bar.
Highlight an expression in the formula that you want the value of
Press [F9] to calculate the expression
The expression will be replaced with its value

vba write numeric data as text into excel

I am writing data into an excel spreadsheet. The data comes from an Access Database. Code like the line below works:
xl.cells(5,5)="joe"
but code like the line below does not:
xl.cells(5,5)="1/2CD438"
I get an error like "Application defined or object defined error"
I think Excel is trying to do a calculation, and the calculation fails. All I really want is the text string.
I've tried formating the column like
columns(1).NumberFormat="#"
, but it doesn't work either.
Any ideas?
Thanks for all the input. I had tried several of your ideas without success. As it turns out I was using variables to determine the exact row, column as:
xl.cells(NextRow, Col) = ars!PartNo
By simply replacing NextRow with an integer, the code worked. Replaced the integer in the test with NextRow, and the code ran. I suspect a misspelling or something, but couldn't see it looking at the code.
Thanks for the suggestions.

Range(...).Formula does not translate fully

I cannot figure this one out.
We use mostly french-version Excel (as we live in a french-speaking province of Canada). Somewhere in VBA code I set a cell's formula directly. Normally, we have to write the formula in english and Excel does the translation (writing the formula in any other language than english in VBA results in an error as far as I know). However, only HALF of this equation is translated which I think is causing me issues (writing the correct formula in another cell yields different results and most probably right results).
range("J2").Formula = "=round(IF(F2="",0,F2),2)-round(IF(G2="",0,G2),2)"
Is translated to this in the cell:
=ARRONDI(SI(F2=",0,F2),2)-round(IF(G2=",0,G2),2)
As you can see, the right part should read "ARRONDI(SI(.." but it does not read that way. I have tried adding spaces, removing the minus sign altogether, etc. Nothing works, it's always half translated. Any idea ?
In VBA you neexd to escape your quotations like this:
range("J2").Formula = "=round(IF(F2="""",0,F2),2)-round(IF(G2="""",0,G2),2)"
This is because the " Character is used in VBA as the start / end of a string. So if you want ot include it IN a string you need to type it twice in a row.

Write a formula in an Excel Cell using VBA

I'm trying to use VBA to write a formula into a cell in Excel.
My problem is that when I use a semicolon (;) in my formula, I get an error:
Run-time error 1004
My macro is the following :
Sub Jours_ouvres()
Dim Feuille_Document As String
Feuille_Document = "DOCUMENT"
Application.Worksheets(Feuille_Document).Range("F2").Formula = "=SUM(D2;E2)"
End Sub
You can try using FormulaLocal property instead of Formula. Then the semicolon should work.
The correct character to use in this case is a full colon (:), not a semicolon (;).
The correct character (comma or colon) depends on the purpose.
Comma (,) will sum only the two cells in question.
Colon (:) will sum all the cells within the range with corners defined by those two cells.
Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem.
But VBA speaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache.
You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).
I don't know why, but if you use
(...)Formula = "=SUM(D2,E2)"
(',' instead of ';'), it works.
If you step through your sub in the VB script editor (F8), you can add Range("F2").Formula to the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...