Автоматическое форматирование заканчивается преждевременно - PullRequest
1 голос
/ 17 мая 2019

Я настроил макрос для форматирования строк в электронной таблице, если есть записи. Я настроил его, используя файл с небольшим количеством записей, и когда я использую его для файла с большим количеством записей, он останавливается раньше.

Я посмотрел на код, и для меня нет смысла, почему он не будет работать должным образом.

Range(Range("A2"), Range("E2").End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThin
End With

Ответы [ 2 ]

0 голосов
/ 17 мая 2019

Согласно моим комментариям, самый простой способ исправить ваш код - это изменить его так:

With Sheets("Sheet1").Range("A2:E" & Cells(Rows.Count, "A").End(xlUp).Row)
    'Identify the worksheet, use xlUp, and remove Select/Selection
    .Borders(xlDiagonalDown).LineStyle = xlNone
    .Borders(xlDiagonalUp).LineStyle = xlNone
    .Borders(xlEdgeLeft).LineStyle = xlNone
    .Borders(xlEdgeTop).LineStyle = xlNone
    With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
End With
0 голосов
/ 17 мая 2019

Вот пример, не проверенный:

Sub BordersAndFilters()


    Dim ws As Worksheet
    Dim aBorderSettings() As Long           'Calls the array
    Dim i As Long

    Set ws = ThisWorkbook.Worksheets("Data") 'your worksheet name

    ReDim aBorderSettings(1 To 8, 1 To 2)   'An Array of length 8x2 (table)
        aBorderSettings(1, 1) = xlDiagonalDown:     aBorderSettings(1, 2) = xlNone
        aBorderSettings(2, 1) = xlDiagonalUp:       aBorderSettings(2, 2) = xlNone
        aBorderSettings(3, 1) = xlEdgeBottom:       aBorderSettings(3, 2) = xlContinuous
        aBorderSettings(4, 1) = xlEdgeLeft:         aBorderSettings(4, 2) = xlNone
        aBorderSettings(5, 1) = xlEdgeRight:        aBorderSettings(5, 2) = xlNone
        aBorderSettings(6, 1) = xlEdgeTop:          aBorderSettings(6, 2) = xlNone
        aBorderSettings(7, 1) = xlInsideHorizontal: aBorderSettings(7, 2) = xlNone
        aBorderSettings(8, 1) = xlInsideVertical:   aBorderSettings(8, 2) = xlNone

    With ws.Range("A2:E" & ws.Cells(ws.Rows.Count, "E").End(xlUp).Row)  'Instead of using LastRow. The "E" should be your column with guaranteed values in every row.
        'Filter and Fit
        .AutoFilter 'remove this if you don't want to filter
        .EntireColumn.AutoFit 'remove this if you don't want to fit the cells to column width

        'For every number in the array, change the borders based on the values in the array
        For i = LBound(aBorderSettings, 1) To UBound(aBorderSettings, 1)
            .Borders(aBorderSettings(i, 1)).LineStyle = aBorderSettings(i, 2)
            If aBorderSettings(i, 2) <> xlNone Then
                .Borders(aBorderSettings(i, 1)).ColorIndex = xlAutomatic
                .Borders(aBorderSettings(i, 1)).TintAndShade = 0
                .Borders(aBorderSettings(i, 1)).Weight = xlThin
            End If
        Next i

    End With

 End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...