I have some macros that copy Sheet 2 from my exsisting work book to a new work book. This code works as it should except that there are hidden rows that should not be shown on the new work book.
Here is the code that I have written that copies the sheet over and pastes only its values:
Dim Output As Workbook
Dim FileName As String
Set Output = Workbooks.Add
Application.DisplayAlerts = False
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
FileName = ThisWorkbook.Path & "\" & ThisWorkbook.Worksheets("Quote Questions").Range("AK545").Value & ".xls"
Output.SaveAs FileName
So where would the code go that will only display the unhidden cells and not the hidden ones?
EDIT The code has changed slightly after an answer was submitted. Here is more info. Some of cells in the sheet that is being copied are merged and I get an error on the line of code:
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
Saying: Cannot change part of a merged cell
, so im guessing there needs to another piece to add?
I do not want to go to the sheet and have un-merge all the cells manually.
Replace the line
ThisWorkbook.Worksheets("Quote & Proposal").Cells.Copy
with
ThisWorkbook.Worksheets("Quote & Proposal").Cells. _
SpecialCells(xlCellTypeVisible).Copy
and it should work.
You can check if the Row is hidden with this code
Sub RowIsHidden()
For i = 1 To 7
MsgBox Cells(i, 1).EntireRow.Hidden
Next
End Sub
This is similar to your code above. Instead of index of the sheet you could also use the sheetname
Sub CopyOnlyValuesFromSheet()
' Copy all Cells from first Sheet (SheetIndex =1)
ThisWorkbook.Worksheets(1).Cells.Copy
' Select second Sheet (SheetIndex =2)
ThisWorkbook.Worksheets(2).Select
' Paste only values into Selection
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
End Sub
I have tried to use Cells(i, 1).EntireRow.Delete Shift:=xlUp
but since this has consequence over which rownumber i you have to iterate next it is easier to just clear the values
Sub RowIsHiddenClearValue()
For i = 1 To 10
If Cells(i, 1).EntireRow.Hidden Then
Cells(i, 1).EntireRow.Value = ""
End If
Next
End Sub
Make sure that the cursor in the destination sheet is placed in the first cell.
Sub AnotherAnswer()
Call CopyValuesOfVisibleRows("Quote & Proposal", "Quote Questions")
End Sub
Sub CopyValuesOfVisibleRows(sourceSheetName, destinationSheetName)
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
ThisWorkbook.Worksheets(destinationSheetName).Paste
End Sub
If you need more pointers to put the pieces together please provide more details with which parts you have problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.