excel help
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] excel help

17 Posts
6 Users
0 Reactions
52 Views
 mrmo
Posts: 10687
Free Member
Topic starter
 

Hopefully the following is fairly self explanatory

I am trying to copy data from one worksheet to another.

Problem is everytime i run the function all that happens is that the data is copied from the same row on one worksheet to the same row on the other. I am guessing my use of the copy destination is wrong?

Sub Workbook_Open()
Dim rowcountold As Integer
Dim t As Integer

'count the number of rows
rowcountold = Worksheets("page").UsedRange.Rows.count
'Loop through counting the rows and each that matches the criteria copy to another worksheet
t = 1
For i = 1 To rowcountold

If Cells(i, 1).Value = "Cash Sale B80" Then Rows(i).Copy Destination:=Worksheets("Sheet1").Rows(t)
t = t + 1
rowcountold = rowcountold + 1

Next

End Sub


 
Posted : 16/03/2009 10:24 am
Posts: 0
Free Member
 

copy and paste special


 
Posted : 16/03/2009 10:44 am
 mrmo
Posts: 10687
Free Member
Topic starter
 

copy paste won't work when you have 60,000 lines and you only want some of them.

I need to look at the first column, if the contents match what i want then copy the entire row to another worksheet. starting at row 1 and going from there.


 
Posted : 16/03/2009 10:55 am
Posts: 36
Free Member
 

have you tried a conditional forumla with Indirect?


 
Posted : 16/03/2009 10:57 am
 mrmo
Posts: 10687
Free Member
Topic starter
 

again not an option, the above is me trying to understand the code, in reality their are 60 different conditions depending on the match to be copied to 10 different worksheets,

I can't see a way of avoiding VBA, i can carry on as i am doing which is pivot tables and cut and paste. But i thought if i can get this right it will save time and help me learn a bit more in the process.

I can get it to work, i just don't understand why it won't paste into the first row but into the same row in the new worksheet as the data came out of the old worksheet.


 
Posted : 16/03/2009 11:03 am
Posts: 36
Free Member
 

=IF(INDIRECT("Sheet2!"&CELL("address",$A1))="Cash Sale B80", INDIRECT("Sheet2!"&CELL("address",A1)), "")

I cant help with the VBA Im afraid, but this may be of use ^


 
Posted : 16/03/2009 11:12 am
 mrmo
Posts: 10687
Free Member
Topic starter
 

sorted

Sub Workbook_Open()
Dim rowcountold As Integer
Dim t As Integer

'count the number of rows
rowcountold = Worksheets("page").UsedRange.Rows.count
'Loop through counting the rows and each that matches the criteria copy to another worksheet

t = 1
For i = 1 To rowcountold
If Cells(i, 1).Value = "Cash Sale B80" Then
Rows(i).Copy Destination:=Worksheets("Sheet1").Rows(t)
t = t + 1
End If
rowcountold = rowcountold + 1

Next i

End Sub


 
Posted : 16/03/2009 11:22 am
Posts: 36
Free Member
 

just the "End if" missing?


 
Posted : 16/03/2009 11:23 am
Posts: 2
Free Member
 

What is this line for?

rowcountold = rowcountold + 1

Doesn't it create a never ending loop (as the end figure increases by one each time the counter i increases)


 
Posted : 16/03/2009 11:29 am
 mrmo
Posts: 10687
Free Member
Topic starter
 

mb, a bit of junk that i hadn't deleted that seems to have no effect?

I think the problem was with the t=t+1 it would always count because it was outside the if then loop. by splitting the if statement and using the end if i can get more if then options.

I think....


 
Posted : 16/03/2009 11:53 am
 mrmo
Posts: 10687
Free Member
Topic starter
 

ok for the development of this problem. I am sure there are variables defined that i don't need and variable undefined...

This works as i want it too, bar one issue, because "A" counts across all the 8 worksheets i have set up, i have a lot of empty rows.

Suggestions as to the best way of removing these empty rows.

As i see it two ways, i can get the data copied to the right cell in the first place.

So i need to d something to

[i]Worksheets("data").Rows(i).Copy Destination:=Worksheets(region).Rows(A)[/i]

Or i clean the data afterwards and remove the empty cells.

Ideas gratefully received!

[i]Sub OrganiseData2()
Dim rowcountold As Integer
Dim i As Integer
Dim region As String
Dim companyname As String
Dim ws As Worksheet
Dim rng As range
Dim z As Integer

Set ws = Sheets("accounts")
Set rng = ws.range("a2:c200")

'count the number of rows
rowcountold = Worksheets("data").UsedRange.Rows.Count
'Loop through counting the rows and each that matches the criteria copy to another worksheet

A = 4
For z = 2 To 117
For i = 1 To rowcountold
companyname = Worksheets("accounts").Cells(z, 1).Value
If Cells(i, 1).Value = companyname Then
region = Application.WorksheetFunction.VLookup(companyname, rng, 3, 0)
Worksheets("data").Rows(i).Copy Destination:=Worksheets(region).Rows(A)
A = A + 1
Else: End If

Next i
Next z

End Sub[/i]


 
Posted : 16/03/2009 4:16 pm
Posts: 36
Free Member
 

Or i clean the data afterwards and remove the empty cells.

doesnt hurt my brain as much... 🙂


 
Posted : 16/03/2009 4:17 pm
 mrmo
Posts: 10687
Free Member
Topic starter
 

I know what you mean...my head hurts from getting this far. Be nice to get the whole problem solved with code, just so i know how.


 
Posted : 16/03/2009 4:34 pm
Posts: 1642
Free Member
 

If you are able to send me the spreadsheet + vb I'll have a look. What you are trying to do looks fairly straight forward but it's a bit difficult to visualise the data layout and how it all relates to be sure.


 
Posted : 16/03/2009 8:09 pm
Posts: 54
Free Member
 

You could just replace A with a last Row function.

i.e Worksheets("data").Rows(i).Copy Destination:=Worksheets(region).Rows(Worksheets(region).UsedRange.Rows.Count + 1)

So it gets the last row everytime...


 
Posted : 16/03/2009 8:59 pm
 mrmo
Posts: 10687
Free Member
Topic starter
 

pp, thanks for the offer, if you let me know your email, i'll send it from work tomorrow,


 
Posted : 16/03/2009 9:32 pm
Posts: 1642
Free Member
 

My email is nick.welsh@hotmail.co.uk


 
Posted : 17/03/2009 1:19 pm
Posts: 1642
Free Member
 

Sorry mrmo my email is nick[b]_[/b]welsh@hotmail.co.uk !!!


 
Posted : 17/03/2009 9:30 pm

6 DAYS LEFT
We are currently at 95% of our target!