vba 将工作表 1 上的行复制到所有现有工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18574676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:06:17  来源:igfitidea点击:

Copy row on Sheet 1 to all existing sheets

excelvbaexcel-vba

提问by user2740022

I got a excel file with 30 sheets in it all containing the same first row. I'm searching for a code/way that copies the first row on sheet 1 and pastes it on all the existing sheets.

我得到了一个 excel 文件,里面有 30 张纸,都包含相同的第一行。我正在寻找一种代码/方式来复制工作表 1 上的第一行并将其粘贴到所有现有工作表上。

The rows change regularly and through this way I only have to adjust it once.

行定期更改,通过这种方式我只需要调整一次。

回答by Jon Crowell

You can use .FillAcrossSheetsto do this:

您可以使用以下.FillAcrossSheets方法执行此操作:

Sub CopyToAllSheets()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Sheets.FillAcrossSheets ws.Range("1:1")
End Sub

Note that you can specify what you want to copy with an optional type parameter:

请注意,您可以使用可选类型参数指定要复制的内容:

  • Copy both contents and formatting = xlFillWithAll
  • Copy contents only = xlFillWithContents
  • Copy formats only = xlFillWithFormats
  • 复制内容和格式 = xlFillWithAll
  • 仅复制内容 = xlFillWithContents
  • 仅复制格式 = xlFillWithFormats

The default is xlFillWithAll. If you want to use one of the other options, do this:

默认值为xlFillWithAll. 如果要使用其他选项之一,请执行以下操作:

' copy contents
Sheets.FillAcrossSheets ws.Range("1:1") xlFillWithContents

or

或者

' copy formats
Sheets.FillAcrossSheets ws.Range("1:1") xlFillWithFormats

In case you don't want to copy to every sheet in your workbook, you can specify a list of target sheets like this:

如果您不想复制到工作簿中的每个工作表,您可以指定一个目标工作表列表,如下所示:

Sub CopyToSpecificSheets()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim targetSheets As Variant

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
    ' the sheet we are copying from must be included in the array
    targetSheets = Array(ws.Name, "Sheet2", "Sheet4")
    wb.Sheets(targetSheets).FillAcrossSheets ws.Range("1:1")
End Sub

回答by exussum

could do a simple way assuming you have the standard 3 sheet set up (Sheet1, Sheet2, Sheet3)

假设您设置了标准的 3 张纸(Sheet1、Sheet2、Sheet3),可以做一个简单的方法

Do all of the changes in Sheet 1, Then click Sheet 2, Shift + Click Sheet 3.

执行 Sheet 1 中的所有更改,然后单击 Sheet 2,Shift + 单击 Sheet 3。

in Cell A1, type =sheet1!a1

在单元格 A1 中,键入 =sheet1!a1

copy it across how ever far you need.

将它复制到您需要的距离。

Select Sheet 1 (to remove the grouping)

选择工作表 1(删除分组)

Now sheet 2 and 3 will have the same top row. This works for as many sheets as excel supports. So clicking sheet2, then shift click sheet 50 will fill out all sheets in the range Sheet2 - sheet 50

现在工作表 2 和 3 将具有相同的顶行。这适用于 excel 支持的尽可能多的工作表。所以点击sheet2,然后shift点击sheet 50将填写范围Sheet2 - sheet 50中的所有表格

回答by Gary's Student

Consider something like:

考虑这样的事情:

Sub qwerty()
    Dim r As Range
    Set r = Sheets(1).Range("1:1")
    For n = 2 To Sheets.Count
        r.Copy Sheets(n).Range("A1")
    Next n
End Sub

回答by Dancharim

You can try the following : Click and hold on "Shift" on your keyboard, then with your mouse click the last sheet. You'll notice that all sheets will be grouped ( Notice the keyword "Group" at the top top of your excel file). Now everything you do on 1 sheet will be applied to all while in group mode.

您可以尝试以下操作:单击并按住键盘上的“Shift”,然后用鼠标单击最后一张纸。您会注意到所有工作表都将被分组(注意 Excel 文件顶部的关键字“组”)。现在,您在一张纸上所做的一切都将在组模式下应用于所有人。