VBA-Object Required Error - excel

I was writing a function with ws declared already. It works well under the private sub of the command button but when i extract it as a function it shows the object required error. The file is too large and I must make it as a function and call it but the error stopped my program running.
Could anyone please help me thanks a lot.
If Month(ws.Cells(j, 11)) = "a" And Year(ws.Cells(j, 11)) = ComboBox1.Text Then 'this sentence error
If ws.Cells(j, 5) <> "-" Then 'this sentence also error if I deleted the above one
n = 2
Else: n = 1
End If
p = 1

Related

VBA GetField Error - Expecting an Already Dimensioned Array

I have a VBA code that allows a user to enter in record ID numbers in a system and the code will record pertinent data to a txt file. This code was working flawlessly and one day it stopped working. An error is occurring at the Call mainLookup(GetField(rid, i, ",") portion of the code but I cannot identify and correct the error.
Sub Main()
Set System = CreateObject("XXXXX.System")
If (System Is Nothing) Then
Stop
End If
Set Session = System.ActiveSession
If (Session Is Nothing) Then
Stop
End If
Set Screen = System.ActiveSession.Screen
rid$ = InputBox("Enter RID (no dashes)" + " separate entries with a comma only")
If (Len(rid) Mod 5 <> 7) Then
MsgBox "Incorrectly formatted RID", 16, "ALAS"
End If
Number% = (Len(rid) + 1) / 6
For i = 1 To Number
Call mainLookup(GetField(rid, i, ","))
Next i
Stop
End Sub

VBA Excel - Compile Error - Expected function or variable

It's probably something simple but I have been trying to work out how to fix a compile error I am getting with an excel document I have created for a while now and I have not made any progress.
Basically there is a form that has several buttons that call a public function called "writeEntry" which is stored in a module called "describePhotosExcel". The code breaks no matter where writeEntry is called from.
So far I have tried calling the function from different subs, changing the subs from private to public, adding the module name before the function and renaming the function... I'm not quite sure what else I can do. Any help would be much appreciated.
Code from the form:
Private Sub PrevPhotoBTN_Click()
wrow = CInt(PhotoDataEntry.CurrentRow.Caption)
test = writeEntry(wrow, Direction.Text, RoadCombo.Text, Chainage.Text, Location.Text, CommentsCombo.Text, Condition.Text)
wrow = CInt(PhotoDataEntry.CurrentRow.Caption) - 1
PhotoDataEntry.CurrentRow.Caption = wrow
temp = PopulatePhotoForm()
End Sub
Code from the module "describePhotosExcel":
Public Function writeEntry(CurrentRow, Dir, RoadName, Chainage, Optional Title = "", Optional Comments = "", Optional Rating = "", Optional PhotoNumber = "") As Variant
Application.Workbooks(PhotoDataEntry.WBK.Caption).Activate
Worksheets("Data").Activate
Cells(CurrentRow, 6) = Dir
Cells(CurrentRow, 8) = RoadName
Cells(CurrentRow, 9) = Chainage
Cells(CurrentRow, 10) = Title
Cells(CurrentRow, 11) = Comments
Cells(CurrentRow, 14) = Rating
Cells(CurrentRow, 13) = PhotoNumber
writeEntry = 1
End Function
So I worked it out.
I had created a subroutine called "test" which was used to do some testing.
Therefore when it saw the word test it thought I was referring to this subroutine and not a new variable I was throwing away. Deleted that and it works well now.

"method 'add' of object 'SmartArtNodes' failed"

I have the follow code that is running in an Excel and it's calling a PowerPoint slide. I am setting a text to each existing node of the SmartArt and it's working. However, I am getting error when I use AddNode method (oSmartArt.AllNodes.Add.AddNode). What am I doing wrong?
Set oSmartArt = d_ppt_output.Slides(SLIDE_SMART).Shapes("MyList").SmartArt
x = 1
While Cells(x, 1).Text <> ""
If x > oSmartArt.Nodes.Count Then
oSmartArt.AllNodes.Add.AddNode ''''' ERROR IS HERE!
End If
oSmartArt.AllNodes(x).Shapes.TextFrame2.TextRange.Text = ActiveSheet.Cells(x, 2).Text
x = x + 1
Wend
I also replaced oSmartArt.AllNodes.Add.AddNode by oSmartArt.Nodes.Add.AddNode but I get the same problem.
The error is:
-2147467259 (80004005)
Method 'add' of object 'SmartArtNodes' failed
The whole code can by found here - https://drive.google.com/drive/folders/1_O79iiG7hbBjMHMSH6kZGkWmjN1WGorR?usp=sharing
As posted, your code doesn't run. You can't reference a slide with a name that's not in quotes. I created a super-simple version in PowerPoint that starts with a 5-member SmartArt. This is running without an error. It adds a new node and fills in the text as expected:
Sub Test()
Set oSmartArt = ActivePresentation.Slides(1).Shapes(2).SmartArt
oSmartArt.AllNodes.Add.AddNode
oSmartArt.AllNodes(6).Shapes.TextFrame2.TextRange.Text = "Wha?"
End Sub

Two modules, Same line of code, one code works but other does not

I have just started using dictionaries and Class collections. I wrote a code using both that worked fine (see code below).
For a = 2 To UBound(FullArray, 1)
Set GEMclass = dict.Items(a) '<-------
GEM = GEMclass.g
'Do while loop
Do
'Check to see if Parent has an owner
If (Not (dict.Exists(GEM))) Then
Nrow = 0
Else
Nrow = dict(GEM).Num
Call Main
Call PartTwo
Call PartThree
End If
Loop Until (Nrow = 0) 'keep doing this unti no tree link
Next a
Call TurnOnFunctionality
End Sub
However, I tried to use the same line of code in another sub and it does not work (the line is in marked with an arrow).
Dim i As Integer
If ((GemDict.Exists(GEM))) Then
i = GemDict.Item(GEM)
i = i - 2
Set GEMclass = GemDict.Items(i) '<-------
'Debug.Print GemDict.Item(GEM)
'Debug.Print GemDict.Keys(i)
If GEMclass.NumofP > 1 Then
MsgBox "Greater 1"
Else
MsgBox "Only 1"
End If
End If
GEM = GEMclass.P
I declared both GEMclasses in each code as so
Public GEMclass As Gclass
Any ideas? I am stumped.

VBA Excel - Mantaining Public Variables With Button Press

I'm trying to Create a document that has a bunch o constant strings.
I've declared then Public in a Module like this:
Public Abc As String
In "ThisWorkbook" I run the following code to initialize de variable
Private Sub Workbook_Open()
Abc = "C5"
End Sub
I have Buttons coded to change some values like:
If Range(Abc) = "" Then
Range(Abc) = 1
Else
Range(Abc) = Range(Abc) + 1
End If
When I run a button with this code:
Sub BotaoNovoDia()
i = 3
While i <= 33
If Cells(i, 11) = "" Then
Cells(i, 11) = Range(Apresentacao)
Cells(i, 12) = Range(Aceitacao)
Cells(i, 13) = Range(Aceitou)
Cells(i, 31) = Range("D41")
Cells(i, 11).Interior.Color = Range(Apresentacao).Interior.Color
Cells(i, 12).Interior.Color = Range(Aceitacao).Interior.Color
If Range("K34") < 0.65 Then
Range("K34").Interior.Color = vbRed
Else
Range("K34").Interior.Color = vbGreen
End If
If Range("L34") < 0.45 Then
Range("L34").Interior.Color = vbRed
Else
Range("L34").Interior.Color = vbGreen
End If
Range(Aceitou) = 0
Range(Rejeitou) = 0
Range(NaoApres) = 0
End
Else
i = i + 1
End If
Wend
End Sub
And i try to run the first button again I get and error saying: "Run-Time error '1004': Method 'Range' of object '_Global' failed"
the debug button takes me to the fisrt line that tries to access to the public variables value. What can i do to mantain the values in the Public variables?
When you call End (By itself, not as part of End If, etc) you clear your Globals.
Don't use End.
Using a named range is a great idea; but there's no reason why your public declaration shouldn't work - except for that pesky End statement. (I missed that first read through...)
However, your scope isn't clear in each of your functions, e.g. which worksheet is the Range referring to, so if one function works on another worksheet, pushing the button that fires "Change" could refer to a different place that doesn't like that reference.
e.g. your ranges should be something like SomeWorkbook.TheWorksheet.Range(<range>) and when you're changing the cell value, you should use .Value to ensure there's no ambiguity - you'll know from searching through here that error 1004 is the least descriptive error code...

Resources