我遇到将Range变量从4x1格式转换为1x4格式(4行)的问题
我到处搜索但我似乎找不到任何可行的东西。
以下是我的尝试:
Function test(periodcol2 As Range, ratecol2 As Range, X As Range)
Dim periodcol As Range
Dim ratecol As Range
If periodcol2.Columns.Count > 1 Then
periodcol.Value = Application.WorksheetFunction.Transpose(periodcol2)
ratecol.Value = Application.WorksheetFunction.Transpose(ratecol2)
End If
价值分配崩溃。
If periodcol2.Columns.Count > 1 Then
Set periodcol = Application.WorksheetFunction.Transpose(periodcol2)
Set ratecol = Application.WorksheetFunction.Transpose(ratecol2)
End If
同样的结果。
编辑:这也不起作用,相反,它会使得句号内容空白'如excel工具提示所示
Dim periodcol As Variant
Dim ratecol As Variant
If periodcol2.Columns.Count > 1 Then
periodcol = Application.WorksheetFunction.Transpose(periodcol2)
ratecol = Application.WorksheetFunction.Transpose(ratecol2)
Else
EDIT2:rate_count&在此代码的末尾,period_count等于0。
Function TEST(periodcol2 As Range, ratecol2 As Range, X As Range)
Dim periodcol As Variant
Dim ratecol As Variant
If periodcol2.Columns.Count > 1 Then
periodcol = Application.Transpose(periodcol2)
ratecol = Application.Transpose(ratecol2)
Else
Set periodcol = periodcol2
Set ratecol = ratecol2
End If
Dim period_count As Integer
Dim rate_count As Integer
period_count = periodcol.Rows.Count
rate_count = ratecol.Rows.Count
注意:我不想将值粘贴到任何位置,只需将它们保留在代码中即可。
答案 0 :(得分:0)
Sub Tester()
Dim rng As Range, arr, arr2
Set rng = Range("A1:D1") 'contents 1,2,3,4
If rng.Columns.Count > 1 Then
arr = Application.Transpose(rng.Value)
'arr is now a 2-d array (1 to 4, 1 to 1)
Debug.Print arr(1, 1), arr(2, 1), arr(3, 1), arr(4, 1)
' >>> 1 2 3 4
'EDIT: if you want a 1-D array
arr2 = Application.Transpose(arr)
Debug.Print arr2(1), arr2(2), arr2(3), arr2(4)
End If
End Sub