VBA复制到新工作簿并保存
我希望你能帮忙。我有下面的代码。基本上,它所做的是打开一个对话框,允许用户选择excel工作表,然后转到国家栏(11)过滤它,然后将该国家复制并粘贴到新工作表中,在该国家之后命名新工作表,然后为下一个国家重复操作 我希望它现在做的是,而不是创建一个新的工作表并命名它。我希望它为每个国家创建一个新工作簿,复制和粘贴信息,然后将新工作簿保存并命名到文件夹中 我的代码如下 我们一如既往地非常感谢您的帮助VBA复制到新工作簿并保存,vba,excel,copy-paste,Vba,Excel,Copy Paste,我希望你能帮忙。我有下面的代码。基本上,它所做的是打开一个对话框,允许用户选择excel工作表,然后转到国家栏(11)过滤它,然后将该国家复制并粘贴到新工作表中,在该国家之后命名新工作表,然后为下一个国家重复操作 我希望它现在做的是,而不是创建一个新的工作表并命名它。我希望它为每个国家创建一个新工作簿,复制和粘贴信息,然后将新工作簿保存并命名到文件夹中 我的代码如下 我们一如既往地非常感谢您的帮助 Sub Open_Workbook_Dialog() Dim my_FileName As Va
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection
If my_FileName <> False Then
Workbooks.Open Filename:=my_FileName
Call Filter '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Filter()
Dim rCountry As Range, helpCol As Range
With Worksheets("CountryList") '<--| refer to data worksheet
With .UsedRange
Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in
End With
With .Range("A1:Y" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A"
.Columns(11).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column
Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row)
For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row)
.AutoFilter 11, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered...
Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet
ActiveSheet.Name = rCountry.Value2 '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
End If
Next
End With
.AutoFilterMode = False '<--| remove autofilter and show all rows back
End With
helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included)
End Sub
子打开工作簿对话框()
将my_文件名设置为变体
MsgBox“Pick your TOV file”这还不完整,但我认为您需要类似的东西,请添加一个wokbook变量:
'Add this above the loop
Dim wb As Workbook
'Replace where you create a new sheet with something similar to this
Set wb = Application.Workbooks.Add
wb.SaveAs Filename:=rCountry.Value2
.SpecialCells(xlCellTypeVisible).Copy wb.Sheets(1).Range("A1")
希望这对您有所帮助这还不完整,但我认为您需要一些类似的东西,请添加一个wokbook变量:
'Add this above the loop
Dim wb As Workbook
'Replace where you create a new sheet with something similar to this
Set wb = Application.Workbooks.Add
wb.SaveAs Filename:=rCountry.Value2
.SpecialCells(xlCellTypeVisible).Copy wb.Sheets(1).Range("A1")
希望对您有所帮助谢谢您的帮助。不幸的是,我得到了一个编译错误“无法分配到只读属性”,有没有办法纠正这个错误?此错误发生在此处wb.Name=“Country Name”
抱歉,您需要保存工作簿以更改其名称。因此,将该行替换为:wb.SaveAs文件名:=“国家名称”
非常感谢您的帮助。非常感谢。它的工作,但不幸的是“国家名称”不能用来保存每个工作簿。我需要代码将每个工作簿保存在“国家”列中的名称之后,以便比利时将另存为工作簿比利时,保加利亚将另存为工作簿保加利亚,依此类推,直到“国家”列为空。很抱歉,我认为您可能会更改此设置,基于您上面的内容,我认为这将起作用:wb.SaveAs Filename:=rccountry.Value2
Boo Yaaah!!那太完美了。非常感谢你。我的朋友,你真的很有技巧。都柏林非常尊敬。:-)谢谢你的帮助。不幸的是,我得到了一个编译错误“无法分配到只读属性”,有没有办法纠正这个错误?此错误发生在此处wb.Name=“Country Name”
抱歉,您需要保存工作簿以更改其名称。因此,将该行替换为:wb.SaveAs文件名:=“国家名称”
非常感谢您的帮助。非常感谢。它的工作,但不幸的是“国家名称”不能用来保存每个工作簿。我需要代码将每个工作簿保存在“国家”列中的名称之后,以便比利时将另存为工作簿比利时,保加利亚将另存为工作簿保加利亚,依此类推,直到“国家”列为空。很抱歉,我认为您可能会更改此设置,基于您上面的内容,我认为这将起作用:wb.SaveAs Filename:=rccountry.Value2
Boo Yaaah!!那太完美了。非常感谢你。我的朋友,你真的很有技巧。都柏林非常尊敬。:-)