
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
None | size: 2.15 KB | hits: 11 | expires: Never
Sub CreateWbFromNames()
Dim r As Integer
Dim p, n, d, f, l, main As String
Dim wb As Workbook
Dim ws As Worksheet
Dim cellSplit As Variant
main = ActiveWorkbook.name
r = 2
' start loop
Do While Range("A" & r).Value <> ""
p = Range("Q" & r).Text
d = "0"
' check for . in name
If InStr(p, ".") Then
cellSplit = Split(p, ".")
f = cellSplit(0)
l = cellSplit(1)
n = f & "_" & l
Else
n = p
End If
' check for workbook
For Each wb In Workbooks
If wb.name = n & ".xls" Then
d = wb.name
Exit For
End If
Next wb
' if wb doesn't exist, create it
If d = "0" Then
Workbooks.Add
ActiveWorkbook.SaveAs n & ".xls"
d = n & ".xls"
' copy and paste the first row
Workbooks(main).Activate
Range("A1").EntireRow.Copy
Workbooks(d).Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveWorkbook.Save
End If
' copy the row
Workbooks(main).Activate
Range("A" & r).EntireRow.Copy
Workbooks(d).Activate
' check for empty row
If Range("A1") = "" Then
Range("A1").Select
Else
Range("A1000").End(xlUp).Offset(1, 0).Select
End If
' paste data
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveWorkbook.Save
' increment row
Workbooks(main).Activate
r = r + 1
Loop
' close all workbooks but main and personal.xls
For Each wb In Workbooks
If wb.name <> main And wb.name <> "PERSONAL.XLS" Then
wb.Close
End If
Next wb
MsgBox ("Actions Completed")
End Sub