Ben Conner
asked on
using VBA to convert single tab spreadsheet to multiple spreadsheets
Hi,
I have a spreadsheet I'll be creating with provider id as the 1st column and other fields from columns 2-11. I need to take this spreadsheet and a template spreadsheet and create new spreadsheets from the original, with the template info at the top of each of them. The name of the new spreadsheets would be the provider #.
Working in Excel 2016. Would love to be able to do this in batch mode if possible, as there will be a lot of spreadsheets created.
Can anyone give an approach to take that I can start with?
Thanks!
--Ben
I have a spreadsheet I'll be creating with provider id as the 1st column and other fields from columns 2-11. I need to take this spreadsheet and a template spreadsheet and create new spreadsheets from the original, with the template info at the top of each of them. The name of the new spreadsheets would be the provider #.
Working in Excel 2016. Would love to be able to do this in batch mode if possible, as there will be a lot of spreadsheets created.
Can anyone give an approach to take that I can start with?
Thanks!
--Ben
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you are welcome. :)
Professor,
I think the "GetDirectory" procedure is missing from your code to pick the location to save the new workbooks. Code works well though once that is included.
I think the "GetDirectory" procedure is missing from your code to pick the location to save the new workbooks. Code works well though once that is included.
Jerry,
if you open the attachment which i posted along with the code, in its module "BrowseSupliment" it does contain the "GetDirectory" function.
what Ben asked can be achieved only up to the 35th line of the code, i provided extra code, in case if Ben wanted to save the sheets into separate workbooks.
if you open the attachment which i posted along with the code, in its module "BrowseSupliment" it does contain the "GetDirectory" function.
what Ben asked can be achieved only up to the 35th line of the code, i provided extra code, in case if Ben wanted to save the sheets into separate workbooks.
My apologies... I did not download the attachment. Only executed the code that was in the comment block above. Again, well done!
no problem Jerry.
thanks.
thanks.
ASKER
Am nearly complete creating the spreadsheet this will run against. Looking at the code, I think I can eliminate the first half of it where it splits the single tab into multiple tabs prior to shipping them out to different spreadsheets. I just did that in the application I wrote instead.
Should be able to test it later today.
Should be able to test it later today.
ASKER
BTW, this will end up with 4000-4500 tabs. That won't be a problem will it?
Hi Ben,
Number of worksheet limit is dependent upon available memory . Do you have 32 bit or 64 bit office?
Number of worksheet limit is dependent upon available memory . Do you have 32 bit or 64 bit office?
ASKER
It's 64 bit, Excel 2016
ASKER
And each tab will be fairly small, about 8 columns and max 100 rows.
ASKER
Oh. Wait. I won't be running it, someone else will. I don't know what their footprint looks like. Oops.
let them try, and see it might work.
ASKER
Just found they are on Excel 2010, 32 bit version.
ASKER
Trying the code above now that I have the spreadsheet created and am getting an unknown function error for GetDirectory(). I don't see it in Excel 2016. Should I use Activeworkbook.Path or something similar? The error was on line:
Folder = GetDirectory(Folder)
--Ben
Folder = GetDirectory(Folder)
--Ben
Hi Ben,
yiu are getting that error because probably you did not copy the whole code from the attached workbook, please open the attached workbook that I posted in earlier post and there is also the UDF in its module "BrowseSupliment"
Once you copy and paste that code with the existing code then you will not see error
yiu are getting that error because probably you did not copy the whole code from the attached workbook, please open the attached workbook that I posted in earlier post and there is also the UDF in its module "BrowseSupliment"
Once you copy and paste that code with the existing code then you will not see error
ASKER
Oops. Missed the xlm attachment. My bad. Thanks!
--Ben
--Ben
ASKER
Just ran the spreadsheet in debug mode. Very nice! The app I developed already does the first part--splitting them out into separate tabs, so I can just lift the latter half of it and let it rip.
Thanks very much!
--Ben
Thanks very much!
--Ben
ASKER
Excellent example of splitting a sheet in a spreadsheet into multiple spreadsheets in an automated fashion.
You are welcome Ben, I am glad I was able to help
ASKER
This is probably a separate question, but can this be automated? The client wants to be able to call this in an automated .bat file. Something like:
<path to excel> <spreadsheet> <path to macro> <path to destination folder> <split column> <# of title rows>
I'm not even sure this is possible. ?
--Ben
<path to excel> <spreadsheet> <path to macro> <path to destination folder> <split column> <# of title rows>
I'm not even sure this is possible. ?
--Ben
yes it is a separate question and it not related to Excel or VBA. i suggest you open a question in the top of related window scripts .
ASKER
Thanks! Will do.
--Ben
--Ben
Professor,
I have 64 bit, Excel 2016 and receive a Compile error when I try to open your xslm attachment. How do I fix that?
--Christine
I have 64 bit, Excel 2016 and receive a Compile error when I try to open your xslm attachment. How do I fix that?
--Christine
Can you please share on which line of the code the highlighter of debugger stops? I
Hi Professor, I got busy on another project and am now back to this one. The section highlighted is this:
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Thank you for your help!!
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Thank you for your help!!
to make my the code in my answer work with both 32bit and 64bit. the entirecode in the module called "BrowseSupliment" to be replaced with the below.
i have also uploaded the complete file.
i have also uploaded the complete file.
#If VBA7 Then
Private Type BROWSEINFO
hOwner As LongPtr
pidlRoot As LongPtr
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As LongPtr
lParam As LongPtr
iImage As Long
End Type
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As LongPtr
Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As LongPtr, ByVal pszPath As String) As LongPtr
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As LongPtr, x As LongPtr, pos As Integer
#Else
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
#End If
Private Const BIF_RETURNONLYFSDIRS = &H1
Function GetDirectory(Optional Msg) As String
' Dim bInfo As BROWSEINFO
' Dim path As String
' Dim r As Long, x As Long, pos As Integer
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
32bit-and-64bit-all-versions.xlsm
ASKER
Thanks! I used the original code as a starting point for the project I had. Would love to post what I developed but I don't own the code and don't have access to that system any longer.
--Ben
--Ben
ASKER
Will give this a shot and let you know.
Thank you!
--Ben