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 7623175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:38:28+00:00 2026-05-31T04:38:28+00:00

I am new to VBA. I have written this code inside a button click

  • 0

I am new to VBA. I have written this code inside a button click event in sheet called “Outputs”.
The problem is when I try to copy data form TestInput sheet to Calculator sheet I get a run time error 1004.
My code is shown below

 Do While currentInd < lastRows
   With Worksheets("TestInput")
    Worksheets("Calculator").Range(Cells(3, "C"), Cells(3, (lastColumns))).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value

  End With

If I replace 3rd line of my code with Worksheets(“Calculator”).Range(“C3:FC3”).Value = .Range(.Cells(currentInd, “A”), .Cells(currentInd, lastColumns)).Value

then Its working fine but “lastColumns” is a variable which would change, so I dont want to fix my range to “C3:FC3”

  • 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-05-31T04:38:29+00:00Added an answer on May 31, 2026 at 4:38 am

    Unless “Calculator” is the current active sheet, you need to fully specify the location of the cells:

    With Worksheets("TestInput")
        Worksheets("Calculator").Range(Worksheets("Calculator").Cells(3, "C"), Worksheets("Calculator").Cells(3, lastColumns)).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value
    End With
    

    Or to make it a little more readable:

    Dim shCalc as WorkSheet
    Set shCalc = Worksheets("Calculator")
    With Worksheets("TestInput")
        shCalc.Range(shCalc.Cells(3, "C"), shCalc.Cells(3, lastColumns)).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value
    End With
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
We have a lot of VBA code in spreadsheets and a lot of time
I have this code that I am trying to copy a record with in
I am new to VBA and want to add two values. The code I
I'm new to VBA and I'm trying this for loop out, but I think
I'm very new to VBA and I'm stuck with a problem that probably has
I am new to Active Directory. I have a VBA Excel Add-In that should
I have working VBA code, but I can't convert it to C#. I have
I have this VBA below that is designed to loop through each WS and
I have a worksheet with 2 columns Key and Value. by VBA code, i

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.