我希望你能提供帮助。我有下面的代码。基本上它的作用是,它打开一个对话框,允许用户选择一个Excel工作表,然后转到国家/地区列(11)过滤它,然后将该国家/地区复制并粘贴到新工作簿中,命名新工作簿在那个国家然后重复下一个国家的行动之后,它会保存并关闭每个工作簿。
代码完全按原样运行,但我现在要做的是,如果有一个单元格,或者在标题下的A,B或C列中有两个单元格或三个单元格空白。我希望它只为每个国家复制和粘贴这些行。
所以在我的图片下面我想要代码做的是啊我看到Cell A5是空白复制这一行并将它放入比利时工作簿,继续往前走啊我看到Cell A14是空白副本这个排在保加利亚的工作簿上,啊Cell C17是空白副本这一排并放入保加利亚的工作簿。 Ah Cell A26,B26和C26是这一行的空白副本,并把它放在克罗地亚的工作簿中。
一如既往,我们非常感谢任何帮助。
这是我的代码
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
Dim my_Workbook As Workbook
MsgBox "Pick your CRO 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
Set my_Workbook = Workbooks.Open(Filename:=my_FileName)
Call Filter(my_Workbook) '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Filter(my_Workbook As Workbook)
Dim rCountry As Range, helpCol As Range
Dim wb As Workbook
With my_Workbook.Sheets(1) '<--| 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:Y" from row 1 to last non empty row of column "A"
.Columns(11).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 11th 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 (11th 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...
Set wb = Application.Workbooks.Add '<--... add new Workbook
wb.SaveAs Filename:=rCountry.Value2 '<--... saves the workbook after the country
.SpecialCells(xlCellTypeVisible).Copy wb.Sheets(1).Range("A1")
ActiveSheet.Name = rCountry.Value2 '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
Sheets(1).Range("A1:Y1").WrapText = False 'Takes the wrap text off
ActiveWindow.Zoom = 55
Sheets(1).UsedRange.Columns.AutoFit 'Autofits the column
wb.Close SaveChanges:=True '<--... saves and closes workbook
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
答案 0 :(得分:-1)
使用xlCellTypeBlanks复制整行很容易。 清除所有过滤器和 如果有,请更换复制代码,否则,将其置于最后
dim country as string
row = activesheet.usedrange.rows.count
activesheet.range("A1:C" & row).SpecialCells(xlCellTypeBlanks).entirerow.select
for each r in selection.rows
country=activesheet.cells(r.row,11)
r.copy
application.workbook(country).sheet(1).rows(1).insert 'assume that your country workbook is already opened
next