简体   繁体   English

VBA通过.SpecialCells(xlCellTypeVisible)合并自动过滤的单元格

[英]VBA merging autofiltered cells via .SpecialCells(xlCellTypeVisible).Range

I have a list of classes my users have taken that I want to add what access to vehicles they have and if the company has activated their badges to use vehicles(fork lifts etc). 我有一个我的用户已经采取的课程列表,我想添加他们拥有的车辆的访问权限,以及公司是否已激活他们的徽章以使用车辆(叉车等)。 So I wanted to merge all the rows of classes for a user then just have a centered answer. 所以我想为用户合并所有类的行,然后只有一个居中的答案。 When I walk through step by step it looks to be working but the end result is the entire column of data being merged. 当我逐步完成它看起来工作但最终结果是整个数据列被合并。 My question is do I have the syntax wrong or am I trying to do something that can not be done in Excel 2013. 我的问题是语法是否错误,还是我尝试执行Excel 2013中无法完成的操作。

Code I am using: 我正在使用的代码:

With Range("A1:K" & LastRow)
  For i = 1 To UBound(FleetID, 1)
    .AutoFilter Field:=5, Criteria1:=FleetID(i)

    LastFilteredRow = .SpecialCells(xlCellTypeVisible).Cells(Rows.Count, "A").End(xlUp).Row
    If LastFilteredRow > 1 Then
     With .SpecialCells(xlCellTypeVisible).Range("R2:R" & LastFilteredRow)
       .Select
       .Merge
       If FleetClass(i) = "Operator" Then .Value = "Standard" Else .Value = FleetClass(i)
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
     End With 'std
     With .SpecialCells(xlCellTypeVisible).Range("S2:S" & LastFilteredRow)
       .Select
       .Merge
       .Value = FleetAct(i)
       .HorizontalAlignment = xlCenter
       .VerticalAlignment = xlCenter
     End With ' ' active
    End If
  Next i
   .Columns("A:U").AutoFit
   .Columns("E").ColumnWidth = 11
   .Columns("H:I").ColumnWidth = 11
   .Columns("N:N").ColumnWidth = 2
 End With   'Range("A1:K" & LastRow)

My result come out as follows 我的结果如下

without merge 没有合并

with merge 与合并

I figured it out I had 2 errors in this code. 我想通了这个代码我有2个错误。 The first error was my range was incorrect, although it doesn't seem to have broken the merge, but does break the code I added later. 第一个错误是我的范围不正确,虽然它似乎没有打破合并,但确实打破了我后来添加的代码。 I had Range after specialcells so was merging cells from the beginning of the sheet to the end of my visible cells instead of merging the visible cells inside of my range the code needed is thus 我在特殊细胞之后使用了Range,所以将细胞从片材的开头合并到我的可见细胞的末端而不是合并我的范围内的可见细胞所需的代码因此

With Range("A1:T" & LastRow)
    For i = 1 To UBound(FleetID, 1)
      .AutoFilter Field:=5, Criteria1:=FleetID(i)
      LastFilteredRow = .SpecialCells(xlCellTypeVisible).Cells(Rows.Count, "A").End(xlUp).Row
      If LastFilteredRow > 1 Then
       With .Range("R2:R" & LastFilteredRow).SpecialCells(xlCellTypeVisible)
         .Merge
         If FleetClass(i) = "Operator" Then .Value = "Standard" Else .Value = FleetClass(i)
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
       End With 'std
       With .Range("S2:S" & LastFilteredRow).SpecialCells(xlCellTypeVisible)
         .Merge
         .Value = FleetAct(i)
         .HorizontalAlignment = xlCenter
         .VerticalAlignment = xlCenter
       End With ' ' active
      End If
    Next i
   .Columns("A:U").AutoFit
   .Columns("E").ColumnWidth = 11
   .Columns("H:I").ColumnWidth = 11
   .Columns("N:N").ColumnWidth = 2
 End With   'Range("A1:T" & LastRow)

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

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