简体   繁体   中英

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.

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. 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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