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!!
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

Sharkie21

Active Member
Joined
Nov 2, 2005
Messages
319
two ways.

1 Make Change into a function and return the string
or 2 pass the param by ref.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,812
Messages
5,574,455
Members
412,595
Latest member
slim313
Top