copying information and pasting in another sheet

Roooster

New Member
Joined
Aug 16, 2011
Messages
8
What I am trying to do: Find where a row has the name "Craig Bethune" in it then select the corresponding cells in column M,O,Q,S and U (this will open a "new sheet") Copy the data that is opened and paste it in the next open row in a sheet called "Prime Broker"

My Problem: It seems like the macro is skipping over some cells in the corresponding columns with a number greater than 0.

Another wierd thing is it starts at a15 instead of a1 on the prime broker sheet.

Here is my Macro code, including the creation of the new sheet

Sub Problem()
'
'select daily summary tab and add new sheet
'
'
Sheets("Daily Summary by Value Date").Select
Sheets.Add
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Prime Broker"
Sheets("Daily Summary by Value Date").Select
'
'copy and paste info from daily values into Prime Broker sheet
'
'
Dim cell As Range, area As Range, x As Integer
x = 0

Set area = Sheets("Daily Summary by Value Date").Range("l1:l250")
For Each cell In area
If cell.Value = "Craig Bethune" Then
'Cell M
If cell.Offset(0, 13) > 0 Then
Sheets("Daily Summary by Value Date").Select
cell.Offset(0, 13).Select
'x = x + cell.Offset(0, 13)
Selection.ShowDetail = True
Selection.Copy
Sheets("Prime Broker").Select
Range("a1").Offset(1, 0).Select
ActiveSheet.Paste
End If
'cell O
If cell.Offset(0, 15) > 0 Then
Sheets("Daily Summary by Value Date").Select
cell.Offset(0, 15).Select
'x = x + cell.Offset(0, 15)
Selection.ShowDetail = True
Selection.Copy
Sheets("Prime Broker").Select
Range("a1").Offset(x, 0).Select
ActiveSheet.Paste
End If
'cell Q
If cell.Offset(0, 17) > 0 Then
Sheets("Daily Summary by Value Date").Select
cell.Offset(0, 17).Select
'x = x + cell.Offset(0, 17)
Selection.ShowDetail = True
Selection.Copy
Sheets("Prime Broker").Select
Range("a1").Offset(x, 0).Select
ActiveSheet.Paste
End If
'cell S
If cell.Offset(0, 19) > 0 Then
Sheets("Daily Summary by Value Date").Select
cell.Offset(0, 19).Select
'x = x + cell.Offset(0, 19)
Selection.ShowDetail = True
Selection.Copy
Sheets("Prime Broker").Select
Range("a1").Offset(x, 0).Select
ActiveSheet.Paste
End If
'cell U
If cell.Offset(0, 21) > 0 Then
Sheets("Daily Summary by Value Date").Select
cell.Offset(0, 21).Select
'x = x + cell.Offset(0, 21)
Selection.ShowDetail = True
Selection.Copy
Sheets("Prime Broker").Select
Range("a1").Offset(x, 0).Select
ActiveSheet.Paste
End If
'x = x + 1
End If
Next cell

End Sub


any help would be greatly appreciated
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
My Problem: It seems like the macro is skipping over some cells in the corresponding columns with a number greater than 0.

Well, what would you expect with code like...

Rich (BB code):
For Each cell In area
    If cell.Value = "Craig Bethune" Then
'Cell M
    If cell.Offset(0, 13) > 0 Then
        Sheets("Daily Summary by Value Date").Select
        cell.Offset(0, 13).Select
        'x = x + cell.Offset(0, 13)
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(1, 0).Select
        ActiveSheet.Paste
    End If

Did you use F8 to step through the code lines and see what happens, 1 line at the time?

PS: Please use code tags when you paste VBA code on the forum.
To add code tags, you should type in your post:

Rich (BB code):
then paste your code, and lastly, type:

['/code]

WITHOUT the ' in front of the /

Thanks for the consideration.
 
Upvote 0
I was unaware I could do that with f8 and the code tags. I will use both of those in the future.

As for now I am kinda shooting in the dark by searching the Internet and what I remember of logic from visual basic.

Thanks again for the help!
 
Upvote 0
F8 doesn't tell me what each line of code is doing. If it matters I'm working on excel 2k3.

Could you tell me how to select a cell in column m when it finds the name Craig Bethune in column l?
 
Upvote 0
Your variable called cell represents cells in column I. If you use cell.Offset(0, 4), you go 4 cells to the right of that cell, so effectively the cell on the same row but in column M. With that knowledge, you can now work on the "new" cell. For instance, checking the contents of that cell using cell.Offset(0, 4).Value
 
Upvote 0
Next question, I am so close. Does excel count hidden cells when going over on the offset?

Ex: say I am going to column M it's (0,13) if 4 columns are hidden before M would it be (0, 9)?
 
Upvote 0
That's very odd then, there are a total of 17 cells it should be selecting with a total of 538 records. I'm getting 6 cells opened and 334 records.
 
Upvote 0
That's very odd then, there are a total of 17 cells it should be selecting with a total of 538 records. I'm getting 6 cells opened and 334 records.

Then there must be something else going on AFAIK.
 
Upvote 0
Let me start by saying you have been a huge help! I have got this almost working!

Now it is pasting over some of the records it has already pasted. I have tried to add extra rows in, but it doesn't seem to work. Here is my code

Code:
Dim cell As Range, area As Range, x As Integer
x = 0

Set area = Sheets("Daily Summary by Value Date").Range("d1:d250")
For Each cell In area
    If cell.Value = "Craig Bethune" Then
'Cell M
    If cell.Offset(0, 9).Value > 0 Then
        x = x + cell.Offset(0, 9).Value + 2
        Sheets("Daily Summary by Value Date").Select
        cell.Offset(0, 9).Select
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(1, 0).Select
        ActiveSheet.Paste
        Sheets("Daily Summary by Value Date").Select
    End If
'cell O
    If cell.Offset(0, 11).Value > 0 Then
        x = x + cell.Offset(0, 11).Value + 2
        cell.Offset(0, 11).Select
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(x, 0).Select
        ActiveSheet.Paste
        Sheets("Daily Summary by Value Date").Select
    End If
'cell Q
    If cell.Offset(0, 13).Value > 0 Then
        x = x + cell.Offset(0, 13).Value + 2
        cell.Offset(0, 13).Select
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(x, 0).Select
        ActiveSheet.Paste
        Sheets("Daily Summary by Value Date").Select
    End If
'cell S
    If cell.Offset(0, 15).Value > 0 Then
        x = x + cell.Offset(0, 15).Value + 2
        cell.Offset(0, 15).Select
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(x, 0).Select
        ActiveSheet.Paste
        Sheets("Daily Summary by Value Date").Select
    End If
'cell U
    If cell.Offset(0, 17).Value > 0 Then
        x = x + cell.Offset(0, 17).Value + 2
        cell.Offset(0, 17).Select
        Selection.ShowDetail = True
        Selection.Copy
        Sheets("Prime Broker").Select
        Range("a1").Offset(x, 0).Select
        ActiveSheet.Paste
        Sheets("Daily Summary by Value Date").Select
        x = x + cell.Offset(0, 17).Value + 2
     End If
     
    End If
Next cell

So somethign is wrong with my count, I think.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top