1

出力を変更するたびに VBA コードを変更する必要がないように、出力変数をソフト コード化しようとしています。

これは動作するコードです

Sub Working()
    Dim cat(1 To 10)
    Dim bat(1 To 10)

    For i = 1 To 10
        cat(i) = i * 10
        bat(i) = i * 5
    Next i

    Sheet2.Range("A2:A11") = Application.Transpose(cat())
    Sheet2.Range("B2:B11") = Application.Transpose(bat())
End Sub

これは私が書きたい理想的な方法ですが、うまくいきません

Sub not_working()
    Dim cat(1 To 10)
    Dim bat(1 To 10)

    For i = 1 To 10
        cat(i) = i * 10
        bat(i) = i * 5
    Next i

    a = 3
    Do While Sheet1.Cells(a, 1) <> ""
        OutVar = Sheet1.cells(a, 1) & "()" 
        Sheet3.Range( _
            Cells(2, a - 2).Address, Cells(11, a - 2).Address _
        ) = Application.Transpose(Outvar)
        a = a + 1
    Loop
End Sub

' Sheet1.cells(3,1) = cat - these cells contain the variable names
' Sheet1.cells(4,1) = bat - these cells contain the variable names

そうすることが可能であれば、誰かが提案してもらえますか?

4

1 に答える 1

-1

あなたの要件を正しく理解していれば、不規則な配列がそれを満たします。

Variant 型の変数がある場合、その変数を整数、実数、文字列、ブール値、配列などに設定できます。

Variant 型の配列がある場合、その配列の各要素を異なる型の値に設定できます。

以下のコードでは、バリアント配列 Main を使用しています。私は設定しました:

  • Main(0) を 1D 配列に、
  • Main(1) をより大きな 1D 配列に、
  • Main(2) を 2D 配列に、
  • Main(3) を単一の整数に、
  • ワークシートの使用範囲への Main(4)。

これは、各要素のサイズが異なるため、不規則配列と呼ばれます。

配列に値をロードしたら、汎用ルーチンを使用して Main の各要素をその性質に従って出力します。

200 ~ 300 個の変数のそれぞれが Main の要素になります。

私のコードを見てください。これは、バリアント配列で実現できることの簡単な紹介にすぎません。私が正しい方向に向かっていると思うが、十分に進んでいないと思われる場合は、質問に戻ってください.

Option Explicit
Sub DemoRaggedArray()

  Dim InxDim As Long
  Dim InxMain As Long
  Dim InxWCol As Long
  Dim InxWRow As Long
  Dim Main() As Variant
  Dim NumOfDim As Long
  Dim Work() As Variant

  ReDim Main(0 To 5)

  Work = Array(1, "A", True)

  Main(0) = Work

  Main(1) = Array(2, "B", False, 1.2)

  ReDim Work(1 To 2, 1 To 3)

  Work(1, 1) = 1
  Work(1, 2) = 2.5
  Work(1, 3) = DateSerial(2012, 12, 27)
  Work(2, 1) = True
  Work(2, 2) = "String"

  Main(2) = Work

  Main(3) = 27

  ' Cells A1:C4 of the worksheet have been set to their addresses
  Main(4) = WorksheetFunction.Transpose(Worksheets("Sheet2").UsedRange.Value)

  For InxMain = LBound(Main) To UBound(Main)
    Debug.Print "Type of Main(" & InxMain & ") is " & VarTypeName(Main(InxMain))
    Select Case VarType(Main(InxMain))
      Case vbEmpty, vbNull
        ' No value
      Case Is >= vbArray
        ' Array
        NumOfDim = NumDim(Main(InxMain))
        Debug.Print "  Main(" & InxMain & ") is dimensioned as: (";
        For InxDim = 1 To NumOfDim
          Debug.Print LBound(Main(InxMain), InxDim) & " To " & _
                                                  UBound(Main(InxMain), InxDim);
          If InxDim < NumOfDim Then
            Debug.Print ", ";
          End If
        Next
        Debug.Print ")"
        Select Case NumOfDim
          Case 1
            For InxWCol = LBound(Main(InxMain)) To UBound(Main(InxMain))
              Debug.Print "  (" & InxWCol & ")[" & _
                                        VarTypeName(Main(InxMain)(InxWCol)) & "]";
              Select Case VarType(Main(InxMain)(InxWCol))
                Case vbEmpty, vbNull, vbArray
                  ' No code to handle these types
                Case Else
                  Debug.Print "=" & Main(InxMain)(InxWCol);
              End Select
            Next
            Debug.Print
          Case 2
            For InxWRow = LBound(Main(InxMain), 2) To UBound(Main(InxMain), 2)
              For InxWCol = LBound(Main(InxMain), 1) To UBound(Main(InxMain), 1)

                Debug.Print "  (" & InxWCol & "," & InxWRow & ")[" & _
                             VarTypeName(Main(InxMain)(InxWCol, InxWRow)) & "]";
                Select Case VarType(Main(InxMain)(InxWCol, InxWRow))
                  Case vbEmpty, vbNull, vbArray
                    ' No code to handle these types
                  Case Else
                    Debug.Print "=" & Main(InxMain)(InxWCol, InxWRow);
                End Select
              Next
              Debug.Print
            Next
          Case Else
            Debug.Print "  There is no display code for this number of dimensions"
        End Select
      Case Else
        ' Single variable
        Debug.Print "  Value = " & Main(InxMain)
    End Select
  Next

End Sub
Public Function NumDim(ParamArray TestArray() As Variant) As Integer

  ' Returns the number of dimensions of TestArray.

  ' If there is an official way of determining the number of dimensions, I cannot find it.

  ' This routine tests for dimension 1, 2, 3 and so on until it get a failure.
  ' By trapping that failure it can determine the last test that did not fail.

  ' Coded June 2010. Documentation added July 2010.

  ' *  TestArray() is a ParamArray because it allows the passing of arrays of any type.
  ' *  The array to be tested in not TestArray but TestArray(LBound(TestArray)).
  ' *  The routine does not validate that TestArray(LBound(TestArray)) is an array.  If
  '    it is not an array, the routine return 0.
  ' *  The routine does not check for more than one parameter.  If the call was
  '    NumDim(MyArray1, MyArray2), it would ignore MyArray2.

  Dim TestDim                   As Integer
  Dim TestResult                As Integer

  On Error GoTo Finish

  TestDim = 1
  Do While True
    TestResult = LBound(TestArray(LBound(TestArray)), TestDim)
    TestDim = TestDim + 1
  Loop

Finish:

  NumDim = TestDim - 1

End Function
Function VarTypeName(Var As Variant)

  Dim Name As String
  Dim TypeOfVar As Long

  TypeOfVar = VarType(Var)

  If TypeOfVar >= vbArray Then
    Name = "Array of type "
    TypeOfVar = TypeOfVar - vbArray
  Else
    Name = ""
  End If

  Select Case TypeOfVar
    Case vbEmpty
      Name = Name & "Uninitialised"
    Case vbNull
      Name = Name & "Contains no valid data"
    Case vbInteger
      Name = Name & "Integer"
    Case vbLong
      Name = Name & "Long integer"
    Case vbSingle
      Name = Name & "Single-precision floating-point number"
    Case vbDouble
      Name = Name & "Double-precision floating-point number"
    Case vbCurrency
      Name = Name & "Currency"
    Case vbDate
      Name = Name & "Date"
    Case vbString
      Name = Name & "String"
    Case vbObject
      Name = Name & "Object"
    Case vbError
      Name = Name & "Error"
    Case vbBoolean
      Name = Name & "Boolean"
    Case vbVariant
      Name = Name & "Variant"
    Case vbDataObject
      Name = Name & "Data access object"
    Case vbDecimal
      Name = Name & "Decimal"
    Case vbByte
      Name = Name & "Byte"
    Case vbUserDefinedType
      Name = Name & "Variants that contain user-defined types"
    Case Else
      Name = Name & "Unknown type " & TypeOfVar
  End Select

  VarTypeName = Name

End Function

DemoRaggedArrayからの出力

Type of Main(0) is Array of type Variant
  Main(0) is dimensioned as: (0 To 2)
  (0)[Integer]=1  (1)[String]=A  (2)[Boolean]=True
Type of Main(1) is Array of type Variant
  Main(1) is dimensioned as: (0 To 3)
  (0)[Integer]=2  (1)[String]=B  (2)[Boolean]=False  (3)[Double-precision floating-point number]=1.2
Type of Main(2) is Array of type Variant
  Main(2) is dimensioned as: (1 To 2, 1 To 3)
  (1,1)[Integer]=1  (2,1)[Boolean]=True
  (1,2)[Double-precision floating-point number]=2.5  (2,2)[String]=String
  (1,3)[Date]=27/12/2012  (2,3)[Uninitialised]
Type of Main(3) is Integer
  Value = 27
Type of Main(4) is Array of type Variant
  Main(4) is dimensioned as: (1 To 3, 1 To 4)
  (1,1)[String]=A1  (2,1)[String]=B1  (3,1)[String]=C1
  (1,2)[String]=A2  (2,2)[String]=B2  (3,2)[String]=C2
  (1,3)[String]=A3  (2,3)[String]=B3  (3,3)[String]=C3
  (1,4)[String]=A4  (2,4)[String]=B4  (3,4)[String]=C4
Type of Main(5) is Uninitialised

私の国のデフォルトの日付形式であるため、日付が「27/12/2012」と表示されていることに注意してください。このコードを実行すると、お住まいの国のデフォルト形式で表示されます。

于 2012-12-27T19:59:31.757 に答える