how to pass a variable no matter what it is

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
Hi,

I understand (I think) that you can pass a variable to another routine like this:

Code:
othersub Target:=Range("A5")

But I want to pass the value of Target to another routine, no matter what Target happens to be at the time.

Basically I want to say:

"Okay, another routine is taking over now, and that new routine needs to know the value of Target, whatever it is at this point."

How do you do that?

Thanks...
 
Another interesting thing is that this error seems to have to do with automation across Office applications, but my code does not refer to anything except excel...at least, that I know of...
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi

As in

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Column = 1 Then
validlist_Col1 Range(Target )
End If ' (If Target.Column = 1 Then)

If Target.Column = 2 Then
validlist_Col2 Range(Target )
End If ' (If Target.Column = 2 Then)

If Target.Column = 3 Then
validlist_Col3 Range(Target )
End If ' (If Target.Column = 3 Then)

End Sub

Tony
 
Upvote 0
Hi

Oops

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Column = 1 Then
validlist_Col1 Range(Target.address )
End If ' (If Target.Column = 1 Then)

If Target.Column = 2 Then
validlist_Col2 Range(Target.address )
End If ' (If Target.Column = 2 Then)

If Target.Column = 3 Then
validlist_Col3 Range(Target.address )
End If ' (If Target.Column = 3 Then)

End Sub


Tony
 
Upvote 0
Miconian,

I can't get the error message to happen for me using your code. However, I think that your problem could be because of your use of the Selection keyword, although I'm not definitely sure. Change your code to get rid of the Selection keyword and see if you still experience the error e.g. your validlist_Col3 procedure should look something like this:-

Code:
Sub validlist_Col3(ByVal Target As Range)

    Select Case Target.Offset(0, -1).Value    'see what's in column B

    Case "blah"
        Target.Value = "blorg"

    Case "blech"
        With Target.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="blah, blah, blah, blah, blah"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With

    End Select

End Sub

Also, there's nothing wrong at all with the way you are calling the procedures (re acw's advice). You should definitely leave your code as is, although it might look nicer if you put each if statement on one line e.g.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Column = 1 Then validlist_Col1 Target
    If Target.Column = 2 Then validlist_Col2 Target
    If Target.Column = 3 Then validlist_Col3 Target

End Sub

HTH
Dan
 
Upvote 0
dk, I made that change, and interestingly, the error is now

Run-time error '-2147417848 (80010108)':
Method 'Add' of object 'Validation' failed

And when I click 'debug,' it's the same stanza of code that was getting the error before.

Code:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="blah, blah"

Actually, instead of "blah, blah" there are 23 choices separated by commas. Some of them include underscores, apostrophes, and question marks, if that matters.
 
Upvote 0
I just comented out all but the first three choices, and didn't get the error (while still using 'With Selection.validation').

The thing is, I need all those choices to be there...
 
Upvote 0
Hi

Did you try the

validlist_Col1 Range(Target.address )

approach. If so, did it work? If not, what error message did you get.


Tony
 
Upvote 0
acw, that yields the same error as before:

Code:
Run-time error '-2147417848 (80010108)':

Automation error
The object invoked has disconnected from its clients.
 
Upvote 0
miconian said:
dk, I made that change, and interestingly, the error is now

....

....

Actually, instead of "blah, blah" there are 23 choices separated by commas. Some of them include underscores, apostrophes, and question marks, if that matters.

What's the actual code you're trying to use?

Dan
 
Upvote 0

Forum statistics

Threads
1,216,160
Messages
6,129,215
Members
449,494
Latest member
pmantey13

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