Return value from seperate sub?

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47
I've made a code that I need to reference another sub routine (it's just getting insane and impossible to follow with just one). I'm passing the sub a string and it manipulates it and calls it something else. I.E.

Sub Main()
string = "532 "
Change string
.
.
.
End Sub

Sub Change(string As String)
CurString = Mid(string, 1, 6)
x = 6
Do While Mid(CurString, 1, x) = " "
CurString = Left(CurString, x)
x = x - 1
Loop
End Sub


How do I get "CurString" back to the Main Sub?

Thanks!!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Here's an example because I'm not sure what you are trying to do with your code:

Code:
Public CurString As String
Sub Main()
Dim str As String
str = "532"
Call Change(str)
MsgBox CurString
End Sub

Sub Change(str As String)
CurString = Left(str, 1)
End Sub


In this line:
Do While Mid(CurString, 1, x) = " "
Mid(CurString, 1, x) is the same as Left(CurString,x)

What are you trying to do with this code?

BTW, you don't want to use string as the name of your variable as it is a reserved word.
 
Upvote 0
An example

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> Str1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> foo()
<SPAN style="color:#00007F">Dim</SPAN> Str2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
Str2 = "xyz"
Str1 = "abc"
<SPAN style="color:#00007F">Call</SPAN> bar(Str2)
MsgBox Str1
MsgBox Str2
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> bar(Str2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
MsgBox Str1
MsgBox Str2
Str1 = Str1 & " revised by bar"
Str2 = Str2 & " and returned to foo"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Note that the Public variable need not be explicitely passed.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,891
Members
449,058
Latest member
Guy Boot

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