简体   繁体   English

如何使用VBA选择一对图纸并将它们复制到新的wb?

[英]How to VBA to select a pair of sheets and copy them to a new wb?

'First-timer here, so thanks for your patience! ‘这里是新手,非常感谢您的耐心等待!
I have a workbook with multiple sheets, and I need to: 我有一个包含多个工作表的工作簿,我需要:

1)Select pairs of worksheets (index and index +1) 1)选择工作表对(索引和索引+1)

2)Copy them into a new workbook 2)将它们复制到新的工作簿中

3)Rename the sheets with whatever label is in a referenced cell (such as A1) 3)使用引用的单元格(例如A1)中的任何标签重命名工作表

4)Then repeat for every pair of sheets in the workbook (dozens of pairs) 4)然后对工作簿中的每对工作表重复(数十对)

Since I am very new to vba (only took one class), I am easily confused. 由于我是vba的新手(只参加了一堂课),所以我很容易感到困惑。 The only part I have so far is referencing cell A1 to rename the sheet to whatever text string is in that cell. 到目前为止,我仅有的一部分是引用单元格A1将工作表重命名为该单元格中的任何文本字符串。 I can't figure out the rest of it. 我无法弄清其余的一切。 I'm thinking it has something to do with worksheet.index or worksheets(index) and some kind of loop where index adds 2 and activates that sheet and the next one before copying: sheets 1 and 2, then sheets 3 and 4, etc. 我认为这与worksheet.index或worksheets(index)和某种循环有关,其中索引加2并在复制之前激活该工作表和下一个工作:工作表1和2,然后工作表3和4,等等。 。

Private Sub Worksheet_Calculate() 
 Dim s As String 
 s = "sheet 1" 
 If Range("A1").Value <> "" Then 
     s = Range("A1").Value 
 End If 
 ActiveSheet.Name = s 
 End Sub 

The way I understand it, cell A1 has to be checked to be sure it's not null because you can't name a sheet with a null value. 以我的理解,必须检查单元格A1以确保它不为空,因为您不能使用空值命名工作表。 The usage and syntax on the rest of it is quite beyond me at this point, but I really want to learn and eventually become a contributing member of this community. 在这一点上,其余部分的用法和语法目前还远远超出了我,但是我真的很想学习,并最终成为该社区的贡献者。 I'm brand new at my job and only working part-time, so bringing a solution to my boss might mean me becoming permanent. 我是刚上班的新人,只能打零工,因此为老板提供解决方案可能意味着我成为永久人。 Therefore, a BIG, BIG THANKS!!! 因此,非常感谢!!!

Option Explicit

Private Sub Worksheet_Calculate()
Dim s As String
Dim ws As Worksheet
Dim wsi As Worksheet

Set wsi = Sheets("Sheet4")' You need to create a name for the sheet to paste into

For Each ws In ActiveWorkbook.Worksheets
If Not IsEmpty(ws.Range("A1")) and ws.name = "INDEXCOPY" Then
     ws.Name = ws.Range("A1").Value
     ws.Range("A1:" & ws.Range("A1").SpecialCells(xlCellTypeLastCell).Address).Copy _
     Destination:=wsi.Range("A1")
End If
Next ws
End Sub

This code will get you started, however, you need a way to distinguish between an index sheet you want to copy from, versus an index sheet you want to copy to. 这段代码可以帮助您入门,但是,您需要一种方法来区分要从中复制的索引表和要复制到的索引表。 You would need to add an additional if then statement, see comment above. 您将需要添加其他if语句,请参见上面的注释。 I would look here - http://www.rondebruin.nl/win/s3/win006.htm 我会在这里-http: //www.rondebruin.nl/win/s3/win006.htm

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

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