Excel VBA /macros需要帮助 – 隐藏的行

我敢肯定,这是一个相对简单的查询,但要说我是一个业余与VBA将是一个恭维。

我想要做的是使用macrosbutton从一张表复制某些信息到主表。 这工作正常,直到我隐藏行(总共有880行,并给予这些坐在旁边的数据录入表,我需要隐藏他们,以方便导航)。

这是我目前使用的代码 – 是否可以修改,以包括隐藏的行?

先谢谢你,

Private Sub CopyDataTeam1() Application.ScreenUpdating = False Dim copySheet As Worksheet Dim pasteSheet As Worksheet Set copySheet = ActiveSheet Set pasteSheet = Worksheets("MainData") copySheet.Range("AY5:BC5", copySheet.Range("AY5:BC5").End(xlDown)).Copy pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues Sheets("MainData").Cells.Replace What:="-", Replacement:="", LookAt:=xlWhole, SearchOrder:= _ xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False Application.CutCopyMode = False Application.ScreenUpdating = True End Sub 

一个解决scheme可能是检查您的工作表首先隐藏的行。 将其保存到Range对象。

取消隐藏你的范围,做你的东西,然后重新隐藏你的范围…

 'pass in a worksheet, and get all the hidden rows Function HiddenRange(ws As Worksheet) As Range Dim hideRange As Range Dim column As Long 'use column a column = 1 'if your hidden rows are at the end of your sheet, then '.End(xlUp) may not capture the end of the sheet correctly. 'could use UsedRange.Rows instead.. For i = 1 To ws.UsedRange.Rows.Count 'ws.Cells(ws.Rows.Count, column).End(xlUp).Row If ws.Rows(i).Hidden Then If hideRange Is Nothing Then Set hideRange = ws.Rows(i) Else Set hideRange = Application.Union(ws.Rows(i), hideRange) End If End If Next i 'return our hidden range If hideRange Is Nothing = False Then Set HiddenRange = hideRange End If End Function Public Sub UsageExample() Dim rng As Range Application.ScreenUpdating = False Application.Calculation = xlCalculationManual 'disable error tracking as we get type mismatch if rng is set to nothing On Error Resume Next Set rng = HiddenRange(Sheet1) 'resume error handling On Error GoTo err If Not rng Is Nothing Then rng.Rows.Hidden = False 'do your stuff in here If Not rng Is Nothing Then rng.Rows.Hidden = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Exit Sub err: Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True MsgBox err.Description, vbExclamation, "An error occured" End Sub 

您也可能想要更改您设置复印范围的方式。 不要使用xlDown,就像在BC列中有空白单元格一样,范围将不会被正确设置。

将其更改为下面将根据列BC的底部值设置范围

 copySheet.Range(copySheet.Range("AY5"), copySheet.Range("BC" & copySheet.Rows.Count).End(xlUp)).Copy