+ Reply to Thread
Results 1 to 7 of 7

Thread: How to fill a cell with the name of the worksheet

  1. #1
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    9

    How to fill a cell with the name of the worksheet

    If I want to fill A1 with "ABC" and A2 with "DEF" AND A3 with the name of the worksheet on multiple worksheets and these actions are not to be done on each worksheet (e.g. not on worksheet 1 & 2 but do it only on worksheet 3-20 or more), how can I do it with VBA?

    With reference to the post on http://www.excelforum.com/excel-new-...ary-sheet.html, I came up with the following codes. Could anyone advise? Many thanks.

    Option Explicit
    Sub AddInfo()
    Dim ws As Worksheet
    Dim MySh As Variant
    
    MySh = "Master DB"
    
    For Each ws In Worksheets
    If ws.Name <> MySh Then _
    
    With ws
          .Range("A1").value = "ABC"
          .Range("A2").value = "DEF"
    End With
    
    Next ws
    
    End Sub
    Last edited by alexxgalaxy; 09-22-2011 at 12:34 AM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How to fill a cell with the name of the tab

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: How to fill a cell with the name of the worksheet

    Try this
    Option Explicit
    
    Sub AddInfo()
        Dim n As Long
        Dim MySh As String
    
        MySh = "Master DB"
    
        For n = 3 To Sheets.Count
            If Sheets(n).Name <> MySh Then
                With Sheets(n)
                    .Range("A1") = "ABC"
                    .Range("A2") = "DEF"
                    .Range("A3") = Sheets(n).Name
                End With
            End If
        Next
    
    End Sub
    Hope this helps
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: How to fill a cell with the name of the worksheet

    Hi,

    You can of course also return the sheet name with a formula. First group all the sheets in which you want the sheet name to appear, i.e. sheets 3-20

    Enter "ABC" in A1 on any sheet, "DEF" in A2 on any sheet and
    =MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,31)
    in A3 on any sheet.

    Then undo the grouping.

    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  5. #5
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: How to fill a cell with the name of the worksheet

    Thanks Marcol ! That worked very well. I would like to add one more cell A4 and it's value changes every time, let say, it's the date. I tried to add an InputBox after Range("A3")... but when I ran the Marco, the InputBox prompted up for every worksheet (I have more than 30 worksheets @@). I'd also like to do some formatting and adjust the column width. Could you take a look at below codes and let me know if that's the way?

    And sorry, one more, what if I don't want the actions done on a few worksheets. They might not be the first two worksheets. Sometimes it might be the 21st and 29th while sometimes the others. I won't be able to tell the sequence of the worksheets because it might change every time (unless I count) but what I could be certain was the name of the worksheet.

    Option Explicit
    
    Sub AddInfo()
        Dim n As Long
        Dim MySh As String
    
        MySh = "Master DB"
    
        For n = 3 To Sheets.Count
            If Sheets(n).Name <> MySh Then
                With Sheets(n)
                    .Range("A1") = "ABC"
                    .Range("A2") = "DEF"
                    .Range("A3") = Sheets(n).Name
                    .Range("A4") = InputBox("Please enter the date.") 
                        With Range("A1:A4")
                             .Interior.ColorIndex = 35
                             .font.bold = True
                             .size = 14
                        End With
                     Columns("A:A").ColumnWidth = 18
                End With
              
            End If
        Next
    
    End Sub
    Last edited by alexxgalaxy; 09-17-2011 at 08:40 AM.

  6. #6
    Registered User
    Join Date
    09-16-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: How to fill a cell with the name of the worksheet

    Thanks Richard. That's what I did at the first place but it didn't work somehow. I did the actions recording it as Marco but when I played it afterwards, the actions were performed on only the worksheet which was activated.
    Last edited by alexxgalaxy; 09-17-2011 at 07:19 PM.

  7. #7
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: How to fill a cell with the name of the worksheet

    Hi,

    Try doing it manually and not bothering to record a macro.

    Copying the cells then Grouping the sheets and pasting works OK for me.

    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

+ 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.2.0