copy specific columns to other worksheet with vba

bflatcoat

New Member
Joined
Jan 31, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have data on sheet1: one row per item. I would like to copy and transpose certain columns but not all to sheet2.
lines with "not" in the B column need to be skipped. see the example.

hope you can help!

what I have
Knipsel.JPG


What I need
Knipsel.JPG2.JPG
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try:

VBA Code:
Sub ReplicateData()
 Dim k As Long, r As Range, rng As Range
  Application.ScreenUpdating = False
  Sheets("Sheet2").Cells = ""
  With Sheets("Sheet1")
   On Error Resume Next
   .AutoFilter.ShowAllData
   On Error GoTo 0
   .Range("A1:D1").AutoFilter 2, "<>" & "not"
   Set rng = .Range("A1:A" & .Cells(Rows.Count, 1).End(3).Row).SpecialCells(12)
   .Columns(3).Hidden = True
   For Each r In rng
    .Range(r, r.Offset(, 3)).SpecialCells(12).Copy
    Sheets("Sheet2").Cells(Rows.Count, k + 1).End(3)(2).PasteSpecial Transpose:=True: k = k + 1
   Next r
   .Columns(3).Hidden = False
   .AutoFilter.ShowAllData
  End With
End Sub
 
Upvote 0
Another way:
VBA Code:
Sub TransposeIfNot()
Application.ScreenUpdating = False
With Sheets("Sheet1").Range("A1").CurrentRegion
    For i = 1 To .Rows.Count
        .Rows(i).Copy
        Sheets("Sheet2").Columns(i).Cells(1, 1).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Next i
    Application.CutCopyMode = False
    With Sheets("Sheet2").Range("A1").CurrentRegion
        On Error Resume Next
        .Columns(1).Replace "city", "#N/A"
        .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
        .Rows(2).Replace "not", "#N/A"
        .SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
    End With
End With
Application.ScreenUpdating = tru
        
End Sub
 
Upvote 0
Solution
Thank you! both options worked. second was a bit nicer since it doesn't change sheet 1 to include autofilter and after running it the cells in the laat row are still selected.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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