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

  • Home
  • SEARCH
  • 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 8900663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:09:09+00:00 2026-06-15T01:09:09+00:00

I have a Excel file with multiple tabs. I have a worksheet with some

  • 0

I have a Excel file with multiple tabs. I have a worksheet with some code which is working fine. This code also refers to data on some “master” tabs. I need to duplicate this sheet so I moved the common functions from there to a module. Now I get a VBA 1004 error when trying to access a range on the same worksheet.

 Dim selectedRange As Range
 Set selectedRange = Worksheets(name).Range("A1", _
 Range("A" & Rows.count).End(xlUp)) 'Error Line

This code worked fine till I moved it to a module. It works if I put a

Worksheets(name).Select 

before it, but I will have to do it too many times. Based on this query: VBA error 1004 – select method of range class failed
the code should work fine without a .Select. And it does as long as the code is within the worksheet. Why does moving code to a module create a problem?

  • 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-15T01:09:10+00:00Added an answer on June 15, 2026 at 1:09 am

    U use Range and Rows properties without an object qualifier. When used without an object qualifier, this properties are a shortcut for ActiveSheet.Range / ActiveSheet.Rows.

    So the code does this:

    Worksheets(Name).Range("A1", ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp))
    

    But Worksheets(name) could be different from active sheet so better is:

    Worksheets(Name).Range("A1", Worksheets(Name).Range("A" & Worksheets(Name).Rows.Count).End(xlUp))
    

    In With-End With block:

    With Worksheets(name)
          Set selectedRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With
    

    So it is ensured that the Range/Rows properties are applied on Worksheets(name) worksheet object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel file which is refreshing data every few seconds and running
I have a excel file which is as follows . 1 Kr Veerappan 123
I have an excel file that I need to make some changes. I need
I have an Excel file with one column which is filled with numbers. I
I have an Excel file that gets external data from database table. I need
I am having few excel files where each file have multiple workbooks. Assume following
I have an excel macro saved in a blank workbook and multiple data workbooks.
I have to code a really annoying script that uses one excel file to
So I have an excel file. I have multiple queries that are returned. However
I have no problem in writing data in excel sheet which is stored in

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.