Excel VBA Debug: Loop not searching through the whole range

拟墨画扇 提交于 2019-12-24 04:19:13

问题


I wrote a basic macro to search through a range (in one sheet) and then copy the selected cell (to another sheet) based on a 3rd sheet holding the selection values. I've used the loop as for i = x to y bu it looks like the macro is skipping some rows !???! i.e. If there are 4 valid values to be copied on rows 1 to 4, the macro only copies values from rows 2 and 4 (ditching the valid cells 1 and 3). Here's the code:

Sub XXXXX()
Dim i As Integer
Dim PasteSheet As Worksheet: Set PasteSheet = Sheets("Sheet1")
Dim CopySheet As Worksheet: Set CopySheet = Sheets("Sheet2")
Dim SearchSheet As Worksheet: Set SearchSheet = Sheets("Sheet3")
Dim LookupID, LookupID_SearchRange, CopyValueID, CopyValueID_Paste As Range

For i = 7 To 2000 'I've also used the (Step 1) option with no success
RowCount = Application.WorksheetFunction.CountA(PasteSheet.Range("A:A")) + 1 'finds the last cell to be used for copy

Set LookupID = CopySheet.Range("A" & i) 'searches all values within column A from row 7 to 2000
Set LookupID_SearchRange = SearchSheet.Range("A:A") ' Seaches if the values from Sheet3 are present in Sheet 1
Set CopyValueID = CopySheet.Range("X" & i) 'counter that follows the same search on A but selects values on X
Set CopyValueID_Paste = PasteSheet.Range("A" & RowCount) 'When it finds the ID, it copies some cell to the last row in Sheet2
      ' Initially  there was an additional RowCount (+1) for CopyValueID. Corrected based on answers for future refrence of the cleaned code.
If Not IsError(Application.Match(LookupID, LookupID_SearchRange, 0)) Then
    If CopyValueID.Value <> "" Then
        CopyValueID.Copy
        CopyValueID_Paste.PasteSpecial xlPasteValues
    End If
End If
Next i

End Sub

Why does the code selects and copies values from rows 2 and 4 (it seams like it's using a +1 step?) Thanks.


回答1:


You are defining RowCount as CountA + 1:

RowCount = Application.WorksheetFunction.CountA(PasteSheet.Range("A:A")) + 1 'finds the last cell to be used for copy

then you actually add one again in the row you are pasting:

Set CopyValueID_Paste = PasteSheet.Range("A" & RowCount + 1) ' When it finds the ID, it copies some cell to the last row in Sheet2

I think you are therefore overwriting your results? Removing one of the +1's should do the trick.

Editorial: Happy to see someone defining the ranges they copy to and from, rather than using a bunch of Select's... definitely better coding style!




回答2:


I think you're adding a +1 to the RowCount in two places.

...
RowCount = Application.WorksheetFunction.CountA(PasteSheet.Range("A:A")) + 1
...
Set CopyValueID_Paste = PasteSheet.Range("A" & RowCount + 1) '<-- Remove the +1 here

A few other notes - nice code overall --------------------------

  • As pointed out in the comments, there is a pretty agreed upon best practice to find the RowCount: RowCount = Range("A" & .Rows.Count).End(xlUp).Row + 1. This will solve the problem of possible blank cells in the column, which would throw off the COUNTA.

  • You can do this without COPY and PASTE with CopyValueID_PASTE.value = CopyValueID.Value. It will make a huge efficiency difference if you are doing this over a lot of rows.



来源:https://stackoverflow.com/questions/19815763/excel-vba-debug-loop-not-searching-through-the-whole-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!