Need to fix macro to list data underneath each other
Hi Jeannie
A couple of problems. You are using the same variable twice ("n") to
count the row numbers.
Also, when counting for Sheet Jane D, you are counting in column A, not
column Q.
Here is your code amended, so that it works. I have also shown a
shortened version below, which avoids all of the selecting, and runs
much quicker.
Sub Transactions()
'
' Transactions Macro
' Macro recorded 6/23/2007 by Jeannie Vincovich
'
' Keyboard Shortcut: Ctrl+n
'
Dim n As Integer, i As Integer, j As Integer
Dim rng As Range
Sheets("Transactions").Activate
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
Sheets("Transactions").Activate
If Cells(i, "A").Value = "Jane Doe" Then
Range("B" & i & ":C" & i).Copy
Sheets("Jane D").Activate
If Range("Q4") = "" Then
Set rng = Worksheets("Jane D").Range("Q4")
rng.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Else
Sheets("Jane D").Activate
j = Cells(Rows.Count, "Q").End(xlUp).Row + 1
Set rng = Worksheets("Jane D").Range("Q" & j)
rng.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End If
End If
Next i
End Sub
And here is the slightly more efficient code to do the same task.
Sub Transactions()
'
' Transactions Macro
' Macro recorded 6/23/2007 by Jeannie Vincovich
'
' Keyboard Shortcut: Ctrl+n
'
Dim n As Integer, i As Integer, j As Integer
Dim rng As Range, dest As Range
Dim wss As Worksheet, wsd As Worksheet
Set wss = Sheets("Transactions")
Set wsd = Sheets("Jane D")
n = wss.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
j = wsd.Cells(Rows.Count, "Q").End(xlUp).Row + 1
j = WorksheetFunction.Max(4, j)
Set dest = wsd.Range("Q" & j & ":R" & j)
If wss.Cells(i, "A").Value = "Jane Doe" Then
wss.Range("B" & i & ":C" & i).Copy dest
dest = dest.Value
End If
Next i
End Sub
--
Regards
Roger Govier
"jeannie v" wrote in message
...
Hi :
I want this macro to give me the data in Columns B & C of a particular
person in Column A on "Transactions" Worksheet and enter in it Q4 and
R4 on
the Named Worksheet. What happens is that it doesn't list all of the
transactions for that person one underneath the other in Q4 and
R4....it
enters the first transaction in Q4 & R4 but the just enters the last
transaction in Q44 & R44...with no transactions in between or
underneath each
other.
Can someone help with this problem:
Sub Transactions()
'
' Transactions Macro
' Macro recorded 6/23/2007 by Jeannie Vincovich
'
' Keyboard Shortcut: Ctrl+n
'
Dim n As Integer, i As Integer
Dim rng As Range
Sheets("Transactions").Activate
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
Sheets("Transactions").Activate
If Cells(i, "A").Value = "Jane Doe" Then
Range("B" & i & ":C" & i).Copy
Sheets("Jane D").Activate
If Range("Q4") = "" Then
Set rng = Worksheets("Jane D").Range("Q4")
rng.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Else
Sheets("Jane D").Activate
n = Cells(Rows.Count, "A").End(xlUp).Row + 1
Set rng = Worksheets("Jane D").Range("Q" & n)
rng.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End If
End If
Next i
End Sub
--
jeannie v
|