I've looked through countless examples (and there are many to be found) of how to use Ranges to accomplish a VBA user defined function/sub to copy the value of one specified cell to another. Unfortunately, no matter which I try, I am unable to avoid Error 1004 Application-defined or object-defined error. Here is the very simple test code:
Private Sub Foobar()
On Error GoTo fooErrorHandler
Dim c1 As Range
Dim c2 As Range
Set c1 = Sheets("Sheet1").Range("D2")
Set c2 = Sheets("Sheet1").Range("B3")
c2.Value = c1.Value
Exit Sub
fooErrorHandler:
MsgBox "Error Number: " & Err.Number & vbNewLine _
& "Description: " & Err.Description
End Sub
Thanks for any help/pointers!
Generally speaking you cannot use a Function to manipulate the worksheet object. It looks like you're trying to get around that by invoking a subroutine from the barfoo function call. I suspect that is the error... If you run the subroutine foobar manually (press F5 or run from the macro menu) it should execute without error.
I confirm that this implementation raises the error, and also test the manual call to foobar without error.
If you can better describe the ultimate goal, perhaps we can recommend a more reliable way to achieve it.
Modifying cells inside UDF (a function, that is used inside a cell the same way as built-in formulas) is not allowed. If you remove a call to barfoo from C1 cell, both Foobar and barfoo should work without problems.
Related
In my excel sheet, I have a cell called su_callLog.
My VBA code is:
Private Sub su_callLogCopy_Click()
CopyCell (Range("su_callLog")) 'Error Here
End Sub
Sub CopyCell(cell As Range)
MsgBox cell.Value
End Sub
I'm trying to use the function to process the info in that cell. Even when i try to merely print the contents of the Range, I get the VBA Run-time error '424' Object Required error. I'm new to VBA. Please tell me what I'm doing wrong.
NOTE: MsgBox (Range("su_callLog")) produces the expected results.
Remove the parentheses:
CopyCell Range("su_callLog")
If you use parentheses, you are forcing that the arguments are taken ByVal. And the Range() is an object type, which is passed ByRef.
ByVal vs ByRef VBA
As #Vityata correctly pointed oute, you're forcing ByVal to be
taken, while you need to be passing a reference to a range.
While that much is true and CopyCell Range("su_callLog") will produce the desired result, there is one more way to achieve this:
in which you implicity reference the Range correctly while in parentheses
so in other words, you (knowingly or unknowingly) pass the reference properly.
CopyCell (Sheets("Sheet1").Range("A1"))
Not only you maintain your desired (although unnecessary) braces syntax, but it actually works. Additionally it's a good VBA coding practice to be using a specific Sheet every time you're working with a Range object
So to speak, you sort of kill two birds with one stone here
I've been trying to make this work and I just can't seem to figure it out. I have this test piece of code that I got from here.
Sub UsingErr()
On Error GoTo eh
Dim total As Long
total = "aa"
Done:
Exit Sub
eh:
Debug.Print "Error number: " & err.Number _
& " " & err.Description
End Sub
This is supposed to help me understand error management but when it's ran I get a: Compile Error: Expected Function or Variable on err.Number; it specifically highlights err.
Any help would be appreciated.
One minor thing I noticed in your code is the "err" is lower case. When I pasted your code into a blank workbook and ran it, Excel automatically capitalized "Err" in the code. As people mentioned in the comments, this probably means that you have something else open which has a conflicting definition of "err" in it. Try running your code without any other files open.
Every cell modifying line in the code below "throws" a 1004 error.
The following Function is called through a cell, this way: =bonjour()
Here is the code:
Public Function bonjour() As Integer
On Error GoTo Handler
Range("B2").Value = 41
Cells(2, 2) = 42
ThisWorkbook.Sheets("Feuil1").Range("B2").Value = 43
ThisWorkbook.Sheets("Feuil1").Cells(2, 2) = 44
Handler:
If Err.Number <> 0 Then
Debug.Print ("Error n° " & Err.Number & " - " & Err.Description)
Err.Clear
Resume Next
End If
bonjour = 45
End Function
You cannot modify cells of the worksheet using a UDF (User Defined Function) called from the worksheet. Sorry. Excel will not allow this. Attempting it will cause an error every time.
From Microsoft Website :
A user-defined function called by a formula in a worksheet cell cannot
change the environment of Microsoft Excel. This means that such a
function cannot do any of the following:
Insert, delete, or format cells on the spreadsheet.
Change another cell's value.
Move, rename, delete, or add sheets to a workbook.
Change any of the environment options, such as calculation mode or screen views.
Add names to a workbook.
Set properties or execute most methods.
You need to find another way to call bonjour. Maybe using a button/shape or perhaps using Events.
I am looking for a formula which can directly be used in cells to read all the active tabs' name. Please refer the screen shot for the same.
There is also a =MID(CELL("filename"),FIND("]",CELL("filename"))+1,255) formula, but it is only giving the current tab name.
Though this is easily possible using macro, but would be great if can get formula for that.
There is a way to do this through formula's only,
Have a look here
It feels a bit double to post exactly how it's done, but the approach makes use of a named range and a lookup formula
It's fairly easy to do
I note you say formula but you could use a very simple User Defined Function (UDF) which goes in a standard module in the VBE (which you open with Alt+F11)
Option Explicit
Public Function GetTabName(ByVal tabIndex As Long) As String
GetTabName = ThisWorkbook.Worksheets(tabIndex).Name
End Function
The sheet index gets passed into the UDF as a parameter and the associated sheetname is returned.
If testing for visible sheet you could use the following, which has additional handling for sheet not found:
Option Explicit
Public Function GetTabName(ByVal tabIndex As Long) As String
Dim ws As Worksheet
On Error GoTo Errhand
Set ws = ThisWorkbook.Worksheets(tabIndex)
If ws.Visible Then
GetTabName = ThisWorkbook.Worksheets(tabIndex).Name
Else
GetTabName = "N/A"
End If
Errhand:
If Err.Number <> 0 Then
Select Case Err.Number
Case 9
GetTabName = "Sheet not found"
End Select
End If
End Function
UDF Limitations
Trying to activate the cell in my column A which says "Generator loading".
I have been trying the 'With' and 'End With' commands and other suggested formats posted on the net. However, I keep getting the same error- Run-time Error 91.
From my various trials I am very sure that there is something wrong within the "Find" command, but I cannot figure out what... I have been filling it using the format on the MSDN page.
Do you have any suggestions?
Dim findstring As String
findstring = "Generator loading"
Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1)).Activate
The error commonly arises if the result of the .Find method is Nothing, because you can't do Nothing.Activate
First, you have to check for Nothingness
Dim rng as Range
Set rng = Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1))
If rng Is Nothing Then
MsgBox findString & " not found!!", vbCritical
Exit Sub
End If
rng.Activate
'the rest of your code goes here...
I don't like to work with columns, so instead of it, I used a range.
For me to work, I just replaced the reference of the sheet
Sheets(1).Range("A:A").Find(What:=findstring, After:=Cells(9, 1)).Activate
See if this works. If not, you could send me the sheet if you want :)