Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:25:50+00:00 2026-06-06T18:25:50+00:00

VBA is throwing the above given error on the line Sheets(Sheet1).Range(A & i).Copy Destination:=Sheets(Sheet2).Range(A

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:25:51+00:00Added an answer on June 6, 2026 at 6:25 pm

    You are missing a “:” before “A”

    Range("A" & i & ":A" & LastCol - 1)
    

    FOLLOWUP

    After I went through your comments, I saw lot of errors in your code

    1) You have dimmed i as Integer. This can give you an error in Excel 2007 onwards if your last row is beyond 32,767. Change it to Long I 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

    Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647

    2) You are finding the Last Column But in Which Sheet? You have to fully qualify the path Like this.

        If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
           LastCol = Cells.Find(What:="*", After:=[A1], _
           SearchOrder:=xlByColumns, _
           SearchDirection:=xlPrevious).Column
        End If
    

    Same is the case with

    With Sheets("Sheet1")
        Set DatesRange = Range("B2" & LastCol)
    End With
    

    You are missing a DOT before Range

    This is the correct way…

    .Range("B2....
    

    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 LastColumn but using LastCol. I would strongly advise using Option Explicit I 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 instead

        If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
           LastCol = Cells.Find(What:="*", After:=[A1], _
           SearchOrder:=xlByColumns, _
           SearchDirection:=xlPrevious).Column
        Else
            MsgBox "No Data Found"
            Exit Sub
        End If
    

    5) ActiveSheet.Rows.Count will 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

    With Sheets("Sheet")
        LastRow =.Range("A" & .Rows.Count).End(xlup).row
    End With
    

    Now use LastRow instead of ActiveSheet.Rows.Count You also might want to use a For Loop so that you don’t have to increment i every time. For example

    For i = 1 to LastRow
    

    6) 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.

    Option Explicit
    
    Sub FindFill()
        Dim wsI As Worksheet, wsO As Worksheet
        Dim DatesRange As Range
        Dim LastCol As Long, LastRow As Long
    
        If Application.WorksheetFunction.CountA(Sheets("Sheet1").Cells) = 0 Then
            MsgBox "No Data Found"
            Exit Sub
        End If
    
        Set wsI = Sheets("Sheet1")
        Set wsO = Sheets("Sheet2")
    
        With wsI
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            Set DatesRange = .Range("B1:" & Split(Cells(, LastCol).Address, "$")(1) & 1)
    
            .Columns(1).Copy wsO.Columns(1)
            DatesRange.Copy
            wsO.Range("B2").PasteSpecial xlPasteValues, _
            xlPasteSpecialOperationNone, False, True
    
            .Range("B2:" & Split(Cells(, LastCol).Address, "$")(1) & LastCol).Copy
            wsO.Range("C2").PasteSpecial xlPasteValues
        End With
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using VBA, is it possible to get the size of a given range in
VBA newbie over here. I'm trying to use an array formula through excel vba
in VBA i am creating a URL: URL = http://api.local.yahoo.com/MapsService/V1/geocode?appid= & yahoo & &street=
Using VBA I want to send a copy of the current word document to
Using VBA with a reference to Microsoft Scripting Runtime. I'm trying to compare filenames
I have the following line in Excel VBA: ActiveCell.Offset(r, 1).Value = IIf(rs2.Fields(SalesRelatedCallsQTD).Value = 0,
I am trying to determine if a selected range is within a set area...
The VBA I'm trying to write is fairly simple but Ive never written VBA
I want to have VBA select a Range, covering a constant number of columns
I am using VBA for Excel 2010 and randomly receiving the following error: Run-time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.