| すぐマク YNxv9867 | Home | Search | Contents | Gallery | Introduction | Service | Support | What's New! |
| セル範囲の値の要素(オートフィルタのリストのよう)を配列に取得するには? |
|
|
|||
こんな感じ。
Sub test()
Dim Row_1 As Integer, Row_2 As Integer, wtRow As Integer, i As Integer
Dim myStr As String, temp
Row_1 = Cells(Rows.Count, 1).End(xlUp).Row
Row_2 = Cells(Rows.Count, 2).End(xlUp).Row
Range(Cells(Row_1 + 1, 1), Cells(Row_1 + Row_2, 1)).Value = _
Range(Cells(1, 2), Cells(Row_2, 2)).Value
ReDim temp(0)
wtRow = -1
For i = 1 To Row_1 + Row_2
myStr = Cells(i, 1).Value
If Application.WorksheetFunction. _
CountIf(Range(Cells(i + 1, 1), Cells(Row_1 + Row_2 + 1, 1)), myStr) = 0 Then
wtRow = wtRow + 1
ReDim Preserve temp(wtRow)
temp(wtRow) = myStr
End If
Next i
Range(Cells(Row_1 + 1, 1), Cells(Row_1 + Row_2, 1)).ClearContents
End Sub
|
|||
|
|
|||||
|
もてもて様、ありがとうございます! なるほど!目からウロコです。 最後のイッコを取得して行けば、いちいちダブり判定する必要が無いんですね。 参考になりました! 1. 可変の Range オブジェクトに対して使用する可能性がある 2. シートに手を加えたくない 3. Rangeオブジェクトは矩形の1つの範囲に限定される ので、少しアレンジして、下記のようにしてみました。
Sub test2()
Dim wtRow As Integer, i As Integer, j As Integer
Dim myStr As String, temp
Dim myRange As Range
Set myRange = Selection '← もしくは Range(......)
ReDim temp(0)
wtRow = -1
For i = 1 To myRange.Count
myStr = myRange.Cells(i)
j = i + 1
Do While myRange.Cells(j) <> myStr And j <= myRange.Count
j = j + 1
Loop
If j = myRange.Count + 1 Then
wtRow = wtRow + 1
ReDim Preserve temp(wtRow)
temp(wtRow) = myStr
End If
Next i
End Sub
|
| http://www.happy500z.com/ | Home | Contents | Gallery | Introduction | Service | Support | What's New! |