vba coding using with statements

bolo

Active Member
Joined
Mar 23, 2002
Messages
423
Hi,

Suppose i have the following code.

With Label1
.caption = "Hello"
.borderstyle = fmbordersingle
?????
end with

sub temp (lbl As msforms.label)
lbl.bordercolor = rgb(255,0,0)
end sub

How do i call the sub temp, within the with statement? (i.e call temp (.) Is it possible?

Thanks

Bolo
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi

I don't think it is possible to write it any shorter than by simply using Label1 again ie:

Code:
With Label1
  '...
  Call temp(Label1)
End With
The alternative is longer:

Code:
With Label1
  '...
  Call temp(.Parent.Label1)
End With

and hence makes no sense.

There's no "Me" (or equivalent) property that can be used AFAIK.
 
Upvote 0
Hi,

...How do i call the sub temp, within the with statement? (i.e call temp (.) Is it possible?

Thanks

Bolo

Not as far as I can see. How about:

Rich (BB code):
Option Explicit
    
Sub temp(lbl As MSForms.Label)
    lbl.BorderColor = RGB(255, 0, 0)
End Sub
    
Private Sub UserForm_Initialize()
    
    With Me
        Call temp(.Label1)
        With Label1
            .Caption = "Hello"
            .BorderStyle = fmBorderStyleSingle
        End With
    End With
End Sub
 
Upvote 0
You could do it with
Code:
Private Sub CommandButton1_Click()
   With Label1
      .Caption = "Hello"
      .BorderStyle = fmBorderStyleSingle
      temp .Object
   End With
End Sub
Sub temp(lbl As Object)
   lbl.BorderColor = RGB(255, 0, 0)
End Sub
but I don't see any benefit.
 
Upvote 0
You could do it with
Code:
Private Sub CommandButton1_Click()
   With Label1
      .Caption = "Hello"
      .BorderStyle = fmBorderStyleSingle
      temp .Object
   End With
End Sub
Sub temp(lbl As Object)
   lbl.BorderColor = RGB(255, 0, 0)
End Sub
but I don't see any benefit.

Ah that's the way to do it - I had erroneously attempted the .Object route without modifying the temp argument type to a variant. Interesting. Thanks :)
 
Upvote 0
De nada. :)

Still can't see any real point to it though.
 
Upvote 0

Forum statistics

Threads
1,214,541
Messages
6,120,110
Members
448,945
Latest member
Vmanchoppy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top