2014-01-25 16 views
1

我是新來vba編程和目前正在研究以下內容:數據刪除匹配項的字符串在VBA

  1. 記錄屬性名稱,並將其記錄到AttributeName()
  2. 要求用戶選擇目標/從屬變量
  3. 刪除目標從AttributeName()

我有問題進行步驟3的屬性(I使用Filter().Delete

Dim DataRange As Range 
Dim nrow As Long 
Dim ncol As Long 

Range("A2").Select 
Range(Selection, Selection.End(xlDown)).Select 
Range(Selection, Selection.End(xlToRight)).Select 

Set DataRange = Selection 
nrow = DataRange.Rows.Count 
ncol = DataRange.Columns.Count 

Dim AttributeName() As String 
For i = 1 to ncol 
     ReDim Preserve AttributeName(i) 
    AttributeName(i) = Sheets("data").Cells(1,i).Value 
Next i 


Dim TargetRange as Range 
    Dim Target as String 
Set TargetRange = Application.InputBox("Please highlight the cell for TARGET", Type:=8) 
Target = TargetRange.Value 

我的想法是:
If AttributeName(i) = Target Then ...

謝謝你的溫柔的幫助。

回答

0

如果你打算從AttributeName刪除元素,考慮如何使用收集link 1link 2),而不是數組:

Dim AttributeName As New Collection 
For i = 1 To ncol 
    AttributeName.Add Sheets("data").Cells(1, i).Value 
Next i 

Dim TargetRange As Range 
Dim Target As String 
Set TargetRange = Application.InputBox("Please highlight the cell for TARGET", Type:=8) 
Target = TargetRange.Value 

With AttributeName 
    For i = .Count To 1 Step -1 
     If .Item(i) = Target Then .Remove (i) 
    Next 
End With 

另外一件重要的事情,會是什麼如果用戶在輸入框中按「取消」?你也應該處理這個。

變化

Set TargetRange = Application.InputBox("Please highlight the cell for TARGET", Type:=8) 

On Error Resume Next 
Set TargetRange = Application.InputBox("Please highlight the cell for TARGET", Type:=8) 
On Error Goto 0 

If TargetRange Is Nothing Then 
    MsgBox "User has pressed cancel" 
    Exit Sub 
End If 
+1

Simoco嗨!非常感謝!
它很完美!採集! – useR

相關問題