Invalid argument value error when setting MS Project task field - excel

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

Related

Excel VBA error when calling MS Project Task's Unique ID

In an Excel worksheet, I am iterating through tasks in a MS Project file with VBA.
I've found that my code works perfectly until I encounter a MS Project task that does not have a name but the error is confusing me. When I delete these unnamed tasks, the error goes away.
I tried adding an if statement: If prj.Tasks(i).UniqueID Is Nothing Then to catch this error but I get a Type mismatch error.
Dim i As Integer
Dim TaskID As Long
Dim ExcelRow As Integer
For i = 1 To prj.Tasks.Count
TaskID = prj.Tasks(i).UniqueID 'This line is highlighted when I debug
ExcelRow = GetRowByUniqueID(TaskID)
Debug.Print ExcelRow
My error is Object variable not set (Error 91). I am confused by this because I was under the impression that all Tasks have a unique ID. When I open my .mpp file in Microsoft Project, I can see that the tasks have no name but have a unique ID in the Unique ID column.
Edit: I just realized it is not an issue with prj.Tasks(i).UniqueID but rather an issue with prj.Tasks(i). How is it possible that this variable is not set when i is within the range of the number of Tasks?
Okay so I just figured it out.
Somehow, when I iterate through all the Tasks, some of them are Nothing. I don't really understand why but at the very least, I was able to catch this with an if statement:
If prj.Tasks(i) Is Nothing Then
Debug.Print "This is nothing."
Else
TaskID = prj.Tasks(i).UniqueID
ExcelRow = GetRowByUniqueID(TaskID)
Debug.Print ExcelRow
Now, my code does not break at all.

Run-time error '438' Object doesn't support this property or method - At rowcount 0

I wrote a VBA script that runs through a bank statement finding payments of a certain value. This code ran when I wrote it, but a couple of months later I get Run-time error '438' Object doesn't support this property or method; here is an extract of the code.
Set bankWB = ActiveWorkbook
Set bankWS = ActiveSheet
For rowcount = 0 To 250
skipline = False
If bankWS.Cells(rowcount, paidcol) = 5 Then
payee = Trim(bankWS.Cells(rowcount, payeecol))
paid = "5"
The error occurs on the 'if' line.
I've found various examples that reference cell values in the same way and this worked previously. If you Google this error there are lots of results and I see that using 'range' is more common than using 'cell' to reference a single cell, but it's not obvious to me how to easily translate my (x,y) cell variables/loop into a range "A1" type value so I've not tried rewriting the code to use range instead yet.
I assume that the object is bankWS.Cells and it's the property rather than the method that is the problem. I want to know what the value of the object is (=property), I'm not trying to do anything with the object (=method)
I've seen lots of different ways of writing the cell object, and tried various permutations, none of which make a difference. eg.
If worksheet.bankWS.Cells(rowcount, paidcol) = 5 Then
If bankWS.Cells.item(rowcount, paidcol) = 5 Then
If bankWS.Cells(rowcount, paidcol).value = 5 Then
I'm thinking that the code is ok, and that something else has changed.
Solved
Thanks to #99moorem my row count loop should have started at 1, not 0, there will be no cell(0,1).

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.

object defined error with assigning named range to array

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

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

Resources