我根据另一个工作表中设置的filter_range
在filter_val
中编写了一些自动过滤值的代码。我想要的结果是一个以filter_val
中每个cust_DMA
命名的标签,其中包含为此值过滤的值。
虽然循环遍历'filter_val'列表,但我对代码的这一部分不满意
' filter_val = .Cells(i, 1).Value
filter_range.AutoFilter Field:=8, Criteria1:=filter_val ''''autofilter field should be 8 as h is column 8
Billed_sheet.Range("a:v").copy
cust_DMA.Sheets.Add.Name = filter_val
ActiveSheet.Paste ''sometimes breaks here;
虽然它产生了我想要的结果,但我不喜欢使用Activesheet.Paste
,偶尔这行代码会失败。
有人可以为此推荐更好的解决方案吗?我尝试根据过滤的单元格设置范围,但是当我使用此范围向Cust_DMA工作表添加值时,将复制整个范围,而不仅仅是过滤后的值。
以下代码,
干杯
Sub filter_DMA_debugged_23_03_15(filter_val As String, filter_range As Range, Lrow As Long, LBox As Object, List_row As Long, DMA_sht As Worksheet, DMA_wb As Workbook, cust_DMA As Workbook, FPath As String, FName As String, list_val As String, i As Integer) 'working
'''works in stepthrough/runtime but the activesheet paste is a bit volatile - find a solution
Application.ScreenUpdating = False
Set DMA_wb = Workbooks("DMA_metered_tool_v11_SICTEST.xlsm")
Set DMA_sht = DMA_wb.Worksheets("DMA list")
FPath = DMA_sht.Range("c8").Text
FName = ("DMA_customers_SICTEST.xlsx")
Workbooks.Add.SaveAs FileName:=FPath & "\" & FName ''''
Set cust_DMA = Workbooks("DMA_customers_SICTEST.xlsx")
Set Billed_sheet = Workbooks("Billed_customers_SICTEST.xls").Sheets("Non Household Metered Users")
With Billed_sheet
.AutoFilterMode = False ' clear any existing filter to get accurate row count
Lrow = .Range("a" & .Rows.count).End(xlUp).row
Set filter_range = .Range("a1:v" & Lrow) '''try changing to a:v to avoid missing anything
End With
With DMA_sht
List_row = .Range("a" & .Rows.count).End(xlUp).row
For i = 2 To List_row '- 1 removed '-1 as it was missing the last value, starting at 2 already accounts for list_row having more items in it than needed.
filter_val = .Cells(i, 1).Value
filter_range.AutoFilter Field:=8, Criteria1:=filter_val ''''autofilter field should be 8 as h is column 8
Billed_sheet.Range("a:v").copy
cust_DMA.Sheets.Add.Name = filter_val
ActiveSheet.Paste ''sometimes breaks here
Next i
End With
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
I've done something like this before, please test the following and see if it works for your needs.
' filter_val = .Cells(i, 1).Value
filter_range.AutoFilter Field:=8, Criteria1:=filter_val
cust_DMA.Sheets.Add.Name = filter_val
'ActiveSheet.Paste ''sometimes breaks here;
With ActiveSheet.AutoFilter.Range.
.Copy Sheets(filter_val).Range("A1") 'may need to change target
.Clear
End With
答案 1 :(得分:0)
感谢我被导向here的信息,我在下面有一个工作版本,代码中有注释。如果有人想提出任何建议,我相信它可以变得更优雅。感谢您的投入。
<button class="btn-text-expand" ng-click="show = true" ng-show="showMoreButton">Show text</button>