Copying only the visible cells from visible worksheets into a new workbook, excel 2007 VBA

geekypenguin
  • I have a master spreadsheet Master Spreadsheet.xlsm and I want to use it to create another spreadsheet defined by OutputFN.
  • This second spreadsheet needs to be a copy of the first but only containing the visible cells from visible worksheets in the first.

I have found code to copy just the visible sheets and other code to copy just the visible cells but not the two together. Any help would be much appreciated.

This is what I've got so far:

Private Sub saveone()

Dim OutputFN As String
Dim OutputWB As Workbook
Dim SourceWB As Workbook
Dim i As Integer

i = 1
Set SourceWB = Application.ActiveWorkbook
OutputFN = ThisWorkbook.Worksheets("Setup Page").Range("B12").Value
Set OutputWB = Workbooks.Add


'Selects active (not hidden cells) from visible sheets and copies

For Each Sheet In ThisWorkbook.Sheets
If Sheet.Visible = True Then
ThisWorkbook.ActiveSheet.Cells. _
SpecialCells(xlCellTypeVisible).Copy

'Pastes into new workbook
Worksheets(i).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats

'Saves new file as output filename in the directory created earlier
 ActiveWorkbook.SaveAs (OutputFN)

i = i + 1
End If
Next

End Sub
brettdj

Something like this

I've tidied up the variables and tweaked the logic a little as well

Private Sub saveone()

Dim OutputFN As String
Dim OutputWB As Workbook
Dim SourceWB As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet    

Set SourceWB = ThisWorkbook
OutputFN = SourceWB.Worksheets("Setup Page").Range("B12").Value
Set OutputWB = Workbooks.Add(1)   

Application.ScreenUpdating = False

For Each ws In SourceWB.Sheets
    If ws.Visible Then
    Set ws2 = OutputWB.Sheets.Add(After:=OutputWB.Sheets(OutputWB.Sheets.Count))
    ws.Cells.SpecialCells(xlCellTypeVisible).Copy
    ws2.[a1].PasteSpecial xlPasteValues
    ws2.[a1].PasteSpecial xlPasteFormats
    End If
Next

Application.ScreenUpdating = True
ActiveWorkbook.SaveAs (OutputFN)    

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA copying visible cells only issues

From Dev

How to copy only visible cells from workbook to workbook?

From Dev

VBA Excel: Show visible cells in listbox only

From Dev

Summing visible cells in excel VBA

From Dev

Excel VBA - Paste to visible cells

From Dev

VBA: Need to loop only through visible cells in Excel

From Dev

VBA Save ONLY VISIBLE Sheet as Workbook

From Dev

Power Query - extracting only visible/filtered cells from an excel file

From Dev

vbscript : To Search a word only from the visible cells in an excel

From Dev

Excel Issue with CountIf with visible cells only

From Dev

Excel Issue with CountIf with visible cells only

From Dev

Handle visible cells in vba

From Dev

Select only visible cells

From Dev

vba excel copy only visible cells on key press ctrl+c for protected sheet

From Dev

Excel VBA Concatenate only visible cells of filtered column. Test code included

From Dev

How can I stop Excel (2007) from copying hidden cells?

From Dev

Copy Only Cells From Visible Sheets And Paste Into Next Free Column VBA

From Dev

Copying worksheets from multiple workbooks into current workbook

From Dev

vba code to select only visible cells in specific column except heading

From Dev

Index/Match Visible Cells Only

From Dev

Load only visible cells in UITableView

From Dev

Only selected cells are visible in datagrid

From Dev

row count for visible cells only

From Dev

Excel VBA To Split Workbook Every 4 Worksheets

From Dev

Retrieving a list of row number from only visible cells

From Dev

How to get cells value from previous visible row in excel formulas

From Dev

Excel VBA, Show comments on new sheet if the cell it is in is visible

From Dev

Excel VBA Copying from Various cells from various Workbooks

From Dev

excel VBA, create a column with sheet or file name in cells while copying multiple CSV files to one workbook

Related Related

  1. 1

    VBA copying visible cells only issues

  2. 2

    How to copy only visible cells from workbook to workbook?

  3. 3

    VBA Excel: Show visible cells in listbox only

  4. 4

    Summing visible cells in excel VBA

  5. 5

    Excel VBA - Paste to visible cells

  6. 6

    VBA: Need to loop only through visible cells in Excel

  7. 7

    VBA Save ONLY VISIBLE Sheet as Workbook

  8. 8

    Power Query - extracting only visible/filtered cells from an excel file

  9. 9

    vbscript : To Search a word only from the visible cells in an excel

  10. 10

    Excel Issue with CountIf with visible cells only

  11. 11

    Excel Issue with CountIf with visible cells only

  12. 12

    Handle visible cells in vba

  13. 13

    Select only visible cells

  14. 14

    vba excel copy only visible cells on key press ctrl+c for protected sheet

  15. 15

    Excel VBA Concatenate only visible cells of filtered column. Test code included

  16. 16

    How can I stop Excel (2007) from copying hidden cells?

  17. 17

    Copy Only Cells From Visible Sheets And Paste Into Next Free Column VBA

  18. 18

    Copying worksheets from multiple workbooks into current workbook

  19. 19

    vba code to select only visible cells in specific column except heading

  20. 20

    Index/Match Visible Cells Only

  21. 21

    Load only visible cells in UITableView

  22. 22

    Only selected cells are visible in datagrid

  23. 23

    row count for visible cells only

  24. 24

    Excel VBA To Split Workbook Every 4 Worksheets

  25. 25

    Retrieving a list of row number from only visible cells

  26. 26

    How to get cells value from previous visible row in excel formulas

  27. 27

    Excel VBA, Show comments on new sheet if the cell it is in is visible

  28. 28

    Excel VBA Copying from Various cells from various Workbooks

  29. 29

    excel VBA, create a column with sheet or file name in cells while copying multiple CSV files to one workbook

HotTag

Archive