Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 475

How can I edit the following Copysheets macro to NOT copy hidden columns/rows?

$
0
0

I have a macro that we use for copying a worksheet into a new workbook. The only issue I am having is that when it copies the worksheet into a new workbook, it copies the hidden columns/rows. What would be the best way to update the macro so that it doesn't copy the hidden columns/rows?

Sub CopySheets()    Dim wkb As Excel.Workbook    Dim newWkb As Excel.Workbook    Dim wks As Excel.Worksheet    Dim newWks As Excel.Worksheet    Dim sheets As Variant    Dim varName As Variant    Dim i As Integer'------------------------------------------------------------    Application.ScreenUpdating = False'Define the names of worksheets to be copied.    sheets = VBA.Array("TAB NAME")'Create reference to the current Excel workbook and to the destination workbook.    Set wkb = Excel.ThisWorkbook    Set newWkb = Excel.Workbooks.Add    For Each varName In sheets'Clear reference to the [wks] variable.        Set wks = Nothing'Check if there is a worksheet with such name.        On Error Resume Next        Set wks = wkb.Worksheets(VBA.CStr(varName))        On Error GoTo 0'If worksheet with such name is not found, those instructions are skipped.        If Not wks Is Nothing Then'Copy this worksheet to a new workbook.            Call wks.Copy(newWkb.Worksheets(1))'Get the reference to the copy of this worksheet and paste'all its content as values.            Set newWks = newWkb.Worksheets(wks.Name)            With newWks                Call .Cells.Copy                Call .Range("A1").PasteSpecial(Paste:=xlValues)                Call .Range("A1").Select            End With        End If'Delete Sheet1 from new workbook        Application.DisplayAlerts = False        For i = newWkb.Worksheets.Count To 2 Step -1        newWkb.Worksheets(i).Delete        Next i    Application.DisplayAlerts = True    Next varName    Application.ScreenUpdating = TrueEnd Sub

Viewing all articles
Browse latest Browse all 475

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>