using the same variable with its value in 2 different sub

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to use the value of variable s in first sub and in the second sub. I do not want to ask user to enter number of students again in the second sub. I asked user to enter number of students in sub1 and assigned this value to "s". So if user enter s=10 then I need to use the same value s=10 in sub2. Can I do that? Thank you


Code:
Dim s as integer
''''''''''''''''''''''''''''''''
Sub studentgrade()
    Dim x As Integer, y As Integer
    s = InputBox("number of students")
    Cells(1, 2).Value = "marks"
    For x = 2 To s
    Cells(x, 2).Value = WorksheetFunction.RandBetween(1, 100)
    Next x
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub myresult()
    Dim x As Integer
    For x = 2 To s
    If Cells(x, 2).Value > 50 Then
    Cells(x, 3).Value = "pass"
    End If
    Next x
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I have tried what you posted and it works properly for me. Do you have any code above the line
Code:
Dim s as integer
That line must be at the very top.
 
Upvote 0
You've declared s twice -- once at the module level, and again in studentgrade. Remove the second declaration.

Read Understanding Scope and Visibility in Help.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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