A
Annette
This works great except it will stop if in the list the office is not found.
Sheet 1 contains all the offices and sheet2 contains all the information and
list of offices and orders (not all offices orders). I need to email back
a list of the orders to the offices, but this code stops completely if while
it runs, a particular office is not found. For example:
If this is my list on Sheet 1
Chicago
Atlanta
Orlando
Detroit
And sheet 2 has Chicago and Detroit listed, Orlando and Detroit will never
get there spreadsheets separated because Atlanta was not in the mix ... I
need this code to overlook that missing office and move along!
================
Sub SeparatetoPrepareForEmailing()
Dim iRow, endRow As Integer
Worksheets("Sheet1").Select
Range("A1").Select
'For each office listed in Sheet1 copy the orders listed in Sheet2
endRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
For iRow = 1 To endRow
Worksheets("Sheet1").Select
Call copyOrders(Cells(iRow, 1).Value, "Sheet2")
Next
End Sub
Sub copyOrders(office As String, ordersSheet As String)
Dim officeSheet
Dim ordersRange As Range
Static iRow As Integer 'Retain row number where office
search ended
Dim lastOrdersRow, startRow, endRow As Long
Sheets(ordersSheet).Select
Range("A1").Select
'Look through the orders sheet and determine the start and end rows for
the orders relating to
'the current office. This allows us to copy and paste the block of
orders in one operation
'instead of individually.
startRow = 0
endRow = 0
iRow = iRow + 1
lastOrdersRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
While iRow <= lastOrdersRow + 1 And endRow = 0
If Cells(iRow, 1) = office And startRow = 0 Then
startRow = iRow
ElseIf Cells(iRow, 1) <> office And startRow <> 0 Then
endRow = iRow - 1
Else
iRow = iRow + 1
End If
Wend
iRow = iRow - 1
'If there are any orders for this office
If endRow <> 0 Then
'Create a new worksheet if it doesn't exist
If worksheetExists(office) Then
Set officeSheet = Sheets(office)
Else
Set officeSheet = Sheets.Add
officeSheet.name = office 'Name sheet as the office
End If
'Copy the office name and related orders to the office sheet.
'This assumes that the office name is in column A, and the orders
are in column B
Sheets(ordersSheet).Select
Set ordersRange = Range(Cells(startRow, 1), Cells(endRow, 2))
ordersRange.Select
Selection.Copy
Sheets(office).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
Function worksheetExists(WSName As String, Optional WB As Workbook) As
Boolean
On Error Resume Next
worksheetExists = CBool(Len(IIf(WB Is Nothing, ActiveWorkbook,
WB).Worksheets(WSName).name))
End Function
Sheet 1 contains all the offices and sheet2 contains all the information and
list of offices and orders (not all offices orders). I need to email back
a list of the orders to the offices, but this code stops completely if while
it runs, a particular office is not found. For example:
If this is my list on Sheet 1
Chicago
Atlanta
Orlando
Detroit
And sheet 2 has Chicago and Detroit listed, Orlando and Detroit will never
get there spreadsheets separated because Atlanta was not in the mix ... I
need this code to overlook that missing office and move along!
================
Sub SeparatetoPrepareForEmailing()
Dim iRow, endRow As Integer
Worksheets("Sheet1").Select
Range("A1").Select
'For each office listed in Sheet1 copy the orders listed in Sheet2
endRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
For iRow = 1 To endRow
Worksheets("Sheet1").Select
Call copyOrders(Cells(iRow, 1).Value, "Sheet2")
Next
End Sub
Sub copyOrders(office As String, ordersSheet As String)
Dim officeSheet
Dim ordersRange As Range
Static iRow As Integer 'Retain row number where office
search ended
Dim lastOrdersRow, startRow, endRow As Long
Sheets(ordersSheet).Select
Range("A1").Select
'Look through the orders sheet and determine the start and end rows for
the orders relating to
'the current office. This allows us to copy and paste the block of
orders in one operation
'instead of individually.
startRow = 0
endRow = 0
iRow = iRow + 1
lastOrdersRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
While iRow <= lastOrdersRow + 1 And endRow = 0
If Cells(iRow, 1) = office And startRow = 0 Then
startRow = iRow
ElseIf Cells(iRow, 1) <> office And startRow <> 0 Then
endRow = iRow - 1
Else
iRow = iRow + 1
End If
Wend
iRow = iRow - 1
'If there are any orders for this office
If endRow <> 0 Then
'Create a new worksheet if it doesn't exist
If worksheetExists(office) Then
Set officeSheet = Sheets(office)
Else
Set officeSheet = Sheets.Add
officeSheet.name = office 'Name sheet as the office
End If
'Copy the office name and related orders to the office sheet.
'This assumes that the office name is in column A, and the orders
are in column B
Sheets(ordersSheet).Select
Set ordersRange = Range(Cells(startRow, 1), Cells(endRow, 2))
ordersRange.Select
Selection.Copy
Sheets(office).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
Function worksheetExists(WSName As String, Optional WB As Workbook) As
Boolean
On Error Resume Next
worksheetExists = CBool(Len(IIf(WB Is Nothing, ActiveWorkbook,
WB).Worksheets(WSName).name))
End Function