Pivot Tables - VBA - excel

I want to select some values through VBA in Pivot Table which is linked to OLAP Cube.
As I know such modification can be realised by typing:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = Array("value1","value2","value3")
Since get list of parameters from cells in Excel sheet, I wrote simple function which - In mentioned example - returns:
""value1","value2","value3""
I can't use such string as parameter for Array function (as it recognize it as one string), so I've tried to convert it to Array of Variant, typing above code:
Dim tableVar() As Variant
myVar = Replace(myVar, Chr(34), "")
myVar = Split(myVar, ",")
lowerB =LBound(myVar)
upperB = UBound(myVar)
ReDim tablica(lowerB To upperB)
For i = lowerB To upperB
tableVar(i) = myVar(i)
Next i
Unfortunately it changes nothing - when I'm calling:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = tableVar
I'm still receiving an error message.
Could you help me, please?

you have a typo in your code, daty should say myVar.
(Either that or we're missing more details)

Stupid thing, but error message is simply correct - there's no such items in Cube:
Run-time error '1004': The item could not be found in the OLAP Cube
I gave incorrect parameter here:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = tableVar
My code was unnecessary complicated - sorry for wasting your time.
Now my problem will be - how to check if specific dimensions or whole Cube exist...
Thanks once more for help.

Related

excel vba listobject HeaderRowsRange

I am working with list objects in excel and there is one thing that puzzles me:
according to this and many other sites I visited the following line of code is a range:
mytable.headerRowRange("nameofColumn")
mytable being a listobject of a particular sheet.
what I wand to do is hide that column
but this would not work:
mytable.headerRowRange("nameofColumn").EntireColumn.Hidden=True
Why?
the error is: Invalid procedure call or argument.
thanks.
mytable.ListColumns("ID").Range.EntireColumn.Hidden = True
You could also have done
mytable.HeaderRowRange.Find("id").EntireColumn.Hidden = True

VBScript - Either getting object required or type mismatch errors

I have scoured the web and this site looking for an answer on this, so I would really appreciate some help.
I'm creating a VBScript to do some modifications to a user-specified Excel spreadsheet. I have the first part of my script working fine, but the second part is driving me nuts. I need it to search the first column for a value and, if found, delete the row. Right now I'm not worrying about the deletion statement--I'm doing testing by seeing if I can get the For Each statement to run properly as well as the If Then statement. Here's the specific block of code:
For Each cell in objSheet.Columns("A:A").Cells
Set cell = objSheet.Columns("A:A").Cells
If cell.Value = "60802400040000" then
cell.font.bold = True
End If
Next
I have tried many variations of this and cannot find the right combination. Initially I was getting an "Object Required" messages, and after reading a number of posts, found that I needed to put in a Set statement for cell, which I did. Now I am getting a Mismatch Type error message.
The funny thing is, before I put in the Set statement, the code would execute, but it would throw the Object Required error when I closed the spreadsheet. After adding it, the error for the Type Mismatch pops up immediately.
Most examples I keep finding on the web are for VBA, and I try to modify them for VBS, which I don't know very well. Any assistance anyone can give me will be greatly appreciated.
You are redefining cell, cell is defined automatically in the For Each statement.
Delete this line
Set cell = objSheet.Columns("A:A").Cells
This is an example from Help, unfortunately Help doesn't have any examples that uses For Each, only For x = n to n and other means. For Each is the right thing to do.
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
For vba to vbs, you have to create the object and use, as some objects are automatically available in VBA (like app object) - Set exceldoc = CreateObject("c:\blah\blah.xls) then to use Set r = exceldoc.worksheets(0).range("MyRange").
Also you have to use constant values not names as vbscript can't look them up.

populate combobox in VBA with array elements

I have a VBA procedure (in Excel 2007) where I aspire to set the ListFillRange property of a combobox styled as a list using an array.
I know this works if I right click the combobox and write "Sheet1!$F2:$F17" next to the "ListFillRange" property. I can also do this in code. However, I am interested in dynamically setting the value of this property by assigning it an array.
I know for sure the array works as I tested it; there is probably a syntax error here:
ThisWorkbook.Worksheets("Sheet1").OLEObjects("cmbS").ListFillRange = ar
when I do this I get:
"Type mismatch" error.
The result of this action should be that the component is populated with the array elements, from element(0) ... to the last element (n-1) of the array. Any pointers, thank you very much!
I also tried:
ThisWorkbook.Worksheets("Sheet1").cmbS.list = ar
and it says "permission denied"
Here are the combobox properties in case it helps:
After testing and trying, I found this works:
ThisWorkbook.Worksheets("Sheet1").cmbS.ListFillRange = ""
Dim i As Integer
For i = LBound(ar) To UBound(ar)
ThisWorkbook.Worksheets("Sheet1").cmbS.AddItem (ar(i))
Next
However, I am interested in populating with all values at once for faster effect, not just adding element by element.
I know its late but maybe it is going to help someone else. At least the following code works (much faster than element for element) for me.
dim arr() as variant
arr = Worksheets("Total").Range("C2:"&lrow).Value
Worksheets("Menu").ComboBox2.List = arr
The only way you can populate a combobox with the content of an array is by doing it element by element. I find it hard to believe that it would be a notably slow process no matter how large your array is.

Excel error 1004 "Unable to get .... property of WorksheetFunction class" appearing inconsistently

I have a VBA function within a spreadsheet which operates on another spreadsheet that is opened in an earlier stage of my macro. The macro used to work fine but just recently has started causing a 1004 error ("Unable to get RoundDown property of the WorksheetFunction class") when it runs.
I believe I understand what the error would be caused by (a problem running RoundDown) but I cannot see why it is getting triggered in my macro and the odd part is that when I go into Debug mode and step through the code in the VBE the error does not recur (despite nothing obviously changing).
Does anyone have a similar experience of this sort of error occuring inconsistently and know what I could do to resolve it?
I'm reasonably VBA/Excel-savvy, but any suggestions on further steps to diagnose it would be appreciated. I am wondering if there is some issue with the opened spreadsheet not being ready but I cannot see how.
The code is here. The error occurs on the line marked with a comment.
Public Function GetDatesA(sWorkbookname As String, sSheetname As String, sCell As String) As Variant
Dim vDateList() As Variant
Dim currentCell As Range
Dim n As Long
Set currentCell = Workbooks(sWorkbookname).Worksheets(sSheetname).Range(sCell)
n = 0
Do
If Trim(currentCell.Value) = "" Then
Exit Do
Else
ReDim Preserve vDateList(0 To 1, 0 To n)
vDateList(0, n) = WorksheetFunction.RoundDown(currentCell.Value, 0) 'error occcurs on this line
vDateList(1, n) = currentCell.Column
'Debug.Print currentCell.Value
End If
Set currentCell = currentCell.Offset(0, 1)
n = n + 1
Loop While currentCell.Column < XL_LAST_COLUMN
GetDatesA = vDateList
End Function
Other details are:
Excel version: 2010
File being opened resides locally on my C: drive; my macro is in a spreadsheet on the network
File format for both files is .xls (i.e. Excel 2003) - I don't have the option of changing this
Windows 7 (not that I think it would be relevant)
Two points I've tried already are:
Substitute a different worksheet function (e.g. Min(currentCell)) and that also causes the same problem
Having the file open already seems to stop the problem - I wonder if there is some way that the workbook which is being opened (rather than my main workbook with the macro in it) is not enabled for macros and this is interfering. But even if this is the cause I'm not sure how to get around it!
Any ideas?
This error occurs often when any argument passed to the worksheet function is not of the correct type or simply doesn't make sense.
For example, I've had this problem when calling WorksheetFunction.Asin with an argument bigger than 1. In your case, I'd guess currentCell.Value is a non-numeric value or one not according to your region settings regarding numbers.
Yes, the error message is really misguiding.
I got the "Unable to get * property of WorksheetFunction Class" error using Transpose, MMult,MDterm, and MInverse functions.
I was able to get my code to run by putting "Option Base 1" in the Declarations (before the actual code) section of the particular Module in the Editer.
Excel assumes "Option Base 0" which will add an extra row and column of empty cells. This will cause the error to occur and isn't immediately obvious to see.
I have come accross this before, and for me it was becase the criteria range made no sense, as Andre said above.
See example formula below:
.Cells(11, i).Formula = Application.WorksheetFunction.CountIfs(Sheets("Sheet1").Range("AC8:C" & n), "S")
Have a look at the Range... it makes no sense. Amended the range from "AC8:C" to "AC8:AC" and it will work perfectly

How to use Excel VBA to extract Memo field from Access Database?

I have an Excel spreadsheet. I am connecting to an Access database via ODBC. Something along then lines of:
Set dbEng = CreateObject("DAO.DBEngine.40")
Set oWspc = dbEng.CreateWorkspace("ODBCWspc", "", "", dbUseODBC)
Set oConn = oWspc.OpenConnection("Connection", , True, "ODBC;DSN=CLIENTDB;")
Then I use a query and fetch a result set to get some table data.
Set oQuery = oConn.CreateQueryDef("tmpQuery")
oQuery.Sql = "SELECT idField, memoField FROM myTable"
Set oRs = oQuery.OpenRecordset
The problem now arises. My field is a dbMemo because the maximum content length is up to a few hundred chars. It's not that long, and in fact the value I'm reading is only a dozen characters. But Excel just doesn't seem able to handle the Memo field content at all. My code...
ActiveCell = oRs.Fields("memoField")
...gives error Run-time error '3146': ODBC--call failed.
Any suggestions? Can Excel VBA actually get at memo field data? Or is it just completely impossible. I get exactly the same error from GetChunk as well.
ActiveCell = oRs.Fields("memoField").GetChunk(0, 2)
...also gives error Run-time error '3146': ODBC--call failed.
Converting to a text field makes everything work fine. However some data is truncated to 255 characters of course, which means that isn't a workable solution.
Try Range.CopyFromRecordset to see if it works.
Try using CStr(oRs.Fields("memoField")) and assign to Value2 of the Range/ActiveCell.
Try making the memo field the last physical column in table. A memo field is read only when retrieved. There was/may-still-be an issue with memo fields that are not at physically at the end of a table.
All I can think of right now.

Resources