10 Replies Latest reply on Jan 26, 2016 6:12 AM by Bryce Gill

    Debugging Help for an amateur programmer

    Kevin Collins

      I am a definite rookie when it comes to the API, and furthermore, to programming.  I am SO close to having my macro working, but I am getting an error that I need to debug.  This appears to be the final hurdle to getting my macro to function as I intended.

       

      The debug error states:  "Object variable or With block not set".  Below is the line of code that is causing the error:

       

      Debug_Macro.jpg

       

      I think I know how to set a variable, I'm just not sure which variable to set or what to set it to.

       

      I can include the full macro if it would be helpful.

       

      Thanks in advance!

        • Re: Debugging Help for an amateur programmer
          Attilio Colangelo

          TextBox1 is an object that has many properties.  You want the text property which is the actual text string.  Something like this:

           

          TextBox1.Text = <put your string here>

           

          It may require a more full reference to the text box in the form..

          Me.Controls("TextBox1").Text  = <put your string here>

           

          This is for VBA.

          • Re: Debugging Help for an amateur programmer
            Deepak Gupta

            Have you set Length, Width and Height as double?

             

            Also could you share full macro to debug?

            • Re: Debugging Help for an amateur programmer
              John Alexander

              Have all the variables in that line (Length, Width, Height) been initialized?

               

              One helpful step when posting questions is to provide context (the entire Sub/Function that you are working on or even the entire macro if it isn't proprietary).

               

              ----

               

              Dim TextBox1 As String

              Dim Length As String

              Dim Width As String

              Dim Height As String

               

              Length = ""

              Width = ""

              Height = ""

               

              TextBox1 = Length & " x " & Height & " x " & Width

              • Re: Debugging Help for an amateur programmer
                Roland Schwarz

                Default properties for objects are for amateurs and idiots. Try not to be either.

                • Re: Debugging Help for an amateur programmer
                  Bryce Gill

                  Kevin,

                   

                  It looks like you are misunderstanding the use of textboxes (and variable types in general) in VBA. In brief, the error you are seeing is happening because you have defined "TextBox1" as an object, and you are attempting to assign a string value to that variable. In VBA, object assignments are done using "Set". Because the format (and assignment type) aren't correct here, it is looking for something that doesn't exist - hence the missing variable error.

                   

                  In a bit more detail, there are two main issues that are collaborating here:

                   

                  The first is what I mentioned above. In this case, you want to use something like the following format:

                   

                  Dim TextBox1 as Object  

                  Set TextBox1 = UserForm1.TextBox1

                  TextBox1.Text = Length & " x " & Height & " x " & Width

                   

                  You have done the first part of this near the top of mainTestMacro2(), but all of the following lines try to directly assign a string to "TextBox1" instead of "TextBox1.Text". For example, line 344 is currently:

                   

                  TextBox1 = TextBox1 + "" + Chr(13)

                   

                  Correct the error, and it should look like:

                   

                  TextBox1.Text = TextBox1.Text + "" + Chr(13)

                   

                  Note that in the variable declaration, you could also use "Dim TextBox1 as TextBox" here, since that appears to be the variable type that you are working with. Predefining the object type like this gives VBA the ability to pull up a dropdown menu of useful properties and methods as you type, to make sure that you are making the correct calls. It isn't strictly necessary, but it can be helpful.

                   

                   

                  The second problem is an issue with the flow of your macro. Near the top of the Sub, you have a conditional that sends the program to the "semMassa" section if swModel.GetMassProperties is empty. This is fine in and of itself, but then that section tries to make a reference to the TextBox1 variable. This is a problem, because that variable was never set! Remember, you skipped over the part where you set up the TextBox1,2,3 objects. Right now, this part of your code looks like this:

                   

                  <Sub Start>

                  <Declare variables>

                  <If model mass properties are empty, go to semMassa>

                  <Assign TextBox1,2,3 variables to UserForm1 objects>

                  <Other stuff>

                  <semMassa: Do some stuff, then make a call to TextBox1 variable>

                  <Sub End>

                   

                  So, you're skipping all the stuff in the middle. When the program gets to the reference to TextBox1 in semMassa, you haven't assigned any object to that variable - so it is empty, and you get the object not set error. You can fix this by assigning the TextBox object variables earlier in the code (before that conditional).

                   

                  And finally, I just noticed that your semMassa section looks like it was intended to be run only sometimes, not every time the macro is run. Right now, you will get to that section of the code no matter what because your macro doesn't have an exit prior to the "semMassa" section start. If you don't want it to run, you will want to put an "Exit Sub" after the last line of code above that section, but before the section name.

                   

                  Hope this helps! Let me know if it doesn't make sense.