PC Review


Reply
Thread Tools Rate Thread

Copy rows if

 
 
Kashyap
Guest
Posts: n/a
 
      11th May 2009
I was trying to copy rows from Sheet2 to Sheet3 if Sheet2 A:A.value= Sheet1
B1.value
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      11th May 2009
Dear Kashya

Try the below and feedback (untested)

Sub MyMacro()
Dim lngRow As Long
Dim lngLastRow As Long
Dim lngNewRow As Long
Dim varTemp As Variant

lngLastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
lngNewRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For lngRow = 2 To lngLastRow
If Sheets("Sheet1").Range("A" & lngRow) = Sheets("Sheet1").Range("B1") Then
varTemp = Sheets("Sheet1").Range(lngRow & ":" & lngRow)
Sheets(2).Range(lngRow & ":" & lngRow) = varTemp
lngNewRow = lngNewRow + 1
End If
Next

End Sub




--
If this post helps click Yes
---------------
Jacob Skaria


"Kashyap" wrote:

> I was trying to copy rows from Sheet2 to Sheet3 if Sheet2 A:A.value= Sheet1
> B1.value

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      11th May 2009
Modified to suit your requirement Sheet2 to Sheet3 referring Sheet1 B1....

Sub MyMacro()
Dim lngRow As Long
Dim lngLastRow As Long
Dim lngNewRow As Long
Dim varTemp As Variant

lngLastRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
lngNewRow = Sheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row
For lngRow = 1 To lngLastRow
If Sheets("Sheet2").Range("A" & lngRow) = Sheets("Sheet1").Range("B1") Then
varTemp = Sheets("Sheet2").Range(lngRow & ":" & lngRow)
Sheets(3).Range(lngNewRow & ":" & lngNewRow) = varTemp
lngNewRow = lngNewRow + 1
End If
Next

End Sub


--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

> Dear Kashya
>
> Try the below and feedback (untested)
>
> Sub MyMacro()
> Dim lngRow As Long
> Dim lngLastRow As Long
> Dim lngNewRow As Long
> Dim varTemp As Variant
>
> lngLastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
> lngNewRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
> For lngRow = 2 To lngLastRow
> If Sheets("Sheet1").Range("A" & lngRow) = Sheets("Sheet1").Range("B1") Then
> varTemp = Sheets("Sheet1").Range(lngRow & ":" & lngRow)
> Sheets(2).Range(lngRow & ":" & lngRow) = varTemp
> lngNewRow = lngNewRow + 1
> End If
> Next
>
> End Sub
>
>
>
>
> --
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Kashyap" wrote:
>
> > I was trying to copy rows from Sheet2 to Sheet3 if Sheet2 A:A.value= Sheet1
> > B1.value

 
Reply With Quote
 
Kashyap
Guest
Posts: n/a
 
      11th May 2009
Hi Jakob, I'm not able to get any result..
 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      11th May 2009
I have tried with the below data in a new workbook. Since I havent refered
the workbook name it works with the active workbook. Try the below

Sheet1 B1 = 1
Sheet2 Col A and Col B with below data

1 a
2 b
1 a
2 b
1 a

and Sheet3 blank


If this post helps click Yes
---------------
Jacob Skaria


"Kashyap" wrote:

> Hi Jakob, I'm not able to get any result..

 
Reply With Quote
 
Kashyap
Guest
Posts: n/a
 
      11th May 2009
Sorry Jocob, there was a slight error while editing.. Its working fine now..
can we also copy rows from Sheet2 to Sheet3 if

Sheet2 A:A.value>= Sheet1 B1.value & Sheet2 A:A.value<= Sheet1 B2.value ?
 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      11th May 2009
Hi Kashya

Try and feedback..

Sub MyMacro()
Dim lngRow As Long
Dim lngLastRow As Long
Dim lngNewRow As Long
Dim varTemp As Variant

lngLastRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
lngNewRow = Sheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row + 1
For lngRow = 1 To lngLastRow
If Sheets("Sheet2").Range("A" & lngRow) >= Sheets("Sheet1").Range("B1") And _
Sheets("Sheet2").Range("A" & lngRow) <= Sheets("Sheet1").Range("B2") Then
varTemp = Sheets("Sheet2").Range(lngRow & ":" & lngRow)
Sheets(3).Range(lngNewRow & ":" & lngNewRow) = varTemp
lngNewRow = lngNewRow + 1
End If
Next

End Sub


If this post helps click Yes
---------------
Jacob Skaria


"Kashyap" wrote:

> Sorry Jocob, there was a slight error while editing.. Its working fine now..
> can we also copy rows from Sheet2 to Sheet3 if
>
> Sheet2 A:A.value>= Sheet1 B1.value & Sheet2 A:A.value<= Sheet1 B2.value ?

 
Reply With Quote
 
Kashyap
Guest
Posts: n/a
 
      11th May 2009
Perfect, Jacob.. Thank you..
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy pasting Rows, but need to Delete any Shapes/Pictures that are within copied rows Corey Microsoft Excel Programming 2 1st Aug 2007 02:02 AM
Copy rows of data (eliminating blank rows) from fixed layout =?Utf-8?B?U3dlZXBlYQ==?= Microsoft Excel Misc 1 13th Mar 2007 11:05 PM
Hide Rows - copy and paste only rows that show =?Utf-8?B?QWNjZXNzMTAx?= Microsoft Excel Worksheet Functions 3 1st Mar 2006 12:39 AM
How do I copy rows with hidden rows between without unhiding data. =?Utf-8?B?QmlnZ2llIEo=?= Microsoft Excel Misc 1 11th Oct 2004 08:57 PM
Copy Rows and insert these rows before a page break AQ Mahomed Microsoft Excel Programming 0 8th Jun 2004 09:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:24 AM.