VBA: if any cell in column A = "value", then copy...

JennV

New Member
Joined
May 9, 2019
Messages
34
Hello,

I'm hoping to get some help with VBA coding. If there are cells in column A that are equal to "value" in Sheet1 then I would like to copy the values of the cells in the same row but in columns D and F to Sheet2.

For example, A4 = "value" so I want to copy the values in D4 and F4 to Sheet2.

Thank you in advance!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
Sub t()
Dim rng As Range
With Sheets("Sheet1")
    Set rng = Union(.Range("D:D"), .Range("F:F"))
    .Range("A2", .Cells(Rows.Count, 1).End(xlUp)).AutoFilter 1, "value"
    Intersect(rng, .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible)).Copy Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)(2)
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
Try this

The data is pasted on sheet2 from cell A1

Code:
Sub Copy_Values()
    Dim ws As Worksheet, lr As Long
    Set ws = Sheets("Sheet1")
    lr = Range("A" & Rows.Count).End(xlUp).Row
    ws.Range("A1:F" & lr).AutoFilter 1, "value"
    ws.AutoFilter.Range.Range("D2:D" & lr & ",F2:F" & lr).Copy Sheets("Sheet2").Range("A1")
    ws.ShowAllData
End Sub
 
Upvote 0
Thank you for the replies! I truly appreciate it!

I just have a follow-up question, is there a way to not copy over the fill/text colors/etc.? My data on Sheet1 has fill/text colors/etc. but I don't want that on Sheet2.

Thank you again!
 
Upvote 0
This will only paste the valuies

Code:
Sub t()
Dim rng As Range
   With Sheets("Sheet1")
        Set rng = Union(.Range("D:D"), .Range("F:F"))
        .Range("A2", .Cells(Rows.Count, 1).End(xlUp)).AutoFilter 1, "value"
        Intersect(rng, .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible)).Copy 
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues
        .AutoFilterMode = False
    End With
End Sub
 
Last edited:
Upvote 0
Thank you for the replies! I truly appreciate it!

I just have a follow-up question, is there a way to not copy over the fill/text colors/etc.? My data on Sheet1 has fill/text colors/etc. but I don't want that on Sheet2.

Thank you again!

Use this

Code:
    Dim ws As Worksheet, lr As Long
    Set ws = Sheets("Sheet1")
    lr = Range("A" & Rows.Count).End(xlUp).Row
    ws.Range("A1:F" & lr).AutoFilter 1, "value"
    ws.AutoFilter.Range.Range("D2:D" & lr & ",F2:F" & lr).Copy 
    Sheets("Sheet2").Range("A1").[COLOR=#333333]PasteSpecial xlPasteValues[/COLOR]
    ws.ShowAllData
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,217,311
Messages
6,135,780
Members
449,963
Latest member
ethanong89

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