一个工作簿中有n个工作表,首先需要提取所以工作表的名称到sheet1,根据sheet的名称索引每个工作表的单元格b11,d11,f11,h11,j11,l11,011的信息,用vba完成。
时间: 2024-03-03 08:51:57 浏览: 102
好的,这是一个VBA代码实现的问题。以下是实现该功能的VBA代码:
```
Sub ExtractDataFromWorksheets()
Dim wb As Workbook
Dim ws As Worksheet
Dim sheetNames() As String
Dim i As Integer
Set wb = ActiveWorkbook
ReDim sheetNames(1 To wb.Sheets.Count)
'获取所有工作表的名称
For Each ws In wb.Sheets
i = i + 1
sheetNames(i) = ws.Name
Next ws
'将工作表名称写入sheet1的A列
With wb.Sheets("Sheet1").Range("A1")
.Resize(UBound(sheetNames)).Value = Application.Transpose(sheetNames)
End With
'根据工作表名称索引单元格并提取数据
For i = 2 To UBound(sheetNames) + 1
With wb.Sheets(sheetNames(i - 1))
wb.Sheets("Sheet1").Cells(i, 2).Value = .Range("B11").Value
wb.Sheets("Sheet1").Cells(i, 3).Value = .Range("D11").Value
wb.Sheets("Sheet1").Cells(i, 4).Value = .Range("F11").Value
wb.Sheets("Sheet1").Cells(i, 5).Value = .Range("H11").Value
wb.Sheets("Sheet1").Cells(i, 6).Value = .Range("J11").Value
wb.Sheets("Sheet1").Cells(i, 7).Value = .Range("L11").Value
wb.Sheets("Sheet1").Cells(i, 8).Value = .Range("O11").Value
End With
Next i
End Sub
```
此代码将所有工作表的名称提取到Sheet1的A列中,并在B到O列中提取每个工作表的B11、D11、F11、H11、J11、L11和O11单元格的值。
阅读全文