仅从可见表格复制单元格并粘贴到下一个自由列VBA中

我有一个VBA代码,目前正在运行的应用程序单元格(C2:C3)从所有工作表和粘贴在“主”表中。 我的问题是,我希望它只复制可见的工作表,因为我的一些隐藏的工作表有不同的数据运行我的工作表。 我也有一个粘贴在下一行的问题,我想粘贴在下一列,不能弄清楚:/。

Option Explicit Sub Sample() Dim wsInput As Worksheet, wsOutput As Worksheet Dim rng As Range Dim LRowO As Long, LRowI As Long Set wsOutput = ThisWorkbook.Sheets("Master") For Each wsInput In ThisWorkbook.Worksheets If wsInput.Name <> wsOutput.Name Then With wsInput Set rng = .Range("C2:C3") rng.Copy With wsOutput LRowO = .Range("A" & .Rows.Count).End(xlUp).Row + 1 .Range("A" & LRowO).PasteSpecial xlPasteValues, _ Operation:=xlNone, SkipBlanks:=False, Transpose:=False End With End With End If Next wsInput Exit Sub End Sub 

听起来就像你需要检查工作表是否被隐藏,并且跟踪下一列的内容,并在你每次粘贴时增加它。 这里是你的代码修改这两个事情

  Option Explicit Sub Sample() Dim wsInput As Worksheet, wsOutput As Worksheet Dim rng As Range LRowI As Long Dim nextCol as Long Set wsOutput = ThisWorkbook.Sheets("Master") nextCol = 1 For Each wsInput In ThisWorkbook.Worksheets If wsInput.Name <> wsOutput.Name and wsInput.Visible = True Then With wsInput Set rng = .Range("C2:C3") rng.Copy With wsOutput .Cells(1, nextCol).PasteSpecial xlPasteValues, _ Operation:=xlNone, SkipBlanks:=False, Transpose:=False nextCol = nextCol + 1 End With End With End If Next wsInput Exit Sub End Sub 

检查

 worksheets(wsInput).visible 

属性。 例如,改变

 If wsInput.Name<>wsOutput.name Then 

 If wsInput.Name<>wsOutput.name and worksheets(wsInput).visible Then 

这只会使用可见的工作表。

更多信息位于https://msdn.microsoft.com/zh-cn/library/office/ff197786.aspx

我不认为我完全理解你的问题的第二部分。

你应该可以使用这样的东西来检查你的床单是否可见/隐藏。

 If wsInput.Visible = True Then ' Do copy here 

要么

 If Sheets(wsInput.Name).Visible = True Then ' Do copy here 

然后粘贴到下一列,可以使用offset属性。

 ActiveCell.Offset(rowOffset, columnOffset).Activate 

rowOffsetcolumnOffset更改为直接到达单元格所需的数字。 即

 ActiveCell.Offset(0, 1).Activate 

将把当前select从A1更改为B1