VBA Using variable workbook and worksheet in a different sub

Zoolou

New Member
Joined
Jun 11, 2019
Messages
2
Here is the code I have right now

Public ReportFile as Workbook
Public ReportWorksheet as Worksheet
Sub VBA_Project()


Define_File
Define_Tab
Data_Cleanup


End Sub


Sub Define_File()


Dim ReportFilePath As Variant

ReportFilePath = Application.GetOpenFilename(filefilter:="excel Files,*.xl*;*xm*")

If ReportFilePath <> False Then
Workbooks.Open Filename:=ReportFilePath
End If

Dim ReportFile As Workbook
Set ReportFile = ActiveWorkbook

End Sub

sub define_tab()

'code to get to the desired worksheet'

Dim DataWorksheet As Worksheet
Set DataWorksheet = ActiveSheet


end sub

Sub Data_Cleanup()

ReportFile.Activate
DataWorksheet.select

'some more code'

end sub


I have tested the variables in the subs they are defined in and they worked perfectly fine. I am trying to run the macro using the first sub that references the others.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Don't declare the variables twice; both as public and within each procedure.

Just declare them once as public and that's it.

Code:
[color=darkblue]Public[/color] ReportFile [color=darkblue]As[/color] Workbook
[color=darkblue]Public[/color] DataWorksheet [color=darkblue]As[/color] Worksheet


[color=darkblue]Sub[/color] VBA_Project()
    
    Define_File
    define_tab
    Data_Cleanup
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]




[color=darkblue]Sub[/color] Define_File()
    
    [color=darkblue]Dim[/color] ReportFilePath [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    
    ReportFilePath = Application.GetOpenFilename(filefilter:="excel Files,*.xl*;*xm*")
    
    [color=darkblue]If[/color] ReportFilePath <> [color=darkblue]False[/color] [color=darkblue]Then[/color]
        [color=darkblue]Set[/color] ReportFile = Workbooks.Open(Filename:=ReportFilePath)
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]


[color=darkblue]Sub[/color] define_tab()
[color=green]'code to get to the desired worksheet'[/color]
    [color=darkblue]Set[/color] DataWorksheet = ActiveSheet
    
[color=darkblue]End[/color] Sub


Sub Data_Cleanup()
    
    ReportFile.Activate
    DataWorksheet.Select
    
    [color=green]'some more code'[/color]
    
[color=darkblue]End[/color] Sub
 
Upvote 0
Worked perfectly. I can't believe it was that simple. I really appreciate the help.

Don't declare the variables twice; both as public and within each procedure.

Just declare them once as public and that's it.

Code:
[COLOR=darkblue]Public[/COLOR] ReportFile [COLOR=darkblue]As[/COLOR] Workbook
[COLOR=darkblue]Public[/COLOR] DataWorksheet [COLOR=darkblue]As[/COLOR] Worksheet


[COLOR=darkblue]Sub[/COLOR] VBA_Project()
    
    Define_File
    define_tab
    Data_Cleanup
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]




[COLOR=darkblue]Sub[/COLOR] Define_File()
    
    [COLOR=darkblue]Dim[/COLOR] ReportFilePath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    
    ReportFilePath = Application.GetOpenFilename(filefilter:="excel Files,*.xl*;*xm*")
    
    [COLOR=darkblue]If[/COLOR] ReportFilePath <> [COLOR=darkblue]False[/COLOR] [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]Set[/COLOR] ReportFile = Workbooks.Open(Filename:=ReportFilePath)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]


[COLOR=darkblue]Sub[/COLOR] define_tab()
[COLOR=green]'code to get to the desired worksheet'[/COLOR]
    [COLOR=darkblue]Set[/COLOR] DataWorksheet = ActiveSheet
    
[COLOR=darkblue]End[/COLOR] Sub


Sub Data_Cleanup()
    
    ReportFile.Activate
    DataWorksheet.Select
    
    [COLOR=green]'some more code'[/COLOR]
    
[COLOR=darkblue]End[/COLOR] Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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