ワークシートdataWS
にデータがあり、列Aに行番号、列Bにデータがあると仮定します。
from
別のワークシートで、値をA1
とto
に入力しますB1
次に、でA2
=IF($A1<$B$1,$A1+1,"")
、この行に表示する行番号を表示します。
次に、でB2
データを取得します=IF($A2="","",VLOOKUP($A2,dataWS!A:B,2,FALSE))
。数式をA2:B2
下の行にコピーすると、設定が完了します。
VBAでそれを実行したい場合は、次のコードを使用できます(データ内のIDが正しく順序付けられていることを前提としています)。また、新しいデータを書き込む前にターゲット領域が空にならないことにも注意してください。
Public Sub getData()
Dim currentId As Long
Dim toId As Long
Dim wsTarget As Worksheet
Dim targetIdCol As Integer
Dim targetDataCol As Integer
Dim wsSource As Worksheet
Dim sourceIdCol As Integer
Dim sourceDataCol As Integer
Dim readRow As Long
Dim writeRow As Long
Set wsTarget = ThisWorkbook.Worksheets("Sheet2") ' name of the target worksheet
targetIdCol = 1 'number of the column where the ids are to be written (a=1)
targetDataCol = 2 'number of the column where the data is to be written
Set wsSource = ThisWorkbook.Worksheets("Sheet1") ' name of the source data worksheet
sourceIdCol = 1 'number of the column where the ids are to be read(a=1)
sourceDataCol = 2 'number of the column where the data is to be read
currentId = wsTarget.Range("A1").Value 'cell in which the from is specified (here "A1" of target worksheet)
toId = wsTarget.Range("B1").Value 'cell in which the to is specified (here "B1" of target worksheet)
readRow = 1 'row at which the data should start to be read
writeRow = 2 'row at which the data should start to be written
While (wsSource.Cells(readRow, sourceIdCol) <> "") And (currentId <= toId)
If currentId = wsSource.Cells(readRow, sourceIdCol).Value Then
wsTarget.Cells(readRow, targetIdCol) = wsSource.Cells(readRow, sourceIdCol).Value
wsTarget.Cells(readRow, targetDataCol) = wsSource.Cells(readRow, sourceDataCol).Value
readRow = readRow + 1
Else
If currentId > wsSource.Cells(readRow, sourceIdCol).Value Then
readRow = readRow + 1
Else
currentId = currentId + 1
End If
End If
Wend
End Sub