Arrays 是否从列标题创建VBA数组?

Arrays 是否从列标题创建VBA数组?,arrays,excel,vba,range,Arrays,Excel,Vba,Range,我有一个导出“NewExport”,它总是随机化我收到的数据列。我需要这些列与“TheOrder”中的列顺序对齐,因此此代码将有助于重新组织导出以与我已经构建的列标题对齐 我有132列需要重新对齐,虽然我可以全部键入,但必须有一种更简单的方法与我已经创建的列标题对齐。需要注意的是,下面的代码是从另一个StackOverflow答案中无耻地复制/粘贴的 Sub OrderColumns(ByVal NewExport As Workbook, ByVal TheOrder As Worksheet

我有一个导出“NewExport”,它总是随机化我收到的数据列。我需要这些列与“TheOrder”中的列顺序对齐,因此此代码将有助于重新组织导出以与我已经构建的列标题对齐

我有132列需要重新对齐,虽然我可以全部键入,但必须有一种更简单的方法与我已经创建的列标题对齐。需要注意的是,下面的代码是从另一个StackOverflow答案中无耻地复制/粘贴的

Sub OrderColumns(ByVal NewExport As Workbook, ByVal TheOrder As Worksheet)

Dim correctOrder() As Variant
Dim lastCol As Long
Dim headerRng As Range, cel As Range
Dim mainWS As Worksheet

Set mainWS = NewExport.Worksheets("Sheet1")

'Need to figure out how to make this an array based on a Range
correctOrder() = Array(TheOrder.Range("A1:A132").Value)

With mainWS
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set headerRng = .Range(.Cells(1, 1), .Cells(1, lastCol))
End With

Dim newWS As Worksheet
Set newWS = Ninja.Sheets.Add
newWS.Name = "Rearranged Sheet"

Dim col As Long
With newWS
    For col = 1 To lastCol
        For Each cel In headerRng
            If cel.Value = correctOrder(col - 1) Then
                mainWS.Columns(cel.Column).Copy .Columns(col)
                Exit For
            End If
        Next cel
    Next col
End With

End Sub

虽然它不像我希望的那样自动化(并且需要一段硬编码),但我能够找到这样的解决方案:

Dim correctOrder(132) As Variant 
'132 will need to be changed if there's ever any more/less columns added/excluded

For i = 1 To 132
    correctOrder(i - 1) = TheOrder.Range("A" & i).Value
Next

这个解决方案为我提供了以后需要使用的阵列。

我最近为我的一个项目编写了一个“列查找器”函数

我已对其进行了修改,以满足以下要求

  • 该函数要求您传递要捕获的正确有序标题中的工作簿。您可以修改它以要求您的
    TargetWorksheet
    ,使其更具动态性
  • 该函数返回一个一维数组
  • 该函数在
    目标工作表中查找最后使用的列
    ,允许更改列标题的数量(如您自己的答案中所述,其中列编号已硬编码)

作为示例,下面是一些示例“测试”代码,以展示使用此函数的概念

您可以这样调用它,循环遍历每个元素,可能会将其他数组元素与正确的order元素进行比较,并在找到正确的order值时执行某些操作

Sub TestSub()

    Dim CorrectOrderArray As Variant
    Dim TargetCorrectOrderElement As Variant
    Dim RandomOrderArray As Variant
    Dim TargetRandomOrderElement As Variant
 
    CorrectOrderArray = CorrectOrderHeadingsArrayFunction(Workbooks("Export (4).csv"))  'Change this to target your correct workbook
    RandomOrderArray = Sheet1.Range("A1:AZ1000")  'Change this to target the correct range for your data.
    
    For Each TargetCorrectOrderElement In CorrectOrderArray
        For TargetRandomOrderElement = LBound(RandomOrderArray) To UBound(RandomOrderArray)
            If RandomOrderArray(TargetRandomOrderElement) = TargetCorrectorderValue Then
                'Do some code to write that column to your worksheet
                Exit For  'Leaves the current iteration of the random order array loop to go to the next iteration of the correct order array
            End If
        Next TargetRandomOrderElement
    Next TargetCorrectOrderElement
End Sub

这回答了你的问题吗?不幸的是,这个解决方案对我不起作用。为什么?它看起来版本相似。我最好的猜测是它没有使用Array()函数?我不确定。我找到了一个答案,但它并不像该解决方案所提供的那样自动化。如果您愿意,您可以使阵列的维度动态化
Sub TestSub()

    Dim CorrectOrderArray As Variant
    Dim TargetCorrectOrderElement As Variant
    Dim RandomOrderArray As Variant
    Dim TargetRandomOrderElement As Variant
 
    CorrectOrderArray = CorrectOrderHeadingsArrayFunction(Workbooks("Export (4).csv"))  'Change this to target your correct workbook
    RandomOrderArray = Sheet1.Range("A1:AZ1000")  'Change this to target the correct range for your data.
    
    For Each TargetCorrectOrderElement In CorrectOrderArray
        For TargetRandomOrderElement = LBound(RandomOrderArray) To UBound(RandomOrderArray)
            If RandomOrderArray(TargetRandomOrderElement) = TargetCorrectorderValue Then
                'Do some code to write that column to your worksheet
                Exit For  'Leaves the current iteration of the random order array loop to go to the next iteration of the correct order array
            End If
        Next TargetRandomOrderElement
    Next TargetCorrectOrderElement
End Sub