Filter the data and copy it to another sheet with conditions

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello, I want to filter column A on a specific value and copy the data to another sheet so that the data is pasted info b12 . For example, the problem we are facing is that I want to copy the value of cell M2 from sheet 1 to sheet 2 next to the transferred values and with the same number of rows that were filtered and transferred.
in column A and E
And when re-continuous again is copying down the old data

Public Sub drewhx15()
Dim Rng As Range

With ThisWorkbook.Worksheets("feuil1")
Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
With Rng
.AutoFilter Field:=1, Criteria1:="ok"
.Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy ThisWorkbook.Worksheets("Sh").Range("b12")
.AutoFilter Field:=1, Criteria1:="yes"
.Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy ThisWorkbook.Worksheets("Sh").Range("F12")
.Parent.AutoFilterMode = False
End With
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I want to copy the value of cell M2 from sheet 1 to sheet 2 next to the transferred values
I hope I have understood correctly, try the following:
VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range
  Dim lr As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
  
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    .AutoFilter Field:=1, Criteria1:="ok"
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("b12")
    lr = sh2.Range("B" & Rows.Count).End(3).Row
    sh2.Range("D12:D" & lr).Value = sh1.Range("M2").Value
    
    .AutoFilter Field:=1, Criteria1:="yes"
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F12")
    lr = sh2.Range("F" & Rows.Count).End(3).Row
    sh2.Range("H12:H" & lr).Value = sh1.Range("M2").Value
    
    .Parent.AutoFilterMode = False
  End With
End Sub
 
Upvote 0
I hope I have understood correctly, try the following:
VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range
  Dim lr As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
 
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    .AutoFilter Field:=1, Criteria1:="ok"
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("b12")
    lr = sh2.Range("B" & Rows.Count).End(3).Row
    sh2.Range("D12:D" & lr).Value = sh1.Range("M2").Value
   
    .AutoFilter Field:=1, Criteria1:="yes"
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F12")
    lr = sh2.Range("F" & Rows.Count).End(3).Row
    sh2.Range("H12:H" & lr).Value = sh1.Range("M2").Value
   
    .Parent.AutoFilterMode = False
  End With
End Sub
Always creative, thank you very much. Is there a possibility to keep the old data and copy the new ones below it?
 
Upvote 0
Try :

VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range
  Dim lr As Long, lr2 As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
  
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    .AutoFilter Field:=1, Criteria1:="ok"
    lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
    lr2 = sh2.Range("B" & Rows.Count).End(3).Row
    sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
    
    .AutoFilter Field:=1, Criteria1:="yes"
    lr = sh2.Range("F" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F" & lr)
    lr2 = sh2.Range("F" & Rows.Count).End(3).Row
    sh2.Range("H" & lr & ":H" & lr2).Value = sh1.Range("M2").Value
    
    .Parent.AutoFilterMode = False
  End With
End Sub
 
Upvote 0
Solution
Try :

VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range
  Dim lr As Long, lr2 As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
 
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    .AutoFilter Field:=1, Criteria1:="ok"
    lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
    lr2 = sh2.Range("B" & Rows.Count).End(3).Row
    sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
   
    .AutoFilter Field:=1, Criteria1:="yes"
    lr = sh2.Range("F" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F" & lr)
    lr2 = sh2.Range("F" & Rows.Count).End(3).Row
    sh2.Range("H" & lr & ":H" & lr2).Value = sh1.Range("M2").Value
   
    .Parent.AutoFilterMode = False
  End With
End Sub
Very nice job thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,762
Members
449,048
Latest member
excelknuckles

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