I have the following short VBA code, what I want is for it to check a cell, B5 for whether it is a Y or a N, and if Y - go to one macro, and if N - go to another macro.
Sub LoadSetup()
Dim Master As Workbook
Set SingleLoad = Setup.Range("B5")
If SingleLoad = "Y" Then
Call LatestLoad
Else
Call TimeSeriesLoad
End If
End Sub
I am getting a Run-rime error '424' Object Required on this line. Any ideas please what could be causing this?
Set SingleLoad = Setup.Range("B5")
Related
I am trying to use the following macro to assign vba code to a shape.
When using the code I get Runtime Error 438 "Object doesnt support this property or method"
My sheet number always changes, so i cannot hard code it to the code below.
Here is the code I am using:
Sub assignCodeToShape()
Dim x As Integer
x = getSheetNumber
ActiveSheet.Shapes("fileShape").OnAction = Sheets(x) & ".CommandButton1_Click"
End Sub
Function getSheetNumber as Integer()
getSheetNumber =ActiveSheet.Index
End Function
You can do this:
With ActiveSheet
.Shapes("fileShape").OnAction = .CodeName & ".CommandButton1_Click"
End With
I am getting an error when I use Application.Caller in Excel VBA.
I have a piece of code attached to a button/shape that I want to use to print a timestamp in the cell next to the button.
I need to use this button over and over so don't want to reference actual ranges of where to put the timestamp, so I thought this code would work.
However, each time the macro gets to the 2nd line, i.e. the Application.Caller row, it gives me an error:
ERROR 91 : 'Object variable or With block variable not set'
I would greatly appreciate if someone could assist with why this is happening as I can't fix it.
Thank you
I have tried activesheet.buttons as well as activesheet.shapes but neither have worked.
Dim x As Object
Set x = ActiveSheet.Shapes(Application.CALLER)
With x.TopLeftCell
Range(Cells(x.Row, x.Column), Cells(x.Row, x.Column)).Offset(0, 1).Select
End With
I'm looking for a code that it is able to check if a worksheet contains any VBA code inside. In fact, I would like to have all worksheets empty of any VBA code.
This check will be done through another VBA code of course.
Thanks for your help.
Make sure you Tick "Trust access to the VBA project object model" under Macro-trust settings:
Sub CheckForCode()
Dim comp As Object, x As Long
For Each comp In ThisWorkbook.VBProject.VBComponents
With comp.CodeModule
x = x + .CountOfLines
End With
Next
If x > 0 Then MsgBox x & " Lines of VBA-code"
End Sub
Post has been Updated below original post
I am working with two tables and want to have them connected however, the first section contains more values than the second one. I was able to work that out by adding an IfError within the Evaluate function, seen from code example (1) to (2), (using help from If Error Then Blank)
(1)
Worksheets("Sheet1").Range("Cell1").Value = Worksheets("Sheet1").Evaluate("=INDEX(data,MATCH(value,lookup_column,FALSE),column)")
(2)
Worksheets("Sheet1").Range("Cell1").Value = Worksheets("Sheet1").Evaluate("=INDEX(data,MATCH(value,lookup_column,FALSE),column), Cell2")
However, I still would like a message saying that there was an error so I tried
Sub Name()
Application.ScreenUpdating = False
On Error GoTo Msg
Worksheets("Sheet1").Range("Cell1").Value = Worksheets("Sheet1").Evaluate("=INDEX(data,MATCH(value,lookup_column,FALSE),column), Cell2")
Worksheets("Sheet1").Range("Cell1").Value = Worksheets("Sheet1").Evaluate("=INDEX(data,MATCH(value,lookup_column,FALSE),column)")
Exit Sub
Msg: MsgBox "You've had a fatal error"
End
End Sub
It did not return a message, I am assuming this is because the code for VBA was written correctly and it was the Excel function code that had an error. So is there a way to use another function to determine the excel error?
This is a sub part of a larger coding so I know it is something that can be done in excel stand alone, but this is just a minor part of the whole. Thanks in advance.
UDATE:
With comments I was able remove the Evaluate function and replace the original code with the following:
Sub SetWaterfall ()
Application.ScreenUpdating = False
Dim vMatchVal As Variant
If Not IsError(vMatchVal) Then
vMatchVal = Application.Match(Sheets("Sheet1").Range("SelectLine"), Sheets("Sheet1").Range("AS8:AS34"), 0)
Worksheets("Sheet1").Range("AW45").Value = Application.Index(Sheets("Controls").Range("AR8:AR34"), vMatchVal)
Else
Worksheets("Controls").Range("AW45").Value = "Not Data"
MsgBox "First number not found"
End If
End Sub
The issue is still that the index/match functions returns a #NA error and the message box never appears.
(Help converting Index/Match function from Excel formula to VBA code https://www.mrexcel.com/forum/excel-questions/691904-translate-index-match-function-vba-code.html)
(If this edit process is not the correct procedure for let me know and I'll close the post)
In your revised code, you have the If Not IsError test preceding the assignment to the variable you're testing for error!
Let's fix that, and try some other refactoring (for legibility's sake). If this is still not working as expected, you're going to need to provide some example data which others can use to replicate the error.
Sub SetWaterfall()
' It's not necessary to disable ScreenUpdating for this procedure...
' Application.ScreenUpdating = False
Dim theSheet as Worksheet, controls as Worksheet
Dim vMatchVal As Variant
Dim lookupVal as String
Dim matchRange as Range, indexRange as Range
Set theSheet = Sheets("Sheet1")
Set controls = Sheets("Controls")
Set matchRange = theSheet.Range("AS8:AS34")
Set indexRange = controls.Range("AR8:AR34")
lookupValue = theSheet.Range("SelectLine").Value
vMatchVal = Application.Match(lookupVal, matchRange, 0)
If Not IsError(vMatchVal) Then
theSheet.Range("AW45").Value = Application.Index(indexRange, vMatchVal)
Else
controls.Range("AW45").Value = "Not Data"
MsgBox "First number not found"
End If
End Sub
This is my code:
Sub VerticalLoop()
Dim i As Integer
Range("VerticalLoop").Activate
i = 1
Do While i <= Range("VerticalLoop").Rows.Count
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop
End Sub
For some reason I can't run this. When I press F8, the error pops up when I reach the line "i=1", so I don't know exactly what I did wrong.
This code will fail if the Sheet containing the Defined Name has not been activated first!
The error is in the line
Range("VerticalLoop").Activate
The error is probably a typo in the name of the range. Are you sure it isn't supposed to be "Vertical_Loop" (with the underscore)?
On the other hand, Gary's Student is correct that a '1004' error could be triggered by trying to activate a range in an inactive sheet. The error message itself (and not just the error number) can tell you more about the exact source of the error. Alternatively, you could break that line into two lines like thus:
Dim VLoop As Range
Set VLoop = Range("VericalLoop")
VLoop.Activate
If the Set VLoop line throws the error then the problem is with your name. If the second line throws the error then then problem is with what is active.
As an added benefit -- once you debug this error you now have a useful range variable allowing you to use expressions like VLoop.Rows.Count