Macro to create and name tabs based on Column values in a worksheet

Skycap

New Member
Joined
Aug 20, 2013
Messages
1
My request is twofold.
1. I keep a large spreadsheet of daily purchases made by each of our departments. The department name is in Col F of my spreadsheet. Each morning I make a workbook with all of the previous day's purchases broken out in tabs by department. On any given day there can be anywhere from 15 to 35 different tabs needed. So far, I've just been using a pivot table to make the tabs, but it's getting old. I've recorded a macro that creates the tabs from a pivot, but it only works when the department names are consistent. On those days when the number of departments spike (usually on Mondays), I have to go back to creating the tabs manually. I'm quite a newbie at exploring the potential of macros, and was hoping that someone might be able to help me with a macro that will create my tabs for me regardless of the # of departments involved.
2. Each tab needs to be named based on the manager of that department, and so I've created a reference table where the Department name is in col A and the Manager (and therefore tab) name in Col B. Example, Department name would be "Production" and the tab name would be "Roberts". Unfortunately, I cannot change the main spreadsheet col F to show Manager name as opposed to Department.
Any direction that you can give would be a huge time saver and greatly appreciated!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The description of the task is a little confusing, but my interpretation is to create a new workbook for each department appearing in Column F and then create a sheet in that new workbook named for the department manager which is listed in column B of the original worksheet and indexed to the Department listed in column A. The code below is based on that interpretation. Assumptions are that data in all columns begin at row 2. You can edit the variable assignments if the assumtions are incorrect. This will only create the workbooks and assign the names. It does not move any data.
Code:
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, "F").End(xlUp).Row
Set rng = sh.Range("F2:F" & lr)
    For Each c In rng
        If c <> "" Then
            Set mNm = sh.Range("A:A").Find(c.Value, LookIn:=xlValues)
                If Not mNm Is Nothing Then
                    Set wb = Workbooks.Add
                    wb.Sheets(1).Name = mNm.Offset(0, 1).Value
                    'You can add code here to move the sales data into the workbooks.
                    wb.SaveAs c.Value & ".xlsx"
                    wb.Close False
                End If
        End If
    Next
Set sh = Nothing
Set rng = Nothing
Set mNm = Nothing
Set wb = Nothing
End Sub
 
Last edited:
Upvote 0
I don't know why I am having such a difficult time getting this posted correctly. But I think this does it.
Code:
Sub makeWb()
Dim sh As Worksheet, lr As Long, rng As Range, c As Range, wb As Workbook, mNm As Range
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, "F").End(xlUp).Row
Set rng = sh.Range("F2:F" & lr)
    For Each c In rng
        If c <> "" Then
            Set mNm = sh.Range("A:A").Find(c.Value, LookIn:=xlValues)
                If Not mNm Is Nothing Then
                    Set wb = Workbooks.Add
                    wb.Sheets(1).Name = mNm.Offset(0, 1).Value
                    'You can add code here to move the sales data into the workbooks.
                    wb.SaveAs c.Value & ".xlsx"
                    wb.Close False
                End If
        End If
    Next
Set sh = Nothing
Set rng = Nothing
Set mNm = Nothing
Set wb = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,966
Messages
6,127,975
Members
449,414
Latest member
sameri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top