#1   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Move Data

Hi folks,

I need a help on Excel macro. I have a spreadsheet which called sheet1 and
has thousands of records and group by departments. I want to create a macro
which can move the same departments records to the new sheet and rename the
sheet to the department name. Could anyone show me the code? Any help will
be appreciated?

Thanks in advance.

Tim.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Move Data

Try:

http://www.rondebruin.nl/copy5.htm#all

Hope this helps
Rowan

Tim wrote:
Hi folks,

I need a help on Excel macro. I have a spreadsheet which called sheet1 and
has thousands of records and group by departments. I want to create a macro
which can move the same departments records to the new sheet and rename the
sheet to the department name. Could anyone show me the code? Any help will
be appreciated?

Thanks in advance.

Tim.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Move Data

keeping it really simple

Option Explicit
Sub test()
CopyRows "x"
End Sub

Sub CopyRows(dept As String)
Dim targetIndex As Long
Dim sourceIndex As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet

Set sourceSheet = Worksheets("sheet1")
Set targetSheet = GetSheet(dept)

Application.ScreenUpdating = False

For sourceIndex = 1 To sourceSheet.Range("A65000").End(xlUp).Row

If sourceSheet.Cells(sourceIndex, 1).Value = dept Then

targetIndex = targetIndex + 1
targetSheet.Rows(targetIndex).Value = _
sourceSheet.Rows(sourceIndex).Value

End If

Next

Application.ScreenUpdating = True

End Sub
Function GetSheet(sheetsname As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(sheetsname)
If Err.Number = 0 Then
GetSheet.Cells.Clear
Else
Set GetSheet = Worksheets.Add
GetSheet.Name = sheetsname
End If
On Error GoTo 0
End Function


this should be easy enough. screen updating speeds things up quite a bit
we simple check each row to see if the cell in A matches teh dept, then we
copy it. we keep a record of which is the nest row to update.

Its probably faster to use 'FIND' so I'll add that routine in my next mail
shortly


"Tim" wrote:

Hi folks,

I need a help on Excel macro. I have a spreadsheet which called sheet1 and
has thousands of records and group by departments. I want to create a macro
which can move the same departments records to the new sheet and rename the
sheet to the department name. Could anyone show me the code? Any help will
be appreciated?

Thanks in advance.

Tim.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Move Data

using FIND for speeier execution

Option Explicit
Sub test()
CopyRows "x"
End Sub

Sub CopyRows(dept As String)
Dim targetIndex As Long
Dim sourceIndex As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet

Dim found As Range
Dim firstaddress As String

Set sourceSheet = Worksheets("sheet1")

Set targetSheet = GetSheet(dept)
Application.ScreenUpdating = False

Set found = sourceSheet.Range("A:A").Find(dept)
If Not found Is Nothing Then
firstaddress = found.Address
Do
targetIndex = targetIndex + 1
sourceIndex = found.Row
targetSheet.Rows(targetIndex).Value = _
sourceSheet.Rows(sourceIndex).Value
Set found = sourceSheet.Range("A:A").FindNext(found)
Loop Until firstaddress = found.Address

End If



Application.ScreenUpdating = True



End Sub
Function GetSheet(sheetsname As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(sheetsname)
If Err.Number = 0 Then
GetSheet.Cells.Clear
Else
Set GetSheet = Worksheets.Add
GetSheet.Name = sheetsname
End If
On Error GoTo 0
End Function



"Patrick Molloy" wrote:

keeping it really simple

Option Explicit
Sub test()
CopyRows "x"
End Sub

Sub CopyRows(dept As String)
Dim targetIndex As Long
Dim sourceIndex As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet

Set sourceSheet = Worksheets("sheet1")
Set targetSheet = GetSheet(dept)

Application.ScreenUpdating = False

For sourceIndex = 1 To sourceSheet.Range("A65000").End(xlUp).Row

If sourceSheet.Cells(sourceIndex, 1).Value = dept Then

targetIndex = targetIndex + 1
targetSheet.Rows(targetIndex).Value = _
sourceSheet.Rows(sourceIndex).Value

End If

Next

Application.ScreenUpdating = True

End Sub
Function GetSheet(sheetsname As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(sheetsname)
If Err.Number = 0 Then
GetSheet.Cells.Clear
Else
Set GetSheet = Worksheets.Add
GetSheet.Name = sheetsname
End If
On Error GoTo 0
End Function


this should be easy enough. screen updating speeds things up quite a bit
we simple check each row to see if the cell in A matches teh dept, then we
copy it. we keep a record of which is the nest row to update.

Its probably faster to use 'FIND' so I'll add that routine in my next mail
shortly


"Tim" wrote:

Hi folks,

I need a help on Excel macro. I have a spreadsheet which called sheet1 and
has thousands of records and group by departments. I want to create a macro
which can move the same departments records to the new sheet and rename the
sheet to the department name. Could anyone show me the code? Any help will
be appreciated?

Thanks in advance.

Tim.

  #5   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Move Data

Patrick and Rowan,

Thank you very much for your help and time. The solutions work great.

Tim.

"Patrick Molloy" wrote:

using FIND for speeier execution

Option Explicit
Sub test()
CopyRows "x"
End Sub

Sub CopyRows(dept As String)
Dim targetIndex As Long
Dim sourceIndex As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet

Dim found As Range
Dim firstaddress As String

Set sourceSheet = Worksheets("sheet1")

Set targetSheet = GetSheet(dept)
Application.ScreenUpdating = False

Set found = sourceSheet.Range("A:A").Find(dept)
If Not found Is Nothing Then
firstaddress = found.Address
Do
targetIndex = targetIndex + 1
sourceIndex = found.Row
targetSheet.Rows(targetIndex).Value = _
sourceSheet.Rows(sourceIndex).Value
Set found = sourceSheet.Range("A:A").FindNext(found)
Loop Until firstaddress = found.Address

End If



Application.ScreenUpdating = True



End Sub
Function GetSheet(sheetsname As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(sheetsname)
If Err.Number = 0 Then
GetSheet.Cells.Clear
Else
Set GetSheet = Worksheets.Add
GetSheet.Name = sheetsname
End If
On Error GoTo 0
End Function



"Patrick Molloy" wrote:

keeping it really simple

Option Explicit
Sub test()
CopyRows "x"
End Sub

Sub CopyRows(dept As String)
Dim targetIndex As Long
Dim sourceIndex As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet

Set sourceSheet = Worksheets("sheet1")
Set targetSheet = GetSheet(dept)

Application.ScreenUpdating = False

For sourceIndex = 1 To sourceSheet.Range("A65000").End(xlUp).Row

If sourceSheet.Cells(sourceIndex, 1).Value = dept Then

targetIndex = targetIndex + 1
targetSheet.Rows(targetIndex).Value = _
sourceSheet.Rows(sourceIndex).Value

End If

Next

Application.ScreenUpdating = True

End Sub
Function GetSheet(sheetsname As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(sheetsname)
If Err.Number = 0 Then
GetSheet.Cells.Clear
Else
Set GetSheet = Worksheets.Add
GetSheet.Name = sheetsname
End If
On Error GoTo 0
End Function


this should be easy enough. screen updating speeds things up quite a bit
we simple check each row to see if the cell in A matches teh dept, then we
copy it. we keep a record of which is the nest row to update.

Its probably faster to use 'FIND' so I'll add that routine in my next mail
shortly


"Tim" wrote:

Hi folks,

I need a help on Excel macro. I have a spreadsheet which called sheet1 and
has thousands of records and group by departments. I want to create a macro
which can move the same departments records to the new sheet and rename the
sheet to the department name. Could anyone show me the code? Any help will
be appreciated?

Thanks in advance.

Tim.

Reply
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
Move data Its me[_3_] Excel Worksheet Functions 2 November 29th 09 04:37 AM
Move data? L. Setting up and Configuration of Excel 12 January 24th 06 10:13 PM
Macro to move data to different column based on data in another co malycom Excel Discussion (Misc queries) 3 August 2nd 05 07:07 PM
enter data in cell which will start macro to move data to sheet2 Tommy Excel Discussion (Misc queries) 0 May 12th 05 05:00 PM
create macro to move label type data to column data JonathonWood9 Excel Programming 4 February 21st 05 10:53 PM


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

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

About Us

"It's about Microsoft Excel"