+ Reply to Thread
Results 1 to 5 of 5

Help with referencing a specific sheet

  1. #1
    Registered User
    Join Date
    03-13-2005
    Posts
    29

    Question Help with referencing a specific sheet

    I have the following code assigned to a list box. The code works perfectly when the sheet 'Employees' is the active sheet. However, when another sheet is the active sheet, the User Form pulls all of its information from the active sheet instead of 'Employees'. I don't understand why this is happening since I have a With statement around the entire macro specifying the correct worksheet.

    Please Login or Register  to view this content.
    What am I doing wrong?

    Scott

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Scott,

    nesting With statements can be misleading. You need to preface the With Range statement with Worksheets("Employees"). Once the compiler sees a with it uses that objects properties and events. Since you state With Range, the default system object is the ActiveWorksheet.

    Code Change:
    With Worksheets("Employees").Range(EmpFound.Address)

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    03-13-2005
    Posts
    29
    Thanks so much. That works perfectly.

    However, using the same logic, I still cannot fix the following code:

    Please Login or Register  to view this content.
    It pastes the information from the userform to the active sheet, and not employees. This is not a nested With statement, so I am unsure why it is ignoring the With statement which specifically points to the employees worksheet.

    Thanks,
    Scott

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Scott,

    You left out the period before Cells. See the below...

    You Wrote:
    With Worksheets("Employees")
    FinalRow = Cells(Rows.Count, "A").End(xlUp).Row


    Should Be:
    With Worksheets("Employees")
    FinalRow = .Cells(Rows.Count, "A").End(xlUp).Row


    The period indicates that the property or method belongs to the object specified by the With statement. Otherwise VBA will use the default object for Cell, the ActiveWorksheet.

    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    03-13-2005
    Posts
    29
    I knew it had to be something small. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1