Steph,
I can only tell you that we could do it, but it would be better if you used
better spreadsheet design. Blanks are BAD in databases, and multiple rows
or headers are _really_ BAD.
Instead of using separate cells to put your header, use a single cell with
Alt-Enter between the lines that you want to show, to control how the text
wraps. For example, instead of
Header1
Header2
in two cells, you would type Header1, press Alt-Enter, then type Header2,
and then press Enter. That would keep your headers on sepearate rows within
the same cell.
If you can't change this for some reason (like there are thousands of these
bad databases out there that you need to do) post back and we'll do
something.
HTH,
Bernie
MS Excel MVP
Steph said:
Thanks Bernie. I think you hit it on the head. I neglected to mention this
in the original post, but the database has 4 header lines (3 of which are
blank i the key column). The actual data begins on row 5. But I'd like (if
possible) the 4 data lines to be on each of the newly created files as well.
Is this an easy fix?
like
/\
'
[ ] and a few others that I don't recall off-hand right now.
Run the code, and when you get the error, go into debug mode. With your
cursor, highlight myCell.Value. A little pop-up box should show the value,
or you could add a watch to see the value.
If it is a blank (""), try adding
If myCell.Value = "" Then GoTo SheetExists
just below the line
For Each myCell In myArea
Otherwise, we'll need to add code to watch for invalid characters in the
sheet name.
HTH,
Bernie
MS Excel MVP
Hi Bernie. Thanks for the code. I tried it, and got an error on this
line:
mySht.Name = myCell.Value
The error was Object variable or with block variable not set.
It looks like it is parsing the data correctly, but errors out at
the
very
end. Any ideas? Am I doing something wrong? Thanks!!
-Steph
Steph,
Copy the macro below and put it into a codemodule of either your
personal.xls or of the workbook with the database.
Select a single cell in your database and run the macro. If you
supervisor
names are in column C, and column C is the second column of the
database,
then enter a 2 when asked "What column # within database to use as key?"
The files will be saved to whatever folder is currently the default
folder,
though that is easy to modify.
HTH,
Bernie
MS Excel MVP
Sub ExportDatabaseToSeparateFiles()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer
myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")
Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells
Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)
For Each myCell In myArea
If myCell.Value = "" Then GoTo SheetExists
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(before:=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell
For Each mySht In ActiveWorkbook.Worksheets
If mySht.Name = myShtName Then
Exit Sub
Else
mySht.Move
ActiveWorkbook.SaveAs "Workbook " & ActiveSheet.Name & ".xls"
ActiveWorkbook.Close
End If
Next mySht
End Sub
Hi everyone. I have a single worksheet that is sort of a
database.
I
t
has
several thousand rows and about 20 columns. One column is Supervisor
name.
I need to separate the single sheet into multiple FILES (1 per
Supervisor)
based on Supervisor name. Can you help? Supervisor name is in column
C.
Thanks!