简体   繁体   English

Excel Visual Basic宏合并选定区域中的单元格

[英]Excel Visual Basic Macro to merge cells in a selected area

I have an excel spreadsheet where I want to merge each cell with a value in it with every empty cell below it until the next cell in that column with a value. 我有一个Excel电子表格,我要合并其中的每个单元格及其下的每个空白单元格,直到该列中的下一个单元格具有值。

Currently I have this: 目前我有这个:

Sub mergemainbody()    
    lrow = ActiveSheet.UsedRange.Rows.Count - 2        
    On Error Resume Next  
    Application.DisplayAlerts = False  
    For col = 1 To 50  
       For Each ar In Cells(3, col).Resize(lrow).SpecialCells  (xlCellTypeBlanks).Areas  
          ar.Resize(ar.Rows.Count + 1).Offset(-1).Merge  
       Next  
    Next  
 End Sub

Which works on an entire sheet, but I want the macro to only apply to a selected area. 它适用于整个工作表,但我希望宏仅适用于选定区域。 However, simply changing For col = 1 to 50 to For Each cell In Selection makes the macro seemingly do nothing. 但是,只需将For col = 1 to 50更改For col = 1 to 50 For Each cell In Selection ,宏似乎就什么也不做。

Example of data: 数据示例:

Heading | Heading   | Heading   | Heading   |      
1456262 | 270520    | 574038    | 583059    |    
Words   | --------- | --------- | --------- |  
586048  | --------- | --------- | --------- |        
Words   | 694574    | 856738    | 068438    |    

Where --- shows the cell is empty. 其中---表示单元格为空。

Here is a rough way to merge down on your selection as you requested. 这是按照您的要求合并选择的一种粗略方法。 Note that this won't work the way you intend if there isn't a value in the first cell 请注意,如果第一个单元格中没有值,这将无法按预期方式工作

Sub MergeDown()
    Dim rng As Range, r As Range
    Dim i As Integer

    Set rng = Selection
    For Each r In rng
        If r.Value <> "" Then
            i = 1
            While r.Offset(i, 0).Value = "" And Not Intersect(r.Offset(i, 0), rng) Is Nothing
                i = i + 1
            Wend
            r.Resize(i, 1).Merge
        End If
    Next r
End Sub

I will assume that you do not want to ever merge the second row with the header row. 我将假设您不想将第二行与标题行合并。

After isolating row 3 to the last used row in the data block radiating out from A1 with the Range.CurrentRegion property and the Range.Resize / Range.Offset properties, use the Range.SpecialCells method with xlCellTypeBlanks . 在使用Range.CurrentRegion属性Range.Resize / Range.Offset属性将第3行隔离到从A1辐射出的数据块中最后使用的行之后,使用带有xlCellTypeBlanksRange.SpecialCells方法 As you cycle through the Range.Areas property , resize and offset before merging. 当您遍历Range.Areas属性时 ,请在合并前调整大小和偏移量。

Dim c As Long, a As Long
With ActiveSheet
    'work on the block of data radiating out from A1
    With .Cells(1, 1).CurrentRegion
        'move off the header row and first row of data
        With .Resize(.Rows.Count - 2, .Columns.Count).Offset(2, 0)
            'work through the columns
            For c = 1 To .Columns.Count
                'locate the blank cells in groups (aka Areas)
                With .Columns(c).Cells.SpecialCells(xlCellTypeBlanks)
                    'cycle through the areas (blank cell groups)
                    For a = 1 To .Areas.Count
                        'work with each Area in turn
                        With .Areas(a).Cells
                            'resize one row larger and offset one row up
                            .Resize(.Rows.Count + 1, 1).Offset(-1, 0).Merge
                            'optionally center the value in the newly merged cells
                            .VerticalAlignment = xlCenter
                        End With
                    Next a
                End With
            Next c
        End With
    End With
End With

I believe your problem is that the Variables were never declared, so VBA is making a guess at what they are. 我相信您的问题是从未声明过变量,因此VBA猜测它们是什么。 Use this code and see if you get any errors: 使用此代码,看看是否出现任何错误:

Option Explicit
Sub mergemainbody()
Dim selRange As Range
Dim lRow    As Long
Dim ar As Range, col As Range

Set selRange = Selection
lRow = selRange.Rows.Count - 2    ' Why -2?
'On Error Resume Next
Application.DisplayAlerts = False

For Each col In selRange.Columns
    For Each ar In Cells(3, col.Column).Resize(lRow).SpecialCells(xlCellTypeBlanks).Areas
        ar.Resize(ar.Rows.Count + 1).Offset(-1).Merge
    Next
Next col
End Sub

The only error that it may throw, is an error after there are no more SpecialCells(xlCellTypeBLanks) , which means it ran successfully over all cells. 它可能引发的唯一错误是没有更多SpecialCells(xlCellTypeBLanks)之后的错误,这意味着它已成功在所有单元上运行。

拿出“ On Error Resume Next”,这是隐藏任何错误的肯定方法。

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

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