Excel/VBA adjustment

Advertisement


Question
Hi Tom

Some time ago you wrote this code for me.  It looks at the names in column A of worksheet "createfiles" and creates a separate workbook for each populated cell in column A.  So that, if the name "Charlie" is in cell A1, a separate file named "Charlie.xlsm" will be created and placed into drive "D:\".

At present this only works for cell A1.  The code does not create files for any other populated cell in column A, So, if cell A2 contain the name Michael and the code is activated a new file called "Michael.xlsm" is not created.

Could you adjust the code for me so that a file is created all populated cells in column A?

Thanks in advance.

Chris

Sub abcde()
Dim bk As Workbook, bk1 As Workbook
Dim sh As Worksheet
Dim r As Range, cell As Range
Dim sName As String

Set bk = ThisWorkbook
bk.SaveCopyAs Filename:="D:\Dummy.xlsm"
Set sh = bk.Worksheets("Createfiles")
Set r = sh.Range("A1", sh.Cells(41, "A").End(xlUp))
On Error GoTo ErrHandler
For Each cell In r
sName = ""
If Len(cell.Text) > 0 Then
  sName = cell.Text & ".xlsm"
  Workbooks.Open "D:\Dummy.xlsm"
  Set bk1 = ActiveWorkbook
  Debug.Print bk1.Name, sName
  bk1.SaveAs Filename:="D:\" & sName, _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
  bk1.Close SaveChanges:=False
End If
Next
Kill "D:\Dummy.xlsm"
Exit Sub
ErrHandler:
MsgBox "Error - problem with file: " & sName
Resume Next
End Sub

Answer
Christopher,

I had a workbook open with a sheet named  CreateFiles

A1:  AAAA
A2:  BBBB
A3:  CCCC
A4:  DDDD

I had a drive named   D   where I had permissions to write
For testing purposed a made a folder   D:\Test

I ran this code:
Sub abcde()
Dim bk As Workbook, bk1 As Workbook
Dim sh As Worksheet
Dim r As Range, cell As Range
Dim sName As String

Set bk = ThisWorkbook
bk.SaveCopyAs Filename:="D:\Dummy.xlsm"
Set sh = bk.Worksheets("Createfiles")
Set r = sh.Range("A1", sh.Cells(41, "A").End(xlUp))
On Error GoTo ErrHandler
For Each cell In r
sName = ""
If Len(cell.Text) > 0 Then
sName = cell.Text & ".xlsm"
Workbooks.Open "D:\Dummy.xlsm"
Set bk1 = ActiveWorkbook
'Debug.Print bk1.Name, sName
' changed "D:\"  to "D:\Test\" for testing
bk1.SaveAs Filename:="D:\Test\" & sName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
bk1.Close SaveChanges:=False
End If
Next
Kill "D:\Dummy.xlsm"
Exit Sub
ErrHandler:
MsgBox "Error - problem with file: " & sName
Resume Next
End Sub

in folder D:\Test

I found four workbooks named

AAAA.xlsm,  BBBB.xlsm,  CCCC.xlsm,   DDDD.xlsm

Code worked fine for me.

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.