我试图在工作表中找到最后一行作为宏的一部分。在此之前的一个步骤将大量数据复制到工作表4。当我执行此步骤时,excel错误地将最后一行标识为工作表中的某些随机行。我已经尝试过.Find
和其他无数种方法,但似乎没有任何效果。
Sub Step19MatchStrike()
ActiveWorkbook.Save
Dim ws As Worksheet
Set ws = Worksheets(4)
Dim LastRowColumnA As Long
LastRowColumnA = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("AA1").Value = "Strike Determination"
ws.Range("AA2").FormulaArray = "=IF((MATCH(E2,INDEX(Sheet3,(MATCH(A2,INDEX(Sheet3,,1),0)),5),0))>0,""To Keep"",""To Delete"")"
ws.Range("AA2").Copy ws.Range("AA3:AA" & LastRowColumnA)
ws.Columns(27).Value = ws.Columns(27).Value
End Sub
答案 0 :(得分:2)
此代码将完成您想要的
Sub Step19MatchStrike()
Dim ws As Worksheet
'~~> Change as applicable
Set ws = Sheet1
Dim lRow As Long
With ws
'~~> Find the last row in col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("AA1").Value = "Strike Determination"
'~~> Fill the formula in one go
.Range("AA2:AA" & lRow).FormulaArray = YOURFORMULA
.Columns(27).Value = .Columns(27).Value
End With
End Sub
我只是对您公式中的Sheet3
感到好奇。看起来不对
"=IF((MATCH(E2,INDEX(Sheet3,(MATCH(A2,INDEX(Sheet3,,1),0)),5),0))>0,""To Keep"",""To Delete"")"
修改
使用自动填充
Sub Step19MatchStrike()
Dim ws As Worksheet
'~~> Change as applicable
Set ws = Sheet1
Dim lRow As Long
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("AA1").Value = "Strike Determination"
.Range("AA2").FormulaArray = "=IF((MATCH(E2,INDEX(Sheet3,(MATCH(A2,INDEX(Sheet3,,1),0)),5),0))>0,""To Keep"",""To Delete"")"
.Range("AA2").AutoFill Destination:=.Range("AA2:AA" & lRow), Type:=xlFillDefault
End With
End Sub
答案 1 :(得分:0)
如何避免使用工作表索引。
Option Explicit
Sub test()
Dim ws As Worksheet
Dim LastRow As Long
With ThisWorkbook
'Refer to the worksheet using CodeName (the name appears in the VBA editor)
Set ws = ws1
'Refer to the worksheet using Name (the name appears in the Excel sheet)
Set ws = .Worksheets("Sheet1")
End With
'Create as With Statement to avoid sheet repetition
With ws
'Find the last row
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
End Sub