Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing a optional argument

Posted on 2007-11-30
3
Medium Priority
?
157 Views
Last Modified: 2013-12-26
Hi experts
I have 5 mshflexgrids on a form 3 are contained in pictureboxs I am trying adjust the grids widths, based on the column widths and am trying to also set the picture widths as optional argument. The code below works fine if i pass the picture argument, but fails if with don't with a "Object variable not set" error message. How can i correct this?

Sub AdjustGridWidth(grd As MSHFlexGrid, Optional pic As PictureBox)
    Dim i As Integer
    Dim iWidth As Integer
   
 With grd
    For i = 1 To .Cols - 1
    iWidth = iWidth + .ColWidth(i) + 13
    .Width = iWidth
   Next i
 End With
   
  If IsMissing(pic) Then
    Exit Sub
   End If
   
    With pic
    pic.Width = iWidth
    End With
 
   
    End Sub
0
Comment
Question by:isnoend2001
3 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 20384022
Replace:

  If IsMissing(pic) Then
    Exit Sub
   End If

with:

  If pic Is Nothing Then
    Exit Sub
   End If

IsMissing only works whne the argument is defined as Variant.
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 500 total points
ID: 20384068
Try this instead.  IsMissing only works with variants.  (See the help.)
Sub AdjustGridWidth(grd As MSHFlexGrid, Optional pic As PictureBox = Nothing)
    Dim i As Integer
    Dim iWidth As Integer
    
    With grd
        For i = 1 To .Cols - 1
            iWidth = iWidth + .ColWidth(i) + 13
            .Width = iWidth
        Next i
    End With
    
    If Not pic Is Nothing Then
        With pic
            pic.Width = iWidth
        End With
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 31411983
Thanks both work perfect
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Progress

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question