用于Excel的VB代码 - 变量的值

时间:2014-07-16 14:50:45

标签: vba excel-vba excel

我是一名业余程序员,并尝试完成以下任务。

我有一组编写到Excel工作簿中的指令,由用户通过“加载项”菜单启动。此特定例程旨在合并多达20个不同的用户选择文件,这意味着源文件夹文件路径对于每个文件可以是唯一的,并且使用户与用户不同。

一旦调用例程,我就可以直接分配20个字符串变量(即Dim refFile1,refFile2,refFile3,... refFile20)中的每一个的文件名和文件路径。在代码循环通过20个变量以识别哪些文件被分配后:

If refFile1 <> 'select file' then
   FileCount = FileCount + 1
End If

现在确定了For Loop运行实际合并代码所需的总时间。

问题: 如何将原始字符串值分配给&#34; refFile1&#34;什么时候通过另一个变量嵌入对变量名的引用?

我有一个For循环来启动这个过程:

For CurFile = 1 to FileCount

然后尝试回收&#39; CurFile&#39;变量以引用根文件路径/名称以打开特定工作簿:

CurRefFile = "refFile" & CurFile 'use variable to loop through 20 possible variables with base variable name "refFile" + # 
Set curWB = Workbooks(curRefFile)
curWB.Open

在上面的代码中,Excel尝试打开显式命名为&#34; refFile1&#34;的工作簿,这是变量curRefFile的最新值。

如何获得&#39;映射&#39; curRefFile的值== refFile1 == [用户的文件路径/名称],以获得正确的工作簿名称的Workbooks()函数?

谢谢。

1 个答案:

答案 0 :(得分:0)

我仍然不确定你在做什么,但我怀疑下面的代码展示了你需要的行动。

我创建了一个包含一个模块和一个用户表单的工作簿。模块中的代码是:

Option Explicit

' Declare a global variable so it can be accessed from the user form.
' The empty parentheses declare this as a uninitialised, dynamic array.
' Declaring FileName as dynamic means I can define its size at run time.
Public FileName() As String
Sub Control()

  Dim InxF As Long

  Load UserForm1
  UserForm1.Show

  ' There is no elegant method of determining if an array is unitialised.
  ' The code below needs to know if there user has exited the form without
  ' selecting any files. I think the easiest solution is for the form
  ' to declare the array with one, empty entry if no files have been
  ' specified.

  If LBound(FileName) = UBound(FileName) And _
     FileName(LBound(FileName)) = "" Then
    ' User did not select any files.
    Debug.Print "User closed form without selecting any files"
    Exit Sub
  End If

  ' Using LBound (lower bound) and UBound (upper bound) means this
  ' routine does not care how big array FileName is.
  For InxF = LBound(FileName) To UBound(FileName)
    Debug.Print FileName(InxF)
  Next

End Sub

此模块加载用户表单,然后Shows将其传递给它。一旦用户表单关闭并且控件返回到模块,表单中的任何数据都将丢失。因此,用户表单必须在退出之前保存全局变量中所需的任何数据。顶部附近的语句Public FileName() As String声明了这个全局变量。

返回时,模块会从用户表单中检查是否未选择任何文件,然后将所选文件的文件名输出到立即窗口。打开VB编辑器时,立即窗口应显示在右下方。如果缺少,请单击 Ctrl + G

用户表单显示为:

Display appearance of user form

在左边我有五个文本框名称txtFile1到txtFile5。我有一个名为lstFile的列表框,两个按钮名为cmdText和cmdList。此用户表单背后的代码是:

Option Explicit
Private Sub cmdList_Click()

  ' This routine loads the contents of the list box to global array FileName

  Dim InxItem As Long

  ' Size array FileName according to how many entries are in the list box
  ReDim FileName(0 To lstFile.ListCount)

  For InxItem = 0 To lstFile.ListCount - 1
    FileName(InxItem) = lstFile.List(InxItem)
  Next

  Unload Me

End Sub
Private Sub cmdText_Click()

  Dim InxC As Long
  Dim InxF As Long
  Dim NumFiles As Long

  ' Count number of file text boxes with values
  NumFiles = 0
  For InxC = 0 To Controls.Count - 1
    If Left(Controls(InxC).Name, 7) = "txtFile" Then
      If Controls(InxC).Text <> "" Then
        NumFiles = NumFiles + 1
      End If
    End If
  Next

  ' Size array FileName according to how many file text boxes contain values
  ReDim FileName(0 To NumFiles - 1)

  ' Copy text box values to array
  InxF = 0

  For InxC = 0 To Controls.Count - 1
    If Left(Controls(InxC).Name, 7) = "txtFile" Then
      If Controls(InxC).Text <> "" Then
        FileName(InxF) = Controls(InxC).Text
        InxF = InxF + 1
      End If
    End If
  Next

  Unload Me

End Sub
Private Sub UserForm_Initialize()

  ' This is called when user form is initialised.  This routine is used
  ' to prepare the user form for the user.

  ' I have used it to fill the text boxes and the list box with values.

  txtFile1.Text = "File 1"
  txtFile3.Text = "File 3"
  txtFile5.Text = "File 5"

  lstFile.AddItem ("File 1")
  lstFile.AddItem ("File 3")
  lstFile.AddItem ("File 5")

  ' Allow for the user closing the form without selecting any files
  ReDim FileName(1 To 1)
  FileName(1) = ""

End Sub

据我所知,你有文本框来保存用户通过点击按钮初始化的文件名。我对创建等效代码不感兴趣所以我使用了表单的初始化例程来创建一些可能的值。我经常使用一个列表框来保存像你这样的值,所以我已经展示了如何在列表框中添加行。您可以在适当的例程中准备txtFile??.Text = "?????"lstFile.AddItem ("?????"),以供用户选择。

命令按钮cmdText的单击例程将文本框的内容保存在全局数组FileName中。我说你不能在运行时构造VBA变量名。对于表单控件,情况并非如此。我的代码查看名称以txtFile开头的所有控件。代码保存它找到的任何值。

命令按钮cmdList的单击例程将列表框的内容保存在全局数组FileName中。

我希望这一切都清楚。如有必要,请回答问题,但是您可以自己理解的越多,您对VBA和用户表单的理解就越快。