将数据粘贴到新工作表上,并隐藏所有单元格

时间:2016-12-14 05:53:32

标签: excel vba excel-vba

由于某种原因,每次运行此代码时,它都会隐藏新工作表上的所有数据 行高设置为0.

我必须使用鼠标拉动行高以使最后一个单元格可见然后从那里我可以单击一个单元格并“起床”到数据。

我该如何解决这个问题?这很烦人 它是我的代码的东西还是我需要在粘贴这样的数据后设置行高?

enter image description here

enter image description here

NumMax = NumMax + 1 'there is more code above that sets NumMax

ThisWorkbook.Activate
ThisWorkbook.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = "XCFIL_" & NumMax
ThisWorkbook.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = "Resultat_" & NumMax

Sheets("XCFIL").Activate
Cells.Copy
Sheets("XCFIL_" & NumMax).Activate
Range("A1").PasteSpecial xlPasteAll
Range("A1").Select ' a try to get up in the sheet, but it does not work

编辑:代码窗格: enter image description here

1 个答案:

答案 0 :(得分:1)

根据聊天情况,快速解决方案是:

'Other option is to simply copy/paste the sheet
NumMax = NumMax + 1 'there is more code above that sets NumMax
With ThisWorkbook
.Sheets("XCFIL").Copy After:=.Sheets(.Sheets.Count)
ActiveSheet.Name = "XCFIL_" & NumMax
End With

但是,我正在研究下面的内容,这可能也有效。

Sub t()
Dim originWS As Worksheet, destWS As Worksheet
Dim NumMax As Long

NumMax = NumMax + 1 'there is more code above that sets NumMax

With ThisWorkbook
Set originWS = .Sheets("XCFIL")
Set destWS = .Sheets.Add(After:=.Sheets(.Sheets.Count))
destWS.Name = "XCFIL_" & NumMax
End With

Dim copyRng As Range
Dim lastCol As Long, lastRow As Long

With originWS
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set copyRng = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) 'assuming your data starts in A1
End With

destWS.Range(destWS.Cells(1, 1), destWS.Cells(lastRow, lastCol)).Value = _
    copyRng.Value
End Sub

作为一个说明,avoid using .Select/.Activate总是一个好主意。

此外,这并没有解决隐藏在PasteSpecial上的行的非常奇怪的问题。 ...但是哦,如果它有效,它就可以了。