References to cells and worksheets in Excel - vba

I have a problem that I don't understand at all:
i = 150
If ActiveWorkbook.Worksheets("foobar").Cells(i, 3).Value Like "*:*" Then
MsgBox "I found a colon!"
End If
As you might guess, the sheet foobar has at position (150, 3) a cell containing a colon, thus the message box is shown.
Now I want to do this:
i = 150
Cell = ActiveWorkbook.Worksheets("foobar").Cells(i, 3).Value 'fails right here
If Cell Like "*:*" Then
MsgBox "I found a colon!"
End If
Here it gives me an error saying "Object variable or With block variable not set. In fact saying:
Sheet = ActiveWorkbook.Worksheets("foobar")
gives a similar message. Why? What am I doing wrong? I just want a reference of that object, or at least a refence.

Bottomline:
When handling objects, use Set.
When handling primitive data (integers, longs, strings*, variants*) you do not use it.
Sheet, Workbook, Range, are objects. Therefore, you need to use Set when assigning them to variables.
A Range.Value returns a Variant (that can be a long, a string, etc.) So, you cannot use Set.
==========================
Now, about your error message... I'd say then that maybe before in your code, Cell is being declared as object. Try use another variable name, or check the Cell variable type.
To check this, right click on it and then click in 'Definition'. Have it declared as Variant might fix the problem (be aware of side effects it might cause, though).
==========================
*I know these types aren't 'primitive'; I used as an example here for the sake of explanation's cleanliness.

As your assigning an object the line
Sheet = ActiveWorkbook.Worksheets("foobar")
should read
Set Sheet = ActiveWorkbook.Worksheets("foobar")

In your line
Cell = ActiveWorkbook.Worksheets("foobar").Cells(i, 3).Value
you are trying to assign the value of cell (probably a string) to a cell. Take the .Value off and the assignment should work better. The Excel message you encountered is not the best: sometimes you get it when you assign variables of the wrong type.
However, the If Cell Like might not work. (Hint: the .Value has to get moved, not deleted.)

I just tried the following code and it works probably in your code make sure the ActiveWorkBook is actually the Active Work book you want(if you are working with multiple work books)
Sub test()
i = 3
If ActiveWorkbook.Worksheets("Sheet1").Cells(i, 2).Value Like ":" Then
MsgBox "I found a colon!"
Else
MsgBox "didn't find a colon!"
End If
Cell = ActiveWorkbook.Worksheets("Sheet1").Cells(i, 2).Value
'fails right here
If Cell Like ":" Then
MsgBox "I found a colon!"
End If
End Sub

Use Dim MyCell as Variant (or as string) since Cell is an existing object

Related

Can't for the life of me figure out what is causing this mismatch error VBA Excel?

I have this code which is causing a mismatch error and I can't figure out why I had it working sort of before with the mismatch error and in an attempt to fix it I can't get it back to working. The values in the merge sheet are all numeric. Basically what I was trying to do was when a value is entered into a cell there would be a VLookup would be executed to input a value into the adjacent cell and once I get this working, more cells in the same row. If any of you are itching to fix something just let me know!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim LooupValue As String
Dim sfx As Long
Set KeyCells = Columns(1)
LooupValue = ActiveCell.Value
sfx = Application.VLookup(LooupValue, Worksheets("Merge").Range("D:BD"), 2, False)
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Range(Target.Address).Offset(0, 1).Value = sfx
End If
End Sub
Edit: Thanks to #Marcucciboy2, #MathieuGuindon, and #BigBen for their successful help, I have done some more research and posted what solved my issue down below.
Dim sfx As Long
sfx = Application.VLookup(LooupValue, Worksheets("Merge").Range("D:BD"), 2, False)
If the vlookup yields xlErrNA i.e. #N/A, then VBA can't coerce its result into a Long, and you get exactly that: a type mismatch error - because xlErrNA is an Error value, which can't be implicitly converted to a String, a Long, or anything. The only type that can contain this data, is a Variant.
Dim result As Variant
result = Application.VLookup(LooupValue, Worksheets("Merge").Range("D:BD"), 2, False)
Dim sfx As Long
If Not IsError(result) Then
sfx = CLng(result)
Else
'lookup yielded no match
End If
Also, it looks like this is off-by-one:
LooupValue = ActiveCell.Value
The ActiveCell likely isn't the same cell as Target, which is the cell that was modified. You probably need this instead:
LookupValue = Target.Value
I'd also recommend making unqualified Range (same with Rows, Colomns, Names, and Cells) calls explicitly qualified - because that exact same code behaves differently depending on where it's written in. By qualifying them with Me, you're making your code more explicit. Code that says what it does, and does what it says, is always better code.
Worksheets("Merge") is a smell: if the sheet exists in ThisWorkbook at compile-time, give it a code name (i.e. set its (Name) property) and use that identifier directly:
result = Application.VLookup(LooupValue, MergeSheet.Range("D:BD"), 2, False)
If the sheet only exists at run-time (e.g. it's in a workbook that was opened by the macro), then you should have a reference to that workbook near where you opened that file, e.g. Set book = Application.Workbooks.Open(path) - use that reference to qualify the Worksheets member call, instead of implicitly referring to ActiveWorkbook.
The lookup range D:DB is excessive when you're only looking for the value in column E. If that hard-coded 2 is there to stay, I'd make the lookup range be D:E.
I would also nest the value setting within the "If" that checks for intersection; otherwise, every time you change the worksheet it does an unnecessary vlookup in the background.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("A:A")) Is Nothing Then
Target.Offset(0, 1).value = Application.VLookup(Target.value, Worksheets("Merge").Range("D:BD"), 2, False)
End If
End Sub
I figured out what the last problem was... The lookup column was, for some reason from the previous document editor, formatted as text. The reason why only some lookup values were working was because even after I formatted the entire column to numbers, the said values were reformatted as numbers only after I had edited the cell. I wasn't going to edit every single cell so I did some research and found out how to refresh an entire column's format.
I refreshed the column by doing the following:
Select the entire column
Format the column as necessary
Go to the Data ribbon and select 'Text-to-Columns'
Ensure Delimited is selected, click Next
Uncheck all the Delimiters, click Next, then Finish

Only some lines throwing "Object variable or with variable not set" error when using .Find method

(I know this sounds like a repeat question, but I've searched everywhere for this, and I couldn't find anything about this)
I'm trying to automate a tax filing process at work. Essentially, I'm reading off several worksheets and populating relevant fields into a master mapping sheet. However (and this is the strange part), when I first coded everything out, there were no errors and I managed to find everything. After saving and reopening the workbook though, I keep getting this error and only for certain lines (9 occurences out of the 57 times I call the .find method in total).
I know the error means that the .find method couldn't find what I was looking for, but I know the field exists since it worked perfectly well before I closed and reopened the workbook, and it works for the rest of the searches. Does anyone have any ideas?
I've checked my macro security settings (everything is enabled), and I know the .find method isn't retrieving anything (I put in "if ___ is nothing" statements to verify, and the affected lines all return nothing when called), but I know the fields I'm searching for exist in their respective sheets.
Here's one of the lines throwing the error:
Range("C7").Select
Set currentRowReference = Worksheets("Stat A").Range("B1:B999").Find("Current year tax losses", LookIn:=xlValues, lookat:=xlPart)
firstCalculation = Worksheets("Stat A").Cells(currentRowReference.Row - 2, "E")
Set currentRowReference = Worksheets("Stat A").Range("A1:A999").Find("Unabsorbed capital allowances c/f", LookIn:=xlValues, lookat:=xlPart)
secondCalculation = Worksheets("Stat A").Cells(currentRowReference.Row - 1, "E")
ActiveCell.FormulaR1C1 = firstCalculation - secondCalculation
Any help or ideas will be much appreciated!
Edit: I've added a larger portion of my code, and a screenshot of the sheet it's supposed to read from. In the code above, firstCalculation successfully computes, while secondCalculation throws the error.
Screenshot of source sheet. Specifically, it's supposed to read A31
since you have to deal with merged cells, you'd better use a function on your own like the following:
Function MyRowFind(rng As Range, textToFind As String) As Long
Dim cell As Range
MyRowFind = -1
For Each cell In rng.SpecialCells(xlCellTypeConstants, xlTextValues)
If InStr(cell.value, textToFind) > 0 Then
MyRowFind = cell.Row
Exit Function
End If
Next cell
End Function
to be used in your main sub as follows:
Sub main()
Dim currentRowReferenceRow As Long
currentRowReferenceRow = MyRowFind(Worksheets("Stat A").Range("A1:A999"), "Unabsorbed capital allowances c/f")
If currentRowReferenceRow > 0 Then
' code
End If
End Sub

VBA Runtime error 1004 when trying to access range of sheet

I am building a small vba script that is merging tables from several workbook into one single worksheet of another workbook. The error is raised when I try to set the destination range's value:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
The error: "Run-time error '1004': Application-Defined or object-defined error"
I went through similar questions, and the general answer is what I found in this one: The selected cell belongs to another worksheet than the one desired.
While this makes complete sense, I still can't figure why my code breaks as I'm only using numerical reference (CurrentRow is a Long) and Resize, which should prevent me from doing such a mistake.
Additionally, I ran a couple quick tests in the Immediate window and it turns out that while the worksheet wksPivotData exists and I can access its name and a cell value, the range function simply doesn't work:
Debug.Print wksPivotData.Name
PivotData
Debug.Print wksPivotData.Cells(1, 1).Value
123
Both of those work but the next one doesn't:
Debug.Print wksPivotData.Range(1, 1).Value
Your last line, Debug.Print wksPivotData.Range(1, 1).Value won't print because you're misuing Range(). I assume you want A1?
When using Range(1,1), you're referring to a non-existent range. If you want to do cell A1, you need
With wksPivotData
myData = .Range(.Cells(1,1),.Cells(1,1)).Value
End with
Since you're using multiple worksheets, I'd use the with statement as above. Another way to write the same thing is wksPivotData.Range(wksPivotData.Cells(1,1),wksPivotData.Cells(1,1)) (You need to explicitly tell Excel what sheet you want to refer to when using Range() and cells().
Finally, for your resize, if I recall correctly, you're going to have to add the same Cell() twice in your range:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1),ksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
Or, for the same thing, but different way of doing it:
With wksPivotData
.Range(.Cells(currentRow, 1), .Cells(currentRow, 1)).Resize(tbl.ListedRows.Count, tbl.ListColumns.Count).Value = tbl.Range.Value
End With

Returning the Cell location of specific value in a separate Workbook

To clarify. I have 1 spreadsheet I am creating the VBA in. When the Macro runs, I want the code to find a specific value in a separate WorkBook sheet and return the location of the value in the other WorkBook.
My initial thought was to use HLookUp, however that will only return the value, not the location of the value. My next thought was to use the Find function, but I can't get it to run. The error I get is: Run-time error "438': Object doesn't support this property or method
startValue = enrollBook.Sheets("Pop-FY").Range("D:Z"). _
Applications.WorksheetFunction. _
Find(What:=FYString, LookIn:=xlValues)
enrollBook is the other workbook.
startValue is supposed to be the location of the found value in the other spreadsheet
This is a weird way of calling a function & formatting you've got there, I first even thought that code was invalid.
First, you are confusing Range.Find with Application.WorksheetFunction.Find. You need the Range one, but are calling the other one.
Second, the error is because it's Application, not Applications.
Third, you will need a Set:
Set startValue = enrollBook.Sheets("Pop-FY").Range("D:Z").Find(What:=FYString, LookIn:=xlValues)
You mean something like this?
Dim sTest As String
Dim oRange As Range
Set oRange = Worksheets(1).Range("A1:Z10000").Find("Test", lookat:=xlPart)
MsgBox oRange.Address
I tested this, but you need to change your parameters.

How to get/set unique id for cell in Excel via VBA

I want to have/define a unique id for each data row in my Excel data sheet - such that I can use it when passing the data onwards and it stays the same when rows are added/deleted above it.
My thoughts are to use the ID attribute of Range (msdn link)
So, I have a user defined function (UDF) which I place in each row that gets/sets the ID as follows:
Dim gNextUniqueId As Integer
Public Function rbGetId(ticker As String)
On Error GoTo rbGetId_Error
Dim currCell As Range
'tried using Application.Caller direct, but gives same error
Set currCell = Range(Application.Caller.Address)
If currCell.id = "" Then
gNextUniqueId = gNextUniqueId + 1
'this line fails no matter what value I set it to.
currCell.id = Str(gNextUniqueId)
End If
rbGetId = ticker & currCell.id
Exit Function
rbGetId_Error:
rbGetId = "!ERROR:" & Err.Description
End Function
But this fails at the line mentioned with
"Application-defined or object-defined error"
I thought perhaps its one of those limitations of UDFs, but I also get the same error if I try it from code triggered from a ribbon button...
Any other suggestions on how to keep consistent ids - perhaps I should populate the cells via my ribbon button, finding cells without IDs and generating/setting the cell value of those...
EDIT:
As Ant thought, I have the sheet protected, but even in an unlocked cell it still fails. Unprotecting the sheet fixes the problem.... but I have used "Protect UserInterFaceOnly:=True" which should allow me to do this. If I manually allow "Edit Objects" when I protect the sheet it also works, but I don't see a programmatic option for that - and I need to call the Protect function in AutoOpen to enable the UserInterfaceOnly feature...
I guess I need to turn off/on protect around my ID setting - assuming that can be done in a UDF... which it seems it cannot, as that does not work - neither ActiveSheet.unprotect nor ActiveWorkbook.unprotect :(
Thanks in advance.
Chris
Okay...
It does appear that if the sheet is locked, macros do not have write access to low-level information such as ID.
However, I do not think it is possible to unprotect the sheet within a UDF. By design, UDFs are heavily restricted; I think having a cell formula control the sheet protection would break the formula paradigm that a cell formula affects a cell only.
See this page on the Microsoft website for more details.
I think this limits your options. You must either:
give up sheet protection
give up the UDF, use a Worksheet_Change event to capture cell changes and write to ID there
use a UDF that writes the ID into the cell value, rather than save to ID
The UDF approach is fraught with problems as you are trying to use something designed for calculation of a cell to make a permanent mark on the sheet.
Nonetheless, here's an example of a UDF you can use to stamp a "permanent" value onto a cell, which works on unlocked cells of a protected sheet. This one only works for single cells (although it could be adapted for an array formula).
Public Function CellMark()
Dim currCell As Range
Set currCell = Range(Application.Caller.Address)
Dim myId As String
' must be text; using .value will cause the formula to be called again
' and create a circular reference
myId = currCell.Text
If (Trim(myId) = "" Or Trim(myId) = "0") Then
myId = "ID-" & Format(CStr(gNextUniqueId), "00000")
gNextUniqueId = gNextUniqueId + 1
End If
CellMark = myId
End Function
This is quite flawed though. Using copy or the fillbox will, however, retain the previous copied value. Only by explicitly setting cells to be a new formula will it work. But if you enter in the formula into the cell again (just click it, hit ENTER) a new value is calculated - which is standard cell behaviour.
I think the Worksheet_Change event is the way to go, which has much more latitude. Here's a simple example that updates the ID of any cell changes. It could be tailored to your particular scenario. This function would need to be added to every Worksheet the ID setting behaviour is required on.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim currCell As Range
Set currCell = Target.Cells(1, 1)
Dim currId As String
currId = currCell.ID
If Trim(currCell.ID) = "" Then
Target.Parent.Unprotect
currCell.ID = CStr(gNextUniqueId)
Target.Parent.Protect
gNextUniqueId = gNextUniqueId + 1
End If
End Sub
Last note; in all cases, your ID counter will be reset if you re-open the worksheet (at least under the limited details presented in your example).
Hope this helps.
Concur with Ant - your code works fine here on Excel 2003 SP3.
I've also been able to use:
Set currCell = Application.Caller
If Application.Caller.ID = "" Then
gNextUniqueId = gNextUniqueId + 1
'this line fails no matter what value I set it to.
currCell.ID = Str(gNextUniqueId)
End If
Aha! I think I have it.
I think you're calling this from an array formula, and it only gets called ONCE with the full range. You can't obtain an ID for a range - only a single cell. This explains why Application.Caller.ID fails for you, because Range("A1:B9").ID generates an Application-defined or object-defined error.
When you use Range(Application.Caller.Address) to get the "cell" you just defer this error down to the currCell.ID line.
I think we may have a few issues going on here, but I think they are testing issues, not problems with the code itself. First, if you call the function from anything other than a Cell, like the immediate window, other code, etc. Application.Caller will not be set. This is what is generating your object not found errors. Second, if you copy/paste the cell that has the function, they you will by copy/pasting the ID too. So wherever you paste it to, the output will stay the same. But if you just copy the text (instead of the cell), and then paste then this will work fine. (Including your original use of Application.Caller.)
The problem is with Application.Caller.
Since you are calling it from a user defined function it is going to pass you an error description. Here is the remark in the Help file.
Remarks
This property returns information about how Visual Basic was called, as shown in the following table.
Caller - Return value
A custom function entered in a single cell - A Range object specifying that cell
A custom function that is part of an array formula in a range of cells - A Range object specifying that range of cells
An Auto_Open, Auto_Close, Auto_Activate, or Auto_Deactivate macro - The name of the document as text
A macro set by either the OnDoubleClick or OnEntry property - The name of the chart object identifier or cell reference (if applicable) to which the macro applies
The Macro dialog box (Tools menu), or any caller not described above - The #REF! error value
Since you are calling it from a user defined function, what is happening is Application.Caller is returning a String of an error code to your range variable curCell. It is NOT causing an error which your error handler would pick up. What happens after that is you reference curCell, it's not actually a range anymore. On my machine it tries setting curCell = Range("Error 2023"). Whatever that object is, it might not have an ID attribute anymore and when you try to set it, it's throwing you that object error.
Here's what I would try...
Try removing your error handler and see if VBA throws up any exceptions on Range(Application.Caller.Address). This won't fix it, but it could point you in the right direction.
Either through logic or Application.ActiveCell or however you want to do it, reference the cell directly. For example Range("A1") or Cells(1,1). Application.Caller.Address just doesn't seem like a good option to use.
Try using Option Explicit. This might make the line where you set curCell throw up an error since Range(Application.Caller.Address) doesn't look like it's passing a range back, which is curCell's datatype.
I have found that if I protect the sheet with "Protect DrawingObjects:=False", the UDF can set the Id. Strange.
Thanks for all the help with this.