Consulting

Results 1 to 5 of 5

Thread: Sheets Object problem

  1. #1

    Sheets Object problem

    The code inserts a worksheet, names this worksheet, and then imports data to this sheet. The added sheet is there, but I am renaming an existing worksheet instead of the added sheet. The imported data would overlay this sheet if I allowed it to continue. Here is the code snippet:

    [vba] Sheets.Add
    Sheets(Sheets.Count).Select
    shName = BldName(impType)
    Sheets(Sheets.Count).Name = shName [/vba]

    I assumed that the added sheet would be the "last" sheet (highest number or "Count"). This is NOT the case. How do I name this added sheet? FYI: BldName is a macro that constructs this name, and it is working properly (I displayed the constructed name).

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Set mySheet = Workheets.Add
    mySheet.Name = BldName(impType)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3

    Thanks again XLD

    Thanks.

    One additional question: Do I need a 'DIM mysheet As Worksheet'?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You don't need it if you don't have Option Explicit at the head of the mmodule, but I strongly recommend that you have both.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

    Thanks again, but one more question

    Thanks again for the input.

    The code now works, but I am still wondering about why my original attempt did not work. Specifically, if I use the "Sheets(1)" nomenclature, I thought that construct would ALWAYS point to the FIRST worksheet created. Using this logic, and assuming I just added a worksheet, then "Sheets(Sheets.Count)" would point to the just-inserted worksheet.

    I know that this is not the case, and because you can add/delete worksheets many times over, this notion of "first one created" could get very fuzzy. Still there must be some logic as to the way Excel would address these sheets. Do you know what it is?

    TIA

Posting Permissions

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