これはExcelの私のデータであると仮定しますData containing duplicate values in the first three columns。 最初の3つの列の値は、複数の行にわたって繰り返されています。Excel VBSで選択せずに特定の列の重複値を削除するにはどうすればよいですか?
私は自動的に私のためにこれを行うマクロを使用することを決めたと、私は重複する値を削除し、このVBSコードを発見しただけで、このスクリーンショット duplicate values are removed using a macro
のようなそれらの中に重複する値を削除します。マクロが実際に行うことは、カーソルがある選択された領域の繰り返し値を削除することです。そのため、マクロが実行されるたびに、値を削除したい領域を選択する必要があります。しかし、私が望むのは、列A、B、およびCからそれらが選択されているかどうか、列の数に関係なく重複を削除することです。そして、私はそれが自動的に開いて作業したい。
Selection()の代わりにRange()を使用すると考えました。私はSet r = Columns("A:C").Select
のようなものを置くが、それはうまくいかなかった。 VBSでこれを行う方法はありますか?
Option Explicit
Private originalValues()
Private originalRange As String
Sub removeDupes()
Dim r As Range 'target range
Dim arr() 'array to hold values
Dim i As Long, j As Long, k As Long 'loop control
Dim upper1D As Long, upper2D As Long, lower2D As Long 'array bounds
Dim s As String 'temp string to compare values
Set r = Selection.Resize(Cells.SpecialCells(xlLastCell).Row)
If r.Rows.Count = 1 Then Exit Sub 'if the target range is only 1 row then quit
arr = r.Value 'copy the values in r to the array
'store the values for an undo
originalValues = r.Value
originalRange = r.Address
upper1D = UBound(arr) 'get the upper bound of the array's 1st dimension
upper2D = UBound(arr, 2) 'get the upper bound of the array's 2nd dimension
lower2D = LBound(arr, 2) 'get the lower bound of the array's 2nd dimension
'loop through 'rows' in the array
For i = LBound(arr) To upper1D
'loop through all the 'columns' in the current row
For j = lower2D To upper2D
s = arr(i, j) 'record the current array component value in s
'Check to see if duplicates exists in the target range
If Application.CountIf(r.Columns(j), s) > 1 _
And LenB(s) Then
'Duplicate found: if the end of the array has not ye been reached then
'loop through the remaining rows for this column, clearing duplicates
If i < upper1D Then
For k = i + 1 To upper1D
If arr(k, j) = s Then arr(k, j) = ""
Next k
End If
End If
Next j
Next i
'copy array back to target range
r.Value = arr
Application.OnUndo "Undo remove duplicates", "restoreOriginalValues"
End Sub
Private Sub restoreOriginalValues()
Range(originalRange).Value = originalValues
End Sub
おかげで、 ラーレ
懸念されるところであるため、それだけで、参照のためです:http://yoursumbuddy.com/get-unique-per-row-values-removeduplicates/ –