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

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).

Related

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 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.

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

I'm getting a runtime errror 438 when trying to format a column in a spreadsheet

Sheets("Reformatted").Column("F").NumberFormat = "$#,##0.000"
To better debug a complex expression, break it into smaller parts. In your case:
Set sh = Sheets("Reformatted")
Set rg = sh.Column("F")
Let rg.NumberFormat = "$#,##0.000"
Broken into multiple statements, you can step using the debugger, and figure out exactly which piece part of the larger statement is causing the problem. Since the error happens on the Column statement, you can deduce that the error message is referring to Column.

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