VBA遍历所有sheet,找到前6位名称为“Daily&”的sheet,然后复制每个sheet里面的AD列数据,分别依次粘贴到名称为“月”的sheet中,粘贴顺序从C列开始往后面列顺移
时间: 2023-07-15 15:14:38 浏览: 41
可以使用以下的VBA代码实现:
```
Sub CopyDataToMonthlySheet()
Dim monthlySheet As Worksheet
Set monthlySheet = ThisWorkbook.Sheets("月")
Dim sourceSheet As Worksheet
For Each sourceSheet In ThisWorkbook.Sheets
If Left(sourceSheet.Name, 6) = "Daily&" Then
Dim lastColumn As Long
lastColumn = monthlySheet.Cells(1, Columns.Count).End(xlToLeft).Column
If lastColumn > 2 Then
lastColumn = lastColumn + 1
End If
sourceSheet.Range("AD:AD").Copy monthlySheet.Cells(1, lastColumn + 1)
End If
Next sourceSheet
End Sub
```
代码中首先获取到名称为“月”的sheet,然后遍历所有的sheet,找到前6位名称为“Daily&”的sheet。对于每个符合条件的sheet,我们获取月sheet的最后一列,然后将AD列的数据复制到月sheet的下一个空列。注意粘贴的起始列为C列,所以需要加上1。
希望能对你有所帮助!