2014-02-17 11 views
0

我創建了一個電子表格來創建清單列表。我正在寫的宏打開我的內聯網搜索頁面,在第a行輸入目錄號碼(圖中未顯示)。並將信息返回給Excel工作表。當它進入位置(RIG)時,它會在第2行中創建一個標題。我創建了第二個宏,用於刪除非活動位置。在這個過濾器之後,我剩下一個更清單的列表,但是我想刪除第2行中不再有信息的頭(AH,AI,AJ等)。我知道我可以找到工作表中最後一個使用的列,但是當row3到lastrow爲空時,我想要做的就是清除格式,邊框和row2中的內容。如何從查找最後一列功能中排除特定行

有沒有辦法調整這個以排除搜索中的第2行?

Dim LastColumn As Integer 

Set LastColumn = Cells.Find(What:="*", After:=[A1], _ 
SearchOrder:=xlByColumns, _ 
SearchDirection:=xlPrevious).Column 

Cells(2,LastColumn).Select 
ActiveCell.Offset(0,1) 
Do 
If not ActiveCell = "" Then 
    ActiveCell.EntireColumn.Delete(xlToLeft) 
    DoEvents 
Else 
    Exit Do 
End If 
Loop 
結合加里的學生響應之後

My Search SHeet

終極密碼。謝謝!!

'Find last used column below header row 
Dim wf As WorksheetFunction 
Dim N As Long 
Dim rCol As Range 

Set wf = Application.WorksheetFunction 
Cells(1, 1).EntireRow.Hidden = True 
Cells(2, 1).EntireRow.Hidden = True 

Do 
For N = Columns.Count To 1 Step -1 
    Set rCol = Cells(1, N).EntireColumn 
    If wf.Subtotal(103, rCol) > 0 Then 
     Exit Do 
    End If 
Next 
Loop 

Cells(1, 1).EntireRow.Hidden = False 
Cells(2, 1).EntireRow.Hidden = False 

'Trim header row to used columns only 
Cells(2, N).Select 
ActiveCell.Offset(0, 1).Select 

Do 
    If Not ActiveCell = "" Then 
     ActiveCell.EntireColumn.Delete (xlToLeft) 
     DoEvents 
    Else 
     Exit Do 
    End If 
Loop 

回答

1

只排除該行從檢查:

Sub FindLastColumn() 
    Dim BadRow As Long, wf As WorksheetFunction 
    Dim N As Long, rCol As Range, i As Long 
    BadRow = 7 
    i = 103 
    Set wf = Application.WorksheetFunction 
    Cells(BadRow, 1).EntireRow.Hidden = True 
    For N = Columns.Count To 1 Step -1 
     Set rCol = Cells(1, N).EntireColumn 
     If wf.Subtotal(i, rCol) > 0 Then 
      MsgBox "The last used column is: " & N 
      Cells(BadRow, 1).EntireRow.Hidden = False 
      Exit Sub 
     End If 
    Next N 
    Cells(BadRow, 1).EntireRow.Hidden = False 
End Sub 
+0

非常好,我不知道「.entirerow.hidden = true」。這將隱藏它在工作表上可見,這是否意味着查找功能將無法看到它? – TPJ87

0

所以從我能理解你正在嘗試做一個搜索,並從搜索中排除某些行基本?爲什麼不使用雙循環?

for i 1 to columns you want 
    for j = 1 to rows you want 
     if j <> row you don't want 
      'add code here 
     end if 
    nextj 
next i 
+0

因爲列的長度會發生變化。我試圖使用查找功能找到「你想要的列」數字。 – TPJ87

相關問題