我有以下一些代码,如果它是在组合框中选择的相同区域,则将行从一个工作表复制到另一个工作表。我遇到的问题是它正在复制到第5行而不是第6行并复制列标题。你知道为什么会这样吗?我认为第四行开始“工作表(”NLE _“)。范围(”A“)......会选择第一个空行,即A6,但它会选择A5?
If chkNLE.Value = True And cboLA = "All" Then
LastRow = Worksheets("NLE_").Range("A" & Rows.Count).End(xlUp).Row *– clears any data that is already in the worksheet*
Worksheets("NLE_").Range("A6:W" & LastRow).Clear
For Each i In Sheets("NLE").Range("NLEregion") *– NLEregion is a named range for the column with region in it in the sheet when the data is being copied from*
If i.Value = cboRegion.Value Then
i.EntireRow.Copy
Worksheets("NLE_").Range("A" & Rows.Count).End(xlUp).Offset (1)
Sheets("NLE_").Visible = True
Sheets("Front Page").Visible = False
UserForm1.Hide
End If
Next i
End If
答案 0 :(得分:0)
感谢@Siddarth Rout的建议。我离开了,并研究了如何使用Autofilter来实现这一点,它比我之前的代码快得多,因为我们在瘦客户端上工作时有点慢。
这可能是一种更有效的方法,因为我是VBA的新手,但这确实有用。
If chkNLE.Value = True And cboLA = "All" And cboLA <> "" And cboRegion <> "All" Then
Application.ScreenUpdating = False 'stops the screen from updating while copying and pasting
LR = Sheets("NLE").Range("A" & Rows.Count).End(xlUp).row ' sets LR to the last cell number in A
Set rng = Sheets("NLE").Range("A5:V" & LR) ' Sets rng from A5 to the last cell in column V - includes second row of titles
With rng
.AutoFilter
.AutoFilter Field:=13, Criteria1:=cboRegion.Value 'filters on the region selected in the drop down box
.SpecialCells(xlCellTypeVisible).Copy 'copies just the filtered data
End With
Sheets("NLE_").Range("A5").PasteSpecial xlPasteAll 'pastes just the filtered data into NLE_
Sheets("NLE").AutoFilterMode = False
Application.CutCopyMode = False
Sheets("NLE_").Range("A5:V5").AutoFilter 'Adds the filter back to NLE_
Sheets("NLE").Range("A5:V5").AutoFilter
Application.ScreenUpdating = True 'allows the screen to update