我创建了一个用于创建库存清单的电子表格。我正在编写的宏打开我的内部网搜索页面,在第a行输入目录号(图中未显示)。并将信息返回到Excel工作表。当它进入位置(RIG)时,它在第2行创建一个标题。我创建了第二个宏来删除非活动位置。在这个过滤器之后,我留下了一个更干净的清单,但我想删除第2行中不再有信息的标题(即AH,AI,AJ等列)。我知道我可以找到工作表的最后一个使用的列,但我想要做的是在row3到lastrow为空时在第2行清除格式,边框和内容。
有没有办法调整它以从搜索中排除row2?
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
加入Gary的学生回应后的最终守则。谢谢!!
'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
答案 0 :(得分: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
答案 1 :(得分: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