vba 将一个工作簿中的命名工作表保存到同一文件夹中的新工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20077150/
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
Save a named sheet from one workbook to a new workbook in the same folder
提问by Amatya
I would like to be able to
我希望能够
- take a sheet called "data" in a given workbook called "original_data.xlsm",
- copy it conditionally (using Autofilter or something else), say only those rows where column C was "dog"
- Create a new workbook, in the same folder as the original book, called dog.xlsm and save the copied stuff into a new sheet called "dog data".
- Then repeat with a different filter.So for example copy and autofilter if column C was "cat" and create a workbook "cat.xlsm", in the same folder as the original book, with a sheet called "cat_data" containing some filtered data.
- 在名为“original_data.xlsm”的给定工作簿中取出一张名为“data”的表,
- 有条件地复制它(使用自动过滤器或其他东西),只说 C 列是“狗”的那些行
- 在与原始书籍相同的文件夹中创建一个名为 dog.xlsm 的新工作簿,并将复制的内容保存到名为“dog data”的新工作表中。
- 然后使用不同的过滤器重复。例如,如果 C 列是“cat”,则复制和自动过滤器并在与原始书籍相同的文件夹中创建一个工作簿“cat.xlsm”,其中包含一个名为“cat_data”的工作表,其中包含一些过滤后的数据.
I've been making incorrect attempts for three days now and would appreciate some help. Here is what I have done so far.
我三天来一直在进行错误的尝试,希望得到一些帮助。这是我到目前为止所做的。
Workbooks.Add
Set wb = ActiveWorkbook
GetBook = ActiveWorkbook.Name
wb.Sheets("data").SaveAs Workbooks(GetBook).Path & "\dog"
Workbooks("dog.xlsx").Worksheets("Sheet1").UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
But it's not working. :(
但它不起作用。:(
回答by DaveU
Looks like you're trying to set wb to "original_data.xlsm", but your first line is making the new workbook the active workbook.
看起来您正在尝试将 wb 设置为“original_data.xlsm”,但您的第一行是将新工作簿设为活动工作簿。
Workbooks.Add
Set wb = ActiveWorkbook
See if this helps.
看看这是否有帮助。
Sub sheetCopy()
Dim wbS As Workbook, wbT As Workbook
Dim wsS As Worksheet, wsT As Worksheet
Set wbS = ThisWorkbook 'workbook that holds this code
Set wsS = wbS.Worksheets("Data")
wsS.Copy
Set wbT = ActiveWorkbook 'assign reference asap
Set wsT = wbT.Worksheets("Data")
wsT.Name = "Dog Data" 'rename sheet
wbT.SaveAs wbS.Path & "\dog.xlsx" 'save new workbook
wsT.UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
End Sub