Domanda

I'm quite new to VBA and I've managed to get by using the forum and a bit of cut and pasta here and there but it would seem I can go no further. I have a workbook that contains 100 worksheets titled 1_OUT_IX_-500.TXT, 2_OUT_IX_-490.TXT and so on till 101_OUT_500.TXT, I'm trying to copy data from the same range on all sheets specifically AO2:AQ2 and pasta it into a new summary sheet in the range B2:D2 so the first row is bllank to allow me to add headers. So the summary sheet would look something like

-500 '1_OUT_IX_-500'$AO$2 '1_OUT_IX_-500'$AP$2 '1_OUT_IX_-500'$AQ$2

-490 '2_OUT_IX_-490'$AO$2 '2_OUT_IX_-490'$AP$2 '2_OUT_IX_-490'$AQ$2


500 '101_OUT_IX_500'$AO$2 '101_OUT_IX_500'$AP$2 '101_OUT_IX_500'$AQ$2

Any Help is greatly appreciated Thank you in advance

È stato utile?

Soluzione

Try this code:

 Sub test()
    Dim summarySheet As Worksheet
    Dim sh As Worksheet

    Dim j As Integer

    'change "Summary" to the sheet name that is true for you'
    Set summarySheet = ThisWorkbook.Worksheets.Add
    summarySheet.name = "Summary"

    'number of first row where need to paste in summary sheet'
    j = 2
    'loop throught all sheets'
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> summarySheet.Name Then
            summarySheet.Range("B" & j & ":D" & j).Value = _
                sh.Range("AO2:AQ2").Value
            j = j + 1
        End If
    Next

End Sub

Note, that you need to specify correct name for Summary sheet in line Set summarySheet = ThisWorkbook.Worksheets("Summary"))

Altri suggerimenti

You can do this without using Macro for it. You can simply use INDIRECT function.

Example:

+----+------------------+--------------------+-------+---------------------+
| Id |       Plan       |     Plan Name      | Range |        Value        |
+----+------------------+--------------------+-------+---------------------+
|  1 | _OUT_IX_-500.TXT | =A2 & B$2          | !A1   | =INDIRECT(C2 & D$2) |
|  2 |                  | 2_OUT_IX_-500.TXT  |       |                     |
|  3 |                  | 3_OUT_IX_-500.TXT  |       |                     |
|  4 |                  | 4_OUT_IX_-500.TXT  |       |                     |
|  5 |                  | 5_OUT_IX_-500.TXT  |       |                     |
|  6 |                  | 6_OUT_IX_-500.TXT  |       |                     |
|  7 |                  | 7_OUT_IX_-500.TXT  |       |                     |
|  8 |                  | 8_OUT_IX_-500.TXT  |       |                     |
|  9 |                  | 9_OUT_IX_-500.TXT  |       |                     |
| 10 |                  | 10_OUT_IX_-500.TXT |       |                     |
|    |                  |                    |       |                     |
+----+------------------+--------------------+-------+---------------------+

I put this code in Summary sheet and put Id table in cell A1.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top