简体   繁体   English

使用未分配的变量作为条目创建Excel VBA数组

[英]Creating an Excel VBA array with unassigned variables as entries

I am trying to set up a workflow/UX where a CSV file is imported and each column of the imported file is assigned a "data type" using dropdowns at the top of each column. 我试图建立一个工作流/ UX,在其中导入CSV文件,并使用每列顶部的下拉菜单为导入文件的每一列分配“数据类型”。 Once these data types are designated/assigned to each column, another macro populates a second sheet with the imported CSV data, where the location in the new sheet is dependent on the data type designated for each column of the imported data. 将这些数据类型指定/分配给每一列后,另一个宏将使用导入的CSV数据填充第二张工作表,其中新工作表中的位置取决于为导入数据的每一列指定的数据类型。

For example, if the first column of the imported data is of data type "DataA", the dropdown selection would be selected as such for this first column (from a total of 12 "data types" in the dropdown menu). 例如,如果导入数据的第一列的数据类型为“ DataA”,则将为此第一列选择下拉选择(从下拉菜单中的总共12个“数据类型”中)。 This "DataA" data would then be populated in the second sheet in its fifth column. 然后,该“ DataA”数据将填充在第二页的第五列中。

Here is the code I have so far: 这是我到目前为止的代码:

Dim DataA As Integer, DataB As Integer, DataC As Integer, DataD As Integer, DataE As Integer, DataF As Integer, DataG As Integer, DataH As Integer, DataI As Integer, DataJ As Integer, DataK As Integer, DataL As Integer

    Dim ColArray(12) As Variant

    For p = 1 To LastColImport 'This is a previously-defined/assigned variable

        q = 1

        Do While q <= 12

        If ActiveSheet.DropDowns(p).Value = q Then

            ColArray(q) = p

            Exit Do

            Else

            q = q + 1

        End If

        Loop

    Next p

This populates the ColArray array with an integer entry if the data type is selected, or an empty entry if it has not been selected. 如果选择了数据类型,则使用整数条目填充ColArray数组;如果未选择数据类型,则使用空条目填充。 The next step I want to do is assign each ColArray entry value to a named variable, so that I can call the ColArray entry values by data type name instead of having to remember or look up what data type each ColArray integer value refers to. 我要做的下一步是将每个ColArray条目值分配给一个命名变量,这样我就可以按数据类型名称调用ColArray条目值,而不必记住或查找每个ColArray整数值所指的数据类型。

I can't find a built-in "dropdown list range name" recall function anywhere, so what I would like to do is the following: 我在任何地方都找不到内置的“下拉列表范围名称”调用函数,因此我想做的是以下几点:

Dim ColArrayNames(12) As Variant

ColArrayNames(1) = DataA 'These variables were defined in the previous code block
ColArrayNames(2) = DataB

...

ColArrayNames(12) = DataL


ColArrayNames = ColArray

I realize that in this specific case, it would probably be easier to just assign the data type variables directly to the ColArray values, instead of putting them into an array and then equating the array values. 我意识到,在这种特定情况下,将数据类型变量直接分配给ColArray值,而不是将它们放入数组然后使数组值相等可能会更容易。 I feel like populating an array with unassigned variables could be useful in other cases as well. 我觉得在其他情况下,用未分配的变量填充数组也很有用。 My attempts at using this method of assigning variables have failed. 我尝试使用这种分配变量的方法失败。

After changing the last line of code to: 将代码的最后一行更改为:

For i = 1 to 12

ColArrayNames(i) = ColArray(i)

Next i

The ColArray values don't get assigned to the data type variables. ColArray值不会分配给数据类型变量。 That being said, the ColArrayNames entries are assigned the correct values, so the issue seems to be the "last step" in assigning the ColArray values to the data type variables by way of the ColArrayNames array of unassigned variables. 就是说,为ColArrayNames条目分配了正确的值,因此问题似乎是通过未分配变量的ColArrayNames数组将ColArray值分配给数据类型变量的“最后一步”。

If anyone has suggestions for how to approach this "general" problem of using arrays of unassigned variables to assign values to each array entry (while preserving the ability to call these values using the entries' "original" variable names), or if there's a more efficient way of approaching this spreadsheet function altogether, please let me know! 如果有人对如何解决使用未分配变量的数组为每个数组条目分配值的“一般”问题提出建议(同时保留了使用条目的“原始”变量名调用这些值的能力),或者是否存在一种更有效的方式来处理此电子表格功能,请让我知道!

EDIT 1: As requested by John Coleman, I'll elaborate a bit more on what I'm trying to do here. 编辑1:根据约翰·科尔曼的要求,我将在这里详细说明我要做的事情。

Once I have the imported column numbers assigned to a data type, I want to send the data to a second sheet with some code in a manner such as: 将导入的列号分配给数据类型后,我想以某种方式将数据发送到带有某些代码的第二张工作表,例如:

For i = 2 to LastRow 'The LastRow variable value will be found using a simple xlDown search process

Worksheets(2).Cells(1,i).Value = Worksheets(1).Cells(DataA,i).Value
Worksheets(2).Cells(4,i).Value = Worksheets(1).Cells(DataB,i).Value

Etc.

Next i

Again, I realize that I could just as easily use 再一次,我意识到我可以轻松地使用

Worksheets(2).Cells(1,i).Value = Worksheets(1).Cells(ColArrayNames(1),i).Value

and so on, but I feel like if what I'm asking about is possible, I might be able to use it in another situation (even if it's not the most ideal method for this example). 等等,但是我觉得如果我要问的是可能的,我也许可以在另一种情况下使用它(即使这不是该示例中最理想的方法)。

For as best as I understand your problem, it seems to come down to effectively (and efficiently) defining a data map between the original source data and the formatted output (in this case, cells on a second worksheet). 就我所知,就您的问题而言,似乎可以有效地(有效地)定义原始源数据和格式化输出(在本例中为第二个工作表上的单元格)之间的数据映射。

When confronted with the data typing part of the problem, I realized the ultimate "holder" of the destination data type is the object that holds it -- in this case its the worksheet cell, not the VBA variable or array. 当遇到问题的数据类型部分时,我意识到目标数据类型的最终“持有人”是持有它的对象-在这种情况下,它是工作表单元格,而不是VBA变量或数组。 Your focus seems to be on the VBA code that transfers/copies the data from one worksheet to another (or to/from variant arrays). 您的焦点似乎在VBA代码上,该代码将数据从一个工作表传输/复制到另一个工作表(或从/从变体数组复制/复制)。 Your own choice in efficiency should be your guide here. 您自己对效率的选择将成为此处的指南。 If you want to use relatively general purpose code for multiple data types, my suggestion is to use a Variant value or array and after copied to the destination cell, set the format of the destination cell (which effectively "types" it for whatever later use necessary). 如果要对多个数据类型使用相对通用的代码,我的建议是使用Variant值或数组,然后将其复制到目标单元格中​​,然后设置目标单元格的格式(这将为以后的使用有效地“类型化”它)必要)。

A dictionary may not even be necessary if the mapping table can be flexibly scanned to accommodate any number of data types, formats, or columns. 如果可以灵活地扫描映射表以容纳任意数量的数据类型,格式或列,则甚至不需要字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM