【问题标题】:dim and set a variable across all modules excel vba暗淡并在所有模块excel vba中设置一个变量
【发布时间】:2017-03-21 14:24:07
【问题描述】:

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

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

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)

【问题讨论】:

标签: vba excel macros


【解决方案1】:

将变量声明Dim 替换为Public。因此:

Public rngCourse as Range

Public strCourse As String

在模块级别声明它们。

【讨论】:

  • 谢谢,我通过使用 Public 声明变量解决了这个问题。然后我创建了一个子过程名称“DefineVariables”来定义整个项目中的所有公共变量。在我调用项目中的任何其他子程序之前,我首先调用“DefineVariables”子程序,以便声明和设置所有变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多