Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
sarahphonics
 
Posts: n/a
Default macro to copy into different worksheets

I have a excel worksheet which shows a list like below.

Directory of C:\Sarah\files\Dec04

09/02/2005 13:30 6,312 Dec04.sav
1 File(s) 6,312 bytes

Directory of C:\Sarah\code\Admissions\0001

20/04/2004 14:45 290,929,896 adm0001.sav
1 File(s) 290,929,896 bytes


What I want a macro to do is to look down this worksheet and select each
record on the list. I.E find the line starting with the word Directory,
selecting all the lines below until it gets to another line starting with
Directory, it then pastes this into a different worksheet. then goes back and
selects the next record etc. The distance between the rows containing
"Directory" can vary in length.
I am not very experienced in Visual Basic and I can not get it to select the
range of rows I need.
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

How about this?

Option Explicit
Sub testme()
Dim newWks As Worksheet
Dim curWks As Worksheet
Dim TopCell As Range
Dim BotCell As Range
Dim FoundCell As Range
Dim FirstAddress As String
Dim myRng As Range

Set curWks = Worksheets("sheet1")
Set myRng = curWks.Range("a:a")

With curWks
With myRng
Set FoundCell = .Find(what:="directory", _
after:=.Cells(.Cells.Count), _
lookat:=xlPart, LookIn:=xlValues, _
searchdirection:=xlNext)
End With

If FoundCell Is Nothing Then
MsgBox "None found!"
Exit Sub
End If

FirstAddress = FoundCell.Address
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value _
= "Directory Dummy"

Set TopCell = FoundCell

Do
Set FoundCell = myRng.FindNext(FoundCell)

If FoundCell.Address = FirstAddress Then
Exit Do
End If

Set BotCell = FoundCell
Set newWks = Worksheets.Add
.Range(TopCell, BotCell.Offset(-1, 0)).EntireRow.Copy _
Destination:=newWks.Range("a1")

Set TopCell = BotCell

Loop

.Cells(.Rows.Count, "A").End(xlUp).EntireRow.Delete

End With

End Sub


sarahphonics wrote:

I have a excel worksheet which shows a list like below.

Directory of C:\Sarah\files\Dec04

09/02/2005 13:30 6,312 Dec04.sav
1 File(s) 6,312 bytes

Directory of C:\Sarah\code\Admissions\0001

20/04/2004 14:45 290,929,896 adm0001.sav
1 File(s) 290,929,896 bytes

What I want a macro to do is to look down this worksheet and select each
record on the list. I.E find the line starting with the word Directory,
selecting all the lines below until it gets to another line starting with
Directory, it then pastes this into a different worksheet. then goes back and
selects the next record etc. The distance between the rows containing
"Directory" can vary in length.
I am not very experienced in Visual Basic and I can not get it to select the
range of rows I need.


--

Dave Peterson
  #3   Report Post  
sarahphonics
 
Posts: n/a
Default

Thank you very much, that worked perfectly.

"Dave Peterson" wrote:

How about this?

Option Explicit
Sub testme()
Dim newWks As Worksheet
Dim curWks As Worksheet
Dim TopCell As Range
Dim BotCell As Range
Dim FoundCell As Range
Dim FirstAddress As String
Dim myRng As Range

Set curWks = Worksheets("sheet1")
Set myRng = curWks.Range("a:a")

With curWks
With myRng
Set FoundCell = .Find(what:="directory", _
after:=.Cells(.Cells.Count), _
lookat:=xlPart, LookIn:=xlValues, _
searchdirection:=xlNext)
End With

If FoundCell Is Nothing Then
MsgBox "None found!"
Exit Sub
End If

FirstAddress = FoundCell.Address
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value _
= "Directory Dummy"

Set TopCell = FoundCell

Do
Set FoundCell = myRng.FindNext(FoundCell)

If FoundCell.Address = FirstAddress Then
Exit Do
End If

Set BotCell = FoundCell
Set newWks = Worksheets.Add
.Range(TopCell, BotCell.Offset(-1, 0)).EntireRow.Copy _
Destination:=newWks.Range("a1")

Set TopCell = BotCell

Loop

.Cells(.Rows.Count, "A").End(xlUp).EntireRow.Delete

End With

End Sub


sarahphonics wrote:

I have a excel worksheet which shows a list like below.

Directory of C:\Sarah\files\Dec04

09/02/2005 13:30 6,312 Dec04.sav
1 File(s) 6,312 bytes

Directory of C:\Sarah\code\Admissions\0001

20/04/2004 14:45 290,929,896 adm0001.sav
1 File(s) 290,929,896 bytes

What I want a macro to do is to look down this worksheet and select each
record on the list. I.E find the line starting with the word Directory,
selecting all the lines below until it gets to another line starting with
Directory, it then pastes this into a different worksheet. then goes back and
selects the next record etc. The distance between the rows containing
"Directory" can vary in length.
I am not very experienced in Visual Basic and I can not get it to select the
range of rows I need.


--

Dave Peterson

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
Copy a Macro Phil K Excel Discussion (Misc queries) 3 June 9th 05 01:52 PM
Why do my dates change when I copy them between Excel worksheets? rrjohnsonia Excel Worksheet Functions 6 June 1st 05 09:42 PM
Copy Excel worksheets TedDSC Excel Discussion (Misc queries) 2 December 27th 04 09:59 PM
Macro with Range of Worksheets Chris E. New Users to Excel 8 December 3rd 04 04:56 PM
copy pivot table to multiple worksheets Todd Excel Worksheet Functions 2 November 19th 04 03:16 AM


All times are GMT +1. The time now is 12:43 AM.

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"