在所有模块中调暗并设置一个变量 excel vba

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40478792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:29:51  来源:igfitidea点击:

dim and set a variable across all modules excel vba

excelvbaexcel-vbamacros

提问by Chuck0185

I have many projects where I have the same variables across multiple modules. In each module I dim and set the variables and each time they are the same variable type and have the same value. How do I dim and set variables across an entire project/workbook?

我有很多项目,其中跨多个模块具有相同的变量。在每个模块中,我调暗和设置变量,每次它们都是相同的变量类型并具有相同的值。如何在整个项目/工作簿中变暗和设置变量?

Ex: (I have many modules in a workbook where I have had to repeat all of the following along with many other similar variables that do not change across modules)

例如:(我在工作簿中有许多模块,我不得不重复以下所有内容以及许多其他不会跨模块更改的类似变量)

Sub PullSFAFiles()
Dim Wb                          As Workbook
Dim WsSFAFiles                  As Worksheet
Dim WsAllCourses                As Worksheet
Dim rngAllCourses               As Range
Dim rngCourse                   As Range
Dim LoSFAFiles                  As ListObject
Dim rngPreviousFiles            As Range
Dim rngRemoveLines              As Range

Dim strCourse                   As String
Dim strApp                      As String
Dim strPeCFldrPath              As String
Dim strFileLocation             As String
Dim strFileNm                   As String
Dim objFile                     As Object

Dim intSFARow                   As Integer
Dim intCourseRow                As Integer
Dim intPFilesRow                As Integer
Dim dtLastUpdate                As Date
Dim intNumRemove                As Integer

Set Wb = ThisWorkbook
Set WsSFAFiles = Wb.Sheets("sfafiles")
Set WsAllCourses = Wb.Sheets("allcourses")
Set rngAllCourses = WsAllCourses.Range("tblAllCourses[CourseName]")
Set LoSFAFiles = WsSFAFiles.ListObjects("tblSFAFiles")

strEBTypeFolder = "Exercise Booklet"
strEBfiletype = "EB"
strCISTypeFolder = "Classroom Information Sheet"
strCISfiletype = "CIS"
intCourseRow = rngCourse.Row - 1
strCourse = rngCourse.Value
strApp = WsAllCourses.Range("tblallcourses[application]").Rows(intCourseRow)
strPeCFldrPath = "\Cx138\training\Live\Credentialed Trainers\"
strEBFileLocation = strApp & "\" & strTypeFolder & "\" & strCourse & "_" & strEBfiletype & "*" & ".pdf"
strEBFileNm = Dir(strPeCFldrPath & "\" & strEBFileLocation)
strCISFileNm = Dir(strPeCFldrPath & "\" & strCISFileLocation)

回答by Peter S

Replace variable declaration Dimwith Public. Thus:

将变量声明替换DimPublic. 因此:

Public rngCourse as Range

Public strCourse As String

Declare them at module level.

在模块级别声明它们。