+ Reply to Thread
Results 1 to 2 of 2

Is there a macro to put a list of cells listed on first sheet onto each successive sheet?

  1. #1
    Registered User
    Join Date
    06-22-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    4

    Question Is there a macro to put a list of cells listed on first sheet onto each successive sheet?

    I have a list of cells on the 1st sheet. Example
    203.1
    204.2
    206.4

    The following macro creates sheets copied from a template sheet so that the workbook will end up with identical sheets named 203.1, 204.2, 206.4. How do i edit the following macro so that each sheet created has the sheet title value in cell D8. By that i mean sheet 203.1 also has 203.1 in cell D8 once the macro is completed? I do not want an equation to be in D8 that will output 203.1, just the value 203.1. Your help is greatly appreciated, thanks!

    ****
    Option Explicit

    Private Sub CommandButton1_Click()

    Dim strCol As String
    Dim strRow As String
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    Dim strWsName As String
    Dim strSrcName As String

    On Error GoTo ErrHnd

    'setup column letter and first row number containing names
    'column
    strCol = "B"
    'row (number is in double quotes)
    strRow = "14"

    'turn off screen updating to stop flicker & increase speed
    Application.ScreenUpdating = False

    'save this worksheet's name, so we can go back to it later
    strSrcName = ActiveSheet.Name

    'set start of data in selected column
    Set rngStart = ActiveSheet.Range(strCol & strRow)
    'find end of data in selected column
    Set rngEnd = ActiveSheet.Range(strCol & CStr(Application.Rows.Count)) _
    .End(xlUp)

    'loop through cells in used range
    For Each rngCell In ActiveSheet.Range(rngStart, rngEnd)
    'ignore empty cells in range
    If rngCell.Text <> "" Then
    'get worksheet name
    strWsName = rngCell.Text
    'test if worksheet exists
    On Error Resume Next
    If Worksheets(strWsName) Is Nothing Then
    'worksheet does not exist
    'reinstate error handling
    On Error GoTo ErrHnd
    'copy worksheet named "Template"
    Worksheets("Template").Copy After:=Worksheets(Worksheets.Count)
    'name new sheet
    Worksheets(Worksheets.Count).Name = strWsName
    Else
    'worksheet already exists
    'reinstate error handling
    On Error GoTo ErrHnd
    End If
    End If
    Next rngCell

    'go back to the source worksheet
    Worksheets(strSrcName).Activate

    'reinstate screen updating
    Application.ScreenUpdating = True
    Exit Sub

    'error handler
    ErrHnd:
    Err.Clear
    'go back to the source worksheet
    Worksheets(strSrcName).Activate
    'reinstate screen updating
    Application.ScreenUpdating = True

    End Sub
    ****

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Is there a macro to put a list of cells listed on first sheet onto each successive she

    Why not an equation? This is a common formula used to display the current sheetname:

    =MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)

    (don't edit the A1 references, they're just part of the trick)

    With that in your template in cell D8, the sheetname will just appear on its own.


    For a macro solution, it's simple to add a value to a cell:

    Please Login or Register  to view this content.

    NOTE: As per forum rules, please EDIT your post above and add a CODE tag and /CODE tag around your posted code, like I've done in this post. (example in my signature below). Thanks.
    Last edited by JBeaucaire; 06-23-2012 at 03:43 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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