Consulting

Results 1 to 11 of 11

Thread: Solved: Read Sheet name from a Cell

  1. #1
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location

    Post Solved: Read Sheet name from a Cell

    I want to change sheet number 5 to a name of sheet that is written in cell B6 of Sheet(1).
    e.g Cell B6 in Sheet(1) has "Data1" written in it.
    Thanks

    [vba]
    Set rDest = Sheets(5).Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
    [/vba]

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Perhaps:

    [VBA]Sub dural()
    Sheets(5).Name = Sheets(1).Range("B6").Value
    End Sub
    [/VBA]
    Have a Great Day!

  3. #3
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location
    I actually don't want to change the name, instead the name of sheet(5) is already "Data1" but I want my code to read the name of sheet from cell B6 of Sheet(1). e.g B6 already have the name of sheet "Data1"

  4. #4
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Do you mean the reverse?

    Sub dural()
    Sheets(1).Range("B6").Value = Sheets(5).Name
    End Sub
    sassora

  5. #5
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location
    So this was what I originally posted

    [vba]
    Set rDest = Sheets(5).Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
    [/vba]
    Here the sheet number is 5 but I don't want to it to call this sheet by its number instead I want it to call it by its name which is "Data1".

    I can replace 5 with "data1" here right!!! and it will work...... but sheet name is not fixed every-time so I want it to call this sheet name from cell A1 of sheet1.

    So that every time sheet name written in cell A1 of sheet1 is changed then sheet name here will also change.

    This should work, but not working !
    [VBA]Sheetname = ThisWorkbook.Sheets(1).Range("A1")

    Set rDest = Sheets("Sheetname").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    [/VBA]


    Thanks
    Last edited by afzalw; 04-14-2013 at 02:35 PM.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    [vba]Set rDest = Sheets(Sheets("Sheet1").Range("A1")).Range("B" & Rows.Count.End(xlUp).Offset(1, 0)) [/vba]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location
    Quote Originally Posted by SamT
    [vba]Set rDest = Sheets(Sheets("Sheet1").Range("A1")).Range("B" & Rows.Count.End(xlUp).Offset(1, 0)) [/vba]
    Did not worked I already tried.

    Its same as
    [VBA]sheetname = Sheets("Sheet1").Range("A1")
    Set rDest = Sheets("sheetname").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)[/VBA]

    Sheetname has the name Data1 stored in it but does not work here don't know why!

  8. #8
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Drop the double quotes:
    [VBA]
    sheetname = Sheets("Sheet1").Range("A1")
    Set rDest = Sheets(sheetname).Range("B" & Rows.Count).End(xlUp).Offset(1, 0)[/VBA]
    Have a Great Day!

  9. #9
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location
    Quote Originally Posted by GarysStudent
    Drop the double quotes:
    [vba]
    sheetname = Sheets("Sheet1").Range("A1")
    Set rDest = Sheets(sheetname).Range("B" & Rows.Count).End(xlUp).Offset(1, 0)[/vba]

    Ok I did try that before it only works when your sheet name is alphanumeric sometimes my sheet names are only numeric..
    e.g
    its working for Data1 but If my sheet name is 123 then it wont work

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    sometimes my sheet names are only numeric..
    That's nice to know.

    [VBA]sheetname = Sheets("Sheet1").Range("A1").Text [/VBA]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  11. #11
    VBAX Regular
    Joined
    Jun 2012
    Posts
    45
    Location
    Quote Originally Posted by SamT
    That's nice to know.

    [vba]sheetname = Sheets("Sheet1").Range("A1").Text [/vba]

    It didn't worked before and its working now .

    Thanks

Posting Permissions

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