-
Jul 25th, 2020, 02:47 AM
#1
Thread Starter
Junior Member
[RESOLVED] Cut & paste the data to new sheet if condition matches
Hi Experts,
I am looking for a macro that will do the things mentioned below
macro will be placed in macro.xlsm,both files are located in different places so the path will be hardcoded in the macro so that i can change it as per my needs
sheet name can be anything
Plz see the sample file & the output file
If column D of 1.xls equal to column E of 1.xls or column D of 1.xls equal to column F of 1.xls then cut that row & paste it to new sheet in 1.xls(Plz name the new sheet as output2)
(header will be same in new sheet also)
Thnx For the Help
https://drive.google.com/file/d/15LM...ew?usp=sharing
https://drive.google.com/file/d/1ifJ...ew?usp=sharing
-
Jul 25th, 2020, 05:10 AM
#2
Re: Cut & paste the data to new sheet if condition matches
you can test this to see if it does what you require
Code:
Dim wb As Workbook, output As Worksheet, rng As Range, rw As Long
Set wb = Workbooks("1.xls") ' if already open
' OR
Set wb = Workbooks.Open("fullpath\1.xls")
rw = 2 ' don't check header row
With wb.Sheets(1)
.Cells(1, 1).EntireRow.Copy output.Cells(1, 1)
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For rw = 2 To lastrow
If .Cells(rw, 4) = .Cells(rw, 5) Or .Cells(rw, 4) = .Cells(rw, 6) Then
If rng Is Nothing Then Set rng = .Cells(rw, 1).EntireRow
Else: Set rng = Union(rng, .Cells(rw, 1).EntireRow)
End If
Next
If Not rng Is Nothing Then ' some rows to move
Set output = Sheets.Add
output.Name = "output2"
.Cells(1, 1).EntireRow.Copy output.Cells(1, 1)
rng.Cut output.Cells(2, 1)
End If
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 25th, 2020, 05:34 AM
#3
Thread Starter
Junior Member
Re: Cut & paste the data to new sheet if condition matches
Code:
Sub PART1()
Dim wb As Workbook, output As Worksheet, rng As Range, rw As Long
Set wb = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
rw = 2
With wb.Worksheets.Item(1)
.Cells(1, 1).EntireRow.Copy output.Cells(1, 1)
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For rw = 2 To lastrow
If .Cells(rw, 4) = .Cells(rw, 5) Or .Cells(rw, 4) = .Cells(rw, 6) Then
If rng Is Nothing Then Set rng = .Cells(rw, 1).EntireRow
Else: Set rng = Union(rng, .Cells(rw, 1).EntireRow)
End If
Next
If Not rng Is Nothing Then
Set output = Sheets.Add
output.Name = "output2"
.Cells(1, 1).EntireRow.Copy output.Cells(1, 1)
rng.Cut output.Cells(2, 1)
End If
wb.Save
wb.Close
End Sub
Compile error
expected end with
-
Jul 26th, 2020, 05:17 AM
#4
Re: Cut & paste the data to new sheet if condition matches
i must have missed the last line when i copy and pasted, after the last end if
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 29th, 2020, 07:15 AM
#5
Thread Starter
Junior Member
Re: [RESOLVED] Cut & paste the data to new sheet if condition matches
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|