を行う必要があります。私はこのような何か探してカスタム関数で、このようなことをするだろう:
' -----------------------------------------------------------------
' GetMyData - picks data from sheet's first 20 rows and two columns
' - sheetName {String} - then name of the sheet where the data is taken
' - col1 {Integer} - first column to read from
' - col2 {Integer} - second column to read from
' returns: 2-dimensional array containing data of columns from the parameters
Function GetMyData(sheetName As String, col1 As Integer, col2 As Integer) As Variant
' declarations
Dim sheet As Worksheet: Set sheet = ActiveWorkbook.Sheets(sheetName)
Dim result(20, 2) As Variant
' logic
Dim row As Integer, col As Integer
For row = 1 To 20
result(row - 1, 0) = sheet.Cells(row, col1)
result(row - 1, 1) = sheet.Cells(row, col2)
Next
' return the result
GetMyData = result
End Function
をそして、あなたはそのようにそれを使用することができます。今すぐ関数に関する言葉をいくつ
Private Sub CommandButton1_Click()
' get data from Sheet3 first 20 rows columns 2(B) and 10(J)
Data = GetMyData("Sheet3", 2, 10)
End Sub
- 私たちがいることを知っているので、我々は、単一のループでそれを行うと、一度ここですべての仕事を行うことができます列の2からデータを取得したい:
For row = 1 To 20
result(row - 1, 0) = sheet.Cells(row, col1) ' Take data from col1
result(row - 1, 1) = sheet.Cells(row, col2) ' Take data from col2
Next
まあ、残念ながらそれは仕事をdoesntの、そしてそれがなかった場合でも、私のポイントは、ということですjは2つの値を持つ必要があります。このループは、私が理解しているように、第1列と第2列を含むだけです。 – Yaahtzeck
@ Yaahtzeck正しいですが、j = 2の場合は1回、j = 10の場合は1回ループを2回行うことができます。手動で配列をロードするのは非効率ですが、*動作します*。 –