LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default complicated question for copying ranges to new sheets

This works so well ... I really like this ... thank you for your help!


"John Williams" wrote in message
m...
Sub Macro1()
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



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF question complicated [email protected] Excel Discussion (Misc queries) 4 January 25th 09 02:23 PM
Complicated FV Question Emma Hope Excel Worksheet Functions 5 October 18th 08 04:00 AM
Complicated question, for me anyway! :) Peter Doak Excel Worksheet Functions 2 March 18th 07 08:34 PM
Complicated Question kyrospeare Excel Worksheet Functions 5 April 27th 06 02:45 AM
Complicated question... ozdemir Excel Worksheet Functions 3 December 7th 05 09:37 PM


All times are GMT +1. The time now is 03:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"