如何在Excel VBA中定义动态变量?

时间:2019-04-24 14:12:59

标签: excel vba

我是moment.utc(event.startDate).format("HH:mm") Excel的初学者,我正在尝试为自己的论文做一个应用程序,该应用程序必须在此过程中处理excel文件。

基本上,我将有一个由许多工作表组成的工作簿,我想有效地保存一些数据,仅使用一个FOR来节省一些时间。问题是我不知道将要导入的工作表的名称(已经完成了此导入部分)。

因此,我想保存发现一些数据的列(字符串“ Create”和“ Delete”),但是我无法将它们分配给静态变量,因为它们将被覆盖。所以我的想法是根据工作表的名称将它们放在动态变量上,但是我不知道该怎么做。

我的第一次尝试是以下操作,但是代码不起作用... VBAws.Name & c = w给出了错误。

ws.Name & d = w

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

使用字典的示例

Dim dict As Object
Dim dictKey As String

Set dict = CreateObject("Scripting.Dictionary")

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Sheet1" And ws.Name <> concepts Then
        For w = 1 To ws.Name.Cells(1, Columns.Count).End(xlToLeft).Column
            If ws.Name.Cells(1, w).Value = Create Then
                dictKey = ws.Name & c
            ElseIf ws.Name.Cells(1, w).Value = Delete Then
                dictKey = ws.Name & d
            End If

            If Not dict.exists(dictKey) Then
                dict.Add dictKey, w
            Else
                MsgBox dictKey & " already exists"
            End If
         Next w
    End If
Next ws