简体   繁体   English

为多个工作表运行相同的VBA宏代码

[英]Run Same VBA Macro Code for more than one Worksheet

My following code runs perfectly, it is just copying and pasting data from one workbook to another workbook called 'summary workbook'. 我下面的代码运行完美,它只是将数据从一个工作簿复制并粘贴到另一个称为“摘要工作簿”的工作簿。 Every workbook corresponds to a gage station in which I am extracting the same cells from all the workbooks onto a 'summary workbook' that contains all the data. 每个工作簿都对应一个量具站,在该站中,我将从所有工作簿中提取相同的单元格到包含所有数据的“摘要工作簿”中。 I wanted to know if there is a way to repeat the same code for each gage workbook, so I wouldn't have to manually change it? 我想知道是否有一种方法可以为每个量具工作簿重复相同的代码,所以我不必手动更改它? I know I would have to change the workbook it is copying it from as well, or would I have to change that manually as well? 我知道我也必须更改要从中复制它的工作簿,还是我也必须手动更改它?

Additional info: 附加信息:

The workbook that I am copying is called "run_10296500.xlsm", and within that workbook I am taking out the data in the sheet called "dashboard". 我正在复制的工作簿称为“ run_10296500.xlsm”,在该工作簿中,我将取出工作表中的数据,称为“仪表板”。 The "summary workbook" is the active workbook, and sheet1 is in this workbook. “摘要工作簿”是活动工作簿,并且sheet1在此工作簿中。

The code I have is as follows: 我的代码如下:

Sub Hungry4Gages()

Dim x As Workbook

Dim y As Workbook

'## Open both workbooks first:

Set y = ActiveWorkbook


 Dim LastRow As Long

    For i = 1 To 50
        If Cells(i, 1).Value = "Grand Total" Then
            Line = i
            Range("A" & i + 1 & ":CS50").Select
            Selection.Clear
            Exit For
        End If
    Next

'FALL

[Class1!E6].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296500.xlsm]dashboard'!E17"
[Class1!E6] = [Class1!E6].Value        

[Class1!E7].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296500.xlsm]dashboard'!E18"
[Class1!E7] = [Class1!E7].Value         

[Class1!E8].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296500.xlsm]dashboard'!E19"
[Class1!E8] = [Class1!E8].Value         


End Sub

Unless I am mistaken only the end part of your code seems to relate to your question. 除非我没有记错,否则您代码的最后部分似乎只与您的问题有关。 In which case you simply need to change the workbook name in each reference as follows. 在这种情况下,您只需更改每个引用中的工作簿名称,如下所示。

[Class1!E6].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296500.xlsm]dashboard'!E17"
[Class1!E6] = [Class1!E6].Value        

[Class1!E7].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296501.xlsm]dashboard'!E17"
[Class1!E7] = [Class1!E7].Value         

[Class1!E8].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296502.xlsm]dashboard'!E17"
[Class1!E8] = [Class1!E8].Value         

Obviously I don't know the name of your other workbooks so I've just incrementes the last digit. 显然,我不知道其他工作簿的名称,因此我只增加了最后一位数字。 If you want to make this a bit slicker then you could create an array of workbook names or use a counter with a For loop to cut down on the repitition but we would need to know more about your workbook naming convention to assist any further. 如果您想让它更流畅一些,那么您可以创建一个工作簿名称数组,或者使用带有For循环的计数器来减少重新排列的位置,但是我们需要更多地了解您的工作簿命名约定,以提供进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM