我正在尝试编写一个VBA脚本,其中包含将数据的重叠部分移出到相邻​​单元格并调整单元格大小以适合数据长度的功能,如以下快照所示。

1)Excel中数据的原始布局

excel - Excel VBA删除重叠到相邻字段中的数据-LMLPHP

2)点击顶部的左手角以选择所有字段

excel - Excel VBA删除重叠到相邻字段中的数据-LMLPHP

3)双击列的单元格侧面以调整单元格大小以适合数据长度。

excel - Excel VBA删除重叠到相邻字段中的数据-LMLPHP

当前,我在下面使用.HorizontalAlignment,但是它不起作用:

With wks
    With .Cells(1, 1).CurrentRegion
        lastCol = .Columns.Count
        lastRow = .Rows.Count
    End With

    'Select the interested cells and insert borders around the interested fields
    .UsedRange.Borders.LineStyle = xlContinuous
    .UsedRange.HorizontalAlignment = xlLeft
End With

最佳答案

我相信您正在寻找Range.AutoFit方法。

Sub allFit()
    Dim c As Long

    With Worksheets("Sheet1")
        With .Cells(1, 1).CurrentRegion
            For c = 1 To .Columns.Count
                With .Columns(c)
                    .WrapText = False
                    .AutoFit
                End With
            Next c
        End With
    End With

End Sub

关于excel - Excel VBA删除重叠到相邻字段中的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36443022/

10-10 18:50
查看更多