Sure thing Leith.
One of the reasons I was looking to do this is because we currently have several users who are supposed to go into a workbook containing multiple sheets and copy information for their schedule to a new workbook then exit the master without saving. There has been a few times when users have manipulate/deleted data and saved the master causing a lot of confusion and problems. The attached book is the result of the following code which is extracting a single sheet to a new workbook.
Sub Electric()
' Electric Macro
Dim pobjXLApp As Excel.Application 'Excel Application
Dim pobjMasterXLBook As Excel.Workbook 'Excel Workbook
Dim pobjMasterXLSheet As Excel.Worksheet 'Excel Worksheet
Dim pobjNewXLBook As Excel.Workbook 'Excel Workbook
Dim pobjNewXLSheet As Excel.Worksheet 'Excel Worksheet
Dim pstrHistoryFileLocation As String
Dim pstrFileNameData As String
Dim pstrNewXLbook As String
Dim pstrResponse As String
Set pobjXLApp = Excel.Application
'Set Master objects
Set pobjMasterXLBook = ActiveWorkbook
Set pobjMasterXLSheet = pobjMasterXLBook.Worksheets("1 week schedule by operation")
'Opens a new workbook
Set pobjNewXLBook = pobjXLApp.Workbooks.Add 'Creates new workbook
Set pobjNewXLSheet = pobjNewXLBook.Worksheets(1) 'Runs Worksheet 1= First Sheet
'Select the Master sheet and copy it to the history spreadsheet.
'Copy the first sheet
pobjMasterXLSheet.Copy pobjNewXLSheet
Set pobjNewXLSheet = pobjNewXLBook.Worksheets("1 week schedule by operation")
pobjNewXLSheet.Activate
' Cells.Select
' Selection.Copy
' Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
' xlNone, SkipBlanks:=False, Transpose:=False
' New for custom sort
Range("B1").Select
Range("A2:K2500").Select
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Add _
Key:=Range("B2:B2500"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Add _
Key:=Range("A2:A2500"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Add _
Key:=Range("C2:C2500"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Add _
Key:=Range("F2:F2500"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("1 week schedule by operation").Sort.SortFields.Add _
Key:=Range("G2:G2500"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("1 week schedule by operation").Sort
.SetRange Range("A1:K2500")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Range("B1").Select
End With
End Sub
The attached workbook has three sheets to show the example only. The goal is to have only one sheet based on the craft(s) that are running it. The sheet (1 week schedule by operation) is a data dump from SAP and can be upto ~2500 lines.
For a single craft example I choose (ELECTRIC). Once the code runs, the sheet (1 week schedule by operation) should look like the (ELECTRIC) sheet.
For the multi craft example I choose (EG OPER, HARD TR, SOFT TR). Once the code runs, the sheet (1 week schedule by operation) should look like the (EG OPER, HARD TR, SOFT TR) sheet.
I think I could mutle through all the minute changes with some sample code for a single and multi example. The end result would be form control buttons to click on based on the crafts being managed.
Bookmarks