Well, the file has confidential information, so I cannot send it. Allow me to put in the original two macros so you can see it as compared to what I'm hoping to accomplish. I'm trying to create 219 OSHA Logs and Summary pages. The first macro creates the 219 OSHA Log and Summary workbooks (now set to 8 workbooks) and names and saves the file...and it works well. The second macro puts the year in cell A1 and then the first branch number in A4; after that various vlookups pull information into the spreadsheet based on the branch number like injuries, branch addresses, phone numbers etc. to then populate the Log and Summary; this second macro also works well. The problem comes when I try to merge the two into one macro.
Sub CreateWorkbooks1()
' This one creates new workbooks
' Created by JieJenn & jpdutch 01-11-12
' Switch 915 with "10" below as needed...and vis
'
' Const NumCopies As Long = 219
Const NumCopies As Long = 8
Dim FilePath As String
Dim i As Long
Application.DisplayAlerts = False
' Be sure to select the correct path in which to create the workbooks (01/27/16: I changed the dates below to 2015)
' Was FilePath = "C:\Users\johnsoj\Desktop\2016 Test Macros\Final Logs"
FilePath = "C:\Users\john.johnson\Desktop\2016 Test Macros\FINAL Logs"
For i = 1 To NumCopies
' ActiveWorkbook.SaveAs Filename:=FilePath & Corporate & "\" & i & "_OSHA LOG_2015 " & ".xlsm" Does this need to be modified?
' Range("D" & i + 40).Text &
ActiveWorkbook.SaveAs Filename:=FilePath & "\" & i & "_OSHA LOG_2016 " & " .xlsm"
Next i
Application.DisplayAlerts = True
End Sub
Here's the second macro I use to populate the OSHA Logs and Summary Pages:
Sub InserYearBranchNumberVLkup2()
' Macro recorded 01/12/2012 by jpdutch
' This starts things rolling 'Use this one if needed to format & clean-up worksheets
' Dim fs As FileSearch
Dim i As Integer
Dim Wbk As Workbook
Dim sFile As String
' Set fs = Application.FileSearch
sFile = Dir(ThisWorkbook.Path & "\* .xlsm")
Do While sFile <> ""
' With fs
' .LookIn = ThisWorkbook.Path
' .Filename = "*.xls"
' For i = 1 To .Execute()
' Set wbk = Workbooks.Open(.FoundFiles(i))
Set Wbk = Workbooks.Open(ThisWorkbook.Path & "\" & sFile)
Sheets("Instruction Sheet").Visible = True
Sheets("Data").Visible = True
Sheets("Data").Select
ActiveSheet.Unprotect
' This will set the correct year. You'll need to change this each year. Then it does the Vlookup Function.
Range("A1").Select
ActiveCell.FormulaR1C1 = "2016"
Range("A1").Select
' This Inserts the Branch number into "A4"
Range("A4").Select
ActiveCell.FormulaR1C1 = _
"=LEFT(MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),SEARCH(""_OSHA"",MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),1)-1)*1"
Range("A5").Select
' This will format the range for general so the VLookUp will work
Columns("B:B").ColumnWidth = 50
Range("B4:D25").Select
Selection.NumberFormat = "General"
' This will do the VLookUps
Range("B4:D25").Select
Selection.ClearContents
Range("B4").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)),""DO NOT PRINT"",(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)))"
Range("C4").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)),"""",(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)))"
Range("D4").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)),"""",(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)))"
Range("B4:D4").Select
Selection.AutoFill Destination:=Range("B4:D25"), Type:=xlFillDefault
Range("A1").Select
' Range("B4:D25").Select
' This will copy the values and paste them as values only.
Range("A4:D25").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Data").Select
Range("A1").Select
' Range("A1").Select
' Sheets("Data").Visible = False
' Sheets("Data").Visible = True
' This closes the sheets
Sheets("Instruction Sheet").Visible = False
Sheets("Data").Visible = False
Sheets("Hours & EE Count").Visible = False
' Sheets("Staff Directory").Visible = False
Sheets("Branch Directory").Visible = False
Sheets("EE Injuries").Visible = False
' Sheets("Data").Select
' Range("A1").Select
Wbk.Close SaveChanges:=True
' wbk.Close SaveChanges:=False
' Next i
sFile = Dir
Loop
'End With
End Sub
Combining these two is where the problem comes in, so I'm trying to modify the code to allow everything to run smoothly. Please let me know if you still need the file; if so, I'll remove the confidential information and then send it to you. Thanks to all who are helping me with this. JPDutch.
Bookmarks