detweiler
Board Regular
- Joined
- Aug 2, 2013
- Messages
- 62
Did a lot of skulking around the inter webs and frankencoded the following to gather that should pull variables from specific cells on multiple worksheets within a workbook into a newly created worksheet in that same workbook. The macro runs and get the message box saying the data extraction is done, but the new worksheet is not gettin created.
Here's what I was able to put together. Your comments ad suggestions are appreciated.
Here's what I was able to put together. Your comments ad suggestions are appreciated.
VBA Code:
Sub ExtractDataToSummary()
Dim ws As Worksheet
Dim infoSheet As Worksheet
Dim infoRow As Long
Dim data(1 To 3) As Variant
Dim excludeSheets As Collection
Dim sheetName As String
' don't know why, but this is the part that isn't working; gets skipped when stepping through the code
' here's where the new worksheet is supposed to get created after checking it's not there
Set infoSheet = ThisWorkbook.Sheets("Info List")
On Error GoTo 0
If infoSheet Is Nothing Then
Set infoSheet = ThisWorkbook.Sheets.Add
infoSheet.Name = "Info List"
End If
' begin to build the list of the pulled data
infoSheet.Cells(1, 1).Value = "Worksheet Name"
infoSheet.Cells(1, 2).Value = "B2"
infoSheet.Cells(1, 3).Value = "B3"
infoSheet.Cells(1, 4).Value = "B7"
infoRow = 2
'need to exclude support worksheets
Set excludeSheets = New Collection
excludeSheets.Add "thisWorksheet"
excludeSheets.Add "thatWorksheet"
' go through each worksheet and collect data
For Each ws In ThisWorkbook.Worksheets
sheetName = ws.Name
If Not IsInCollection(excludeSheets, sheetName) Then
data(1) = ws.Range("B2").Value
data(2) = ws.Range("B3").Value
data(3) = ws.Range("B7").Value
' put the data on the list sheet
infoSheet.Cells(summaryRow, 1).Value = sheetName
infoSheet.Cells(summaryRow, 2).Value = data(1)
infoSheet.Cells(summaryRow, 3).Value = data(2)
infoSheet.Cells(summaryRow, 4).Value = data(3)
infoRow = summaryRow + 1
End If
Next ws
MsgBox "Data extraction complete!", vbInformation
End Sub
Function IsInCollection(coll As Collection, key As Variant) As Boolean
Dim item As Variant
On Error Resume Next
item = coll(key)
IsInCollection = (Err.Number = 0)
Err.Clear
End Function