Edit the code to Multiple Columns

Status
Not open for further replies.

ayman helmy

New Member
Joined
Mar 17, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
hello All

i use below code to copy filtered data and paste them is same range for Column Z

i need to edit it to contain more columns until column BI


VBA Code:
Sub CopyToZ()
   
    Dim ws As Worksheet: Set ws = ActiveSheet ' be more specific
   
    ' First Cell of the Data Range (in the row below headers)
    Dim fCell As Range: Set fCell = ws.Range("Z3")
    ' Last Cell of the Filtered Range
    Dim lCell As Range: Set lCell = ws.Range("Z" & ws.Rows.Count).End(xlUp)
    ' If no filtered data, the last cell will be the header cell, which
    ' is above the first cell. Check this with:
    If lCell.Row < fCell.Row Then Exit Sub ' no filtered data
   
    ' Range from First Cell to Last Cell
    Dim rg As Range: Set rg = ws.Range(fCell, lCell)
   
    ' Filtered Data Range
    Dim frg As Range: Set frg = rg.SpecialCells(xlCellTypeVisible)
   
    ' Area Range
    Dim arg As Range
   
    For Each arg In frg.Areas
        ' Either copy values (more efficient (faster))...
        arg.EntireRow.Columns("Z").Value = arg.Value
        ' ... or copy values, formulas and formatting
        'arg.Copy arg.EntireRow.Columns("Y")
    Next arg
End Sub
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try such correction:
change source range to Columns("Z:BI") and resize output range to 36 columns (Z:BI is 36) .Resize(, 36)
and let us know is it what you ment by "more columns until column BI"

so the code will read:
VBA Code:
    For Each arg In frg.Areas
        ' Either copy values (more efficient (faster))...
        arg.EntireRow.Columns("Z:BI").Value = arg.Resize(, 36).Value
        ' ... or copy values, formulas and formatting
        'arg.Copy arg.EntireRow.Columns("Y")
    Next arg
 
Upvote 1
Solution
Status
Not open for further replies.

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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