Pulling data from a single cell in multiple tabs

时间:2015-07-28 16:02:21

标签: excel vba excel-vba

I am not a savvy VBA user, but I believe this question is possible. I am attempting to compile data from multiple worksheets (100+) in an Excel workbook into a "summary worksheet". Each worksheet is identical and the data would be pulled from the same cell in each worksheet onto the same cell in the summary.

Is there a code that will do this for me while also being able to exclude certain other worksheets? If so, what is the code?

1 个答案:

答案 0 :(得分:0)

Dim ws As Excel.Worksheet
Dim TargetSheet As Excel.Worksheet
Dim iIndexas Long
Dim lRow As Long
Dim strValue as string

    lRow = 1

    'Loop each worksheet in the workbook
    For iIndex = 1 To Application.Worksheets.count

        'Get the value from this worksheet
        Set ws = Application.Worksheets(iIndex)
        ws.Activate

        'Check if the name has template in it
        if instr(ucase(ws.name), "TEMPLATE") = 0 then

            strValue = ws.Range("B4").Value

            'Switch to the target sheet
            Set TargetSheet = ActiveWorkbook.Sheets("SummarySheetName")
            TargetSheet.Activate

            'Write the value
            TargetSheet.Range("C" & lRow).Value = strValue

            'Increment your row
            lRow = lRow  + 1

        End if

    Next iIndex