我编写了一个代码,如果只有第一个单元格包含" X",则从另一个工作表导入一些行。问题是它只适用于第一行(例如:它导入行101和102,即使它在第一个单元格中不包含" X")。有人可以在下面的代码中检测到我的错误吗?
Public Sub refresh()
'
' refresh Macro
'
' Touche de raccourci du clavier: Ctrl+y
'
Dim ws1 As Worksheet, ws2 As Worksheet, lr1 As Long, lRow As Long
Set ws1 = ThisWorkbook.Worksheets("Scénarios de menace")
Set ws2 = ThisWorkbook.Worksheets("Analyse de risque")
Application.Calculation = xlCalculationAutomatic
ws2.Range("B6:AP" & ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row).Clear
lr1 = ws1.Cells(ws1.Rows.Count, 2).End(xlUp).Row
Sheets("Scénarios de menace").Select
Range("B4:Z398").Select
Selection.Copy
Sheets("Analyse de risque").Select
Range("B6:Z400").Select
ActiveSheet.Paste
ws1.Range("A1:A" & lr1).AutoFilter Field:=1, Criteria1:="x"
ws1.Range("B3:AP" & lr1).SpecialCells(xlCellTypeVisible).Copy
ws2.Range("B6").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
ws1.Range("A6:A" & lr1).AutoFilter
ws2.Activate: ws2.Cells(1, 1).Activate
End Sub
答案 0 :(得分:0)
也许这段代码可以帮到你:
Public Sub Test()
Dim intLastCell As Integer
Dim wsTable1 As Worksheet
Dim wsTable2 As Worksheet
'Worksheets
Set wsTable1 = ThisWorkbook.Worksheets(2)
Set wsTable2 = ThisWorkbook.Worksheets(3)
'Check if Autofilter is on
If wsTable1.AutoFilterMode = True Then
wsTable1.AutoFilterMode = False
End If
'Search for last cell on column 'B'
intLastCell = wsTable1.Cells(Rows.Count, 2).End(xlUp).Row
'Apply autofilter
wsTable1.Range("A1:B" & intLastCell).AutoFilter Field:=1, Criteria1:="x"
'Select and Copy the results
Range("A1:B" & intLastCell).Select
Selection.Copy
'Activate the second table and paste the results
wsTable2.Activate
Cells(1, 1).Select
ActiveSheet.Paste
'Disable autofilter
wsTable1.AutoFilterMode = False
End Sub