Here's a re-write of your code, warts (as mentioned in my comment above) and all, meaning no improvement of its functionality.
Sub Test_2()
' declare row numbers as Long because the Integer data type can't
' hold the value of the last row number in a sheet
Dim R As Long ' loop counter: Row
Dim Rng As Range ' loop object:
Dim Rcount As Long ' Rows: count
Dim Ccount As Long ' Columns: count
' No need to Select anything
Sheet2.Columns("A:C").ClearContents
' use a For / Next loop to call up each row number
' Next R advances R to the next integer
For R = 1 To Sheet1.Range("A1", Range("A1").End(xlDown)).Rows.Count
' (not useful but instructive)
Sheet1.Range("E1").Value = Sheet1.Cells(R, "A").Value
' Range("K4") is difficult to handle in a loop.
' Better use Cells(4, "K") and better still, Cells(4, 11)
' where both 4 and 11 can be calculatable variables.
' Here the range is fixed to K4 and can't change in the loop.
Ccount = Sheet1.Range("K4").End(xlToRight).Column - Columns("K").Column + 1
Rcount = Sheet1.Range("K4").End(xlDown).Row - Range("K4").Row + 1
Set Rng = Sheet1.Cells(4, "K").Resize(Rcount, Ccount)
' Debug.Print Rng.Address ' check the address of the range created
Rng.Copy Destination:=Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Offset(1)
Next R
End Sub
Now, having dispensed with Select
, please give your attention to the With
statement. It further simplifies the code by avoiding repetitions of qualifiers. Using that technique the last line of the above procedure would appear as shown below. The repetitive "Sheet2" is replaced with a leading period on each use.
With Sheet2
Rng.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
End With
I'm not confident that you really want to use xlDown in determining the range dimensions. I did change it to xlUp in determining the destination cell and would change it for setting the end of the For / Next loop, too because as the code is now it will fail (because of the xlDown) if A1 is blank. Better read up on the difference between xlDown and xlUp in this context. As a hint, xlDown looks from the start cell downward until it finds a blank, while xlUp looks from the start cell upward to find a non-blank. So, xlDown will find the first blank cell following the start cell and return the cell above that, while xlUp will find the first non-blank cell (usually from the last row in the sheet) and return it. Similarly, for xlToLeft and xlToRight.