VBA is throwing the above given error on the line Sheets("Sheet1").Range("A" & i).Copy Destination:=Sheets("Sheet2").Range("A" & i & "A" & LastCol - 1)
What I am trying to do is actually to copy the "A" & i cell (in first iteration it’s A2) to a range in the second worksheet named Sheet2.
Sub FindFill()
Dim DatesRange As Range
Dim i As Integer
Dim TransposeThis As Range
Dim LastCol As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
With Sheets("Sheet1")
Set DatesRange = Range("B2" & LastCol)
End With
i = 1
Do While i <= ActiveSheet.Rows.Count
Sheets("Sheet1").Range("A" & i + 1).Copy Destination:=Sheets("Sheet2").Range("A" & i & "A" & LastCol - 1)
i = i + 1
Loop
End
End Sub
You are missing a “:” before “A”
FOLLOWUP
After I went through your comments, I saw lot of errors in your code
1) You have dimmed
iasInteger. This can give you an error in Excel 2007 onwards if your last row is beyond 32,767. Change it toLongI would recommend having a look at this link.Topic: The Integer, Long, and Byte Data Types
Link: http://msdn.microsoft.com/en-us/library/aa164754%28v=office.10%29.aspx
Quote from the above link
2) You are finding the Last Column But in Which Sheet? You have to fully qualify the path Like this.
Same is the case with
You are missing a DOT before
RangeThis is the correct way…
Also
Range("B2" & LastCol)will not give you the range that you want. See the code below on how to create your range.3) You are using a variable
LastColumnbut usingLastCol. I would strongly advise usingOption ExplicitI would also recommend having a look at this link (SEE POINT 2 in the link).Topic: To ‘Err’ is Human
Link: http://www.siddharthrout.com/2011/08/01/to-err-is-human/
4) What happens if there
.CountA(Sheets("Sheet1").Cells) = 0? 🙂 I would suggest you this code instead5)
ActiveSheet.Rows.Countwill not give you the last active row. It will give you the total number of rows in that sheet. I would recommend getting the last row of Col A which has data.You can use this for that
Now use
LastRowinstead ofActiveSheet.Rows.CountYou also might want to use aFor Loopso that you don’t have to incrementievery time. For example6) Finally You should never use
End. Reason is quite simple. It’s like Switching your Computer using the POWER OFF button. The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Also the Object references held (if any) by other programs are invalidated.7) Based on your image in Chat, I believe you are trying to do this? This uses a code which doesn’t use any loops.