我是VBA的新手,我一直在使用以下代码将列重新排序为nams数组中的指定顺序。
我想要做的是不是让代码中写入的列列表从另一个工作表的范围中获取列表,以使代码更加灵活,但我不知道从哪里开始。任何帮助将不胜感激!
Dim rng As Range
Dim i As Integer
Dim J As Integer
Dim Temp
Dim nams As Variant
Dim F
Dim Dex As Integer
nams = Array("RequestCode", "Contact", "OpenDate", "ClosedDate")
Set rng = Range("A1").CurrentRegion
For i = 1 To rng.Columns.Count
For J = i To rng.Columns.Count
For F = 0 To UBound(nams)
If nams(F) = rng(J) Then Dex = F: Exit For
Next F
If F < i Then
Temp = rng.Columns(i).Value
rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value
rng(J).Resize(rng.Rows.Count) = Temp
End If
Next J
Next I
答案 0 :(得分:0)
Dim nams() As Variant
nams = Sheets("Sheet1").Range("A1:A5")
这会将指定范围的值读入你的nams()数组,其索引为1,而不是0.请记住,执行此方法总是会生成一个二维数组,所以要获得第三个值,例如你必须做nams(3,1)
之类的事情。