object defined error with assigning named range to array - excel

i am trying t assign the values of a named range to an array of variants. I get an application or object defined error almost at the level of assignment code
Material = ThisWorkbook.Names("RMInStoreName").RefersToRange
i got this example from here and have used it before sucessfully.
I am still trying to figure out why this error is showing up, i seem to have run out of ideas, anybody can help me point in the right direction will realy save me alot
The code is below
Here is the code
Sub MonitorStore()
Dim ThreshHold As Variant, InStore As Variant, StatusReport As Variant
Dim Material As Variant 'is the name of the material
Status As Variant
'status is a variable which holds data on wether the user has seen msg and
'wants to supress msg
'the ThreshHold is the minimum allowed in store below which messages are firerd
'InStore is the volume of materials currently in store
'and be told of another error after solving error one, report all at once
Material = ThisWorkbook.Names("RMInStoreName").RefersToRange
ThreshHold = ThisWorkbook.Names("RMThreshHold").RefersToRange
InStore = ThisWorkbook.Names("RMInStore").RefersToRange
Status = ThisWorkbook.Names("RMStatus").RefersToRange
'other code.............
'dont expect error from unexecuted code
End Sub
Thanks for help
Stephen

The most likely reason is that you don't have all of the named ranges defined in the workbook.
You can verify the named ranges using the Formula tab.
Formula --> Name Manager

RefersToRange will return a range object. Its the value you are after then:
Material = ThisWorkbook.Names("RMInStoreName").RefersToRange.Value
or you can use:
Material = Evaluate("RMInStoreName")
or:
Material = Evaluate(ThisWorkbook.Names("RMInStoreName").RefersTo).Value

Related

How can I use cell value in declaration of new class object?

I have a fairly simple question to which I can't seem to find the answer anywhere so I'll try here. I have created a new class called "Groups" in vba. What I want to do is retrieve a value from a cell in excel and use this as the name of the new group I want to create. I've tried accessing the value through Range("B20").Value but then I need to store it in another variable first and if I do that I can't use it as the name after since it thinks I want the group to be named like my variable I stored it in.
Does anyone know a way around this? Appreciate any help or suggestions.
Below is what I've tried which doesn't work.
Dim Range("B20").Value As Group
I've also tried this but then it thinks I want to name the group as my variable "getName"
Dim getName As String
getName = Range("B20").Value
Dim getNAme As Group
Kind regards,
Jahlove
As others have suggested in the comments, you cannot change a string variable into a variable name. However, if all you want to access/reference an object using a string, you can try a Scripting.Dictionary (docs here) like this:
Sub DynamicGroupNaming()
Dim sGroupName As String
Dim dictGroups As Object
Set dictGroups = CreateObject("Scripting.Dictionary")
'Change Sheet1 to the name of your worksheet
sGroupName = ThisWorkbook.Sheets("Sheet1").Range("B20").Value
' Add a new group to the dictionary
dictGroups.Add sGroupName, New Group
' Access the group's properties/methods in the usual way
dictGroups(sGroupName).Name = sGroupName
MsgBox dictGroups(sGroupName).Name
Set dictGroups = Nothing
End Sub

VBA Populate Different Objects Using the Value from a Sub Argument

Good Afternoon All,
Let me preface this post by saying I have very little experience using VBA, but it is the tool I have to work with in this instance, so here I am. I am using the SAS Add-In for Microsoft Office, which isn't entirely relevant in this situation as far as I can tell, but it is best to give you some context. I have 2 subs that function very differently but make use of the same named ranges.
The first sub uses values from 4 cells within Excel and submits them to a stored process which returns values to particular cells which are defined as named ranges - approximately 64. Once that takes place the end-user will validate the results, make some changes to the values in the 64 cells and then submit the second sub. The second sub then passes the values contained within the 64 cells for processing by a second stored process.
It makes more sense to me to have the 64 variables defined once and not multiple times to save on maintenance, but they are applied to different objects for example:
Sub1
Dim outputParams As SASRanges
Set outputParams = New SASRanges
Dim DD_BD_Age As Range
Set DD_BD_Age = Sheet1.Range("DD_BD_Age")
outputParams.Add "DD_BD_AGE", DD_BD_Age
Sub2
Dim prompts As SASPrompts
Set prompts = sas.CreateSASPromptsObject
Dim DD_BD_Age As Range
Set DD_BD_Age = Sheet1.Range("DD_BD_Age")
prompts.Add "DD_BD_AGE", DD_BD_Age
Is there any way that I can define these variables for use across either sub. If I was using SAS I would create a macro with a parameter which would allow me to specify the value of outputParams or prompts depending on the context in which I was using them.
Admittedly, with my limited experience with VBA, I may just be making things more difficult than they need to be, so please let me know if this is the case.
Any help would be greatly appreciated.
If your SAS objects have a common "Add" method then you can do something like this:
Dim outputParams As SASRanges
Set outputParams = New SASRanges
AddParams outputParams
Dim prompts As SASPrompts
Set prompts = sas.CreateSASPromptsObject
AddParams prompts
'add common parameters
Sub AddParams(obj As Object)
With obj
.Add "DD_BD_AGE", Sheet1.Range("DD_BD_Age")
'etc for the rest
End With
End Sub

Object Required error when I clearly have an object

Have searched and have come up short on any solutions to this. I am relatively new to VB for the record. The variable minDate here is declared in the module outside of the procedure. I have tried declaring it inside the procedure, using set, let, and passing the argument as a range variable. Nothing.
Sub SocialTimeSinceFirstComment()
'
' SocialTimeSinceFirstComment Macro
'
Range("A11").Select
ActiveCell.FormulaR1C1 = "=MIN(SocialTransform!C[4])"
minDate = Application.WorksheetFunction.Min(Workbook.SocialTransform!.Range("c4").End(xlDown))
Apparently you have a worsheet named SocialTransform so use this:
Dim ws as Worksheet
set ws = ThisWorkbook.Worksheets("SocialTransform")
Dim minRange as Range
set minRange = ws.range(ws.Range("c4"), ws.Range("c4").End(xlDown))
minRange.Select 'use this when testing so you can see exactly what is included
minDate = Application.WorksheetFunction.Min(minRange)
Hopefully you have minDate declared as a date Dim MinDate As Date otherwise you'll just get a number; which could of course be transformed back to the corresponding date.
Also I might explain that the error you got was not because of minDate but because SocialTransform! is not an object of Workbook. You need to use Worksheets("SocialTransform")
EDIT: Actually you can use the code name to reference a sheet within the workbook like this: Debug.Print Sheet1.Name
In this example the user renamed the first sheet to "Data" and the Sheet1.Name will return "Data". The only way to change the code name is by change the "(Name)" property in the VBA editor window.
See: Worksheet.CodeName Property (Excel)
The error message is clear, and you don't "clearly have an object". (The compiler is almost always more aware of the code and syntax than we are, so if it says something is wrong you should probably believe it until you can prove otherwise.)
Workbook.SocialTransform!.Range in the last line of code you posted is invalid (WOrkbook.SocialTransform! isn't valid code the way you're using it), and therefore it doesn't return an object. However, you're referencing it as one, which generates the error.
It's valid inside quotes as you're using it in the line that precedes it.

Pivot Tables - VBA

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.

Invalid argument value error when setting MS Project task field

In Excel VBA, I'm getting this error all of a sudden:
Run-timer error 1101: The argument value is not valid.
I'm trying to set all the Percent Work Complete fields of tasks from Microsoft Project to 0.
The error occurs in this block of code:
Dim t As Task
Dim row As Variant
For Each row In tasksDict.Keys
If tasksDict(row).Active Then
Set t = tasksDict(row)
t.SetField FieldID:=188743713, Value:=0 ' ERROR HERE (sets the Percent Work Complete field)
End If
Next row
It doesn't work if I do this either:
t.SetField FieldID:=188743713, Value:="0"
Can anyone help me figure out what a valid value would be?
Edit: Note that this code was working up until today. Could this be a bug on Microsoft's end?
Solved. Upon reading the docs, I realized you cannot set the Percent Work Complete field of summary tasks so I added an extra if-statement in my code:
Dim t As Task
Dim row As Variant
For Each row In tasksDict.Keys
If tasksDict(row).Active Then
Set t = tasksDict(row)
If Not t.Summary Then
t.SetField FieldID:=188743713, Value:=0 ' ERROR HERE (sets the Percent Work Complete field)
End If
End If
Next row

Resources