简体   繁体   English

Excel VBA循环查找特定范围并连接2个单元格值并删除空单元格

[英]Excel VBA to loop and find specific range and concatenate 2 cell values and delete empty cell

I am trying to identify a specific range in column-A and concatenate two cells within the specific range and delete the empty cell. 我正在尝试在A列中标识特定范围,并将特定范围内的两个单元格连接起来,然后删除空单元格。 I have been successful in putting a code together and it does the job very well. 我已经成功地将代码组合在一起,并且可以很好地完成工作。 But, I don't know how to loop it to identify next range. 但是,我不知道如何循环使用它来确定下一个范围。 Any help would be appreciated. 任何帮助,将不胜感激。

As per below image and code, First, I am finding and selecting a range between two (MCS) in column-A with a condition that, if the rows are more than 8 between two MCS. 根据下面的图像和代码,首先,我要找到并选择A列中两个(MCS)之间的范围,条件是,如果两个MCS之间的行数大于8。 Then I am concatenating first 2 cells immediately after MCS and delete the empty row. 然后,我将在MCS之后立即连接前两个单元格,并删除空行。

The below code works well for first range but I am unable to loop to identify next range from row 22 to 32 and perform concatenations. 下面的代码对于第一个范围效果很好,但是我无法循环以标识第22行到第32行的下一个范围并执行串联。

I dont know how to loop in column-A and select ranges and concatenate. 我不知道如何在A列中循环并选择范围并进行连接。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks 谢谢

在此处输入图片说明

Sub MergeStem()
    Dim findMCS1 As Long
    Dim findMCS2 As Long
    Dim myCount As Integer
    Dim myStems As Long
    Dim mySelect As Range
    Dim c As Range

    findMCS1 = Range("A:A").Find("MCS", Range("A1")).Row
    findMCS2 = Range("A:A").Find("MCS", Range("A" & findMCS1)).Row

    myCount = Range("A" & findMCS1 + 1 & ":A" & findMCS2 - 1).Cells.Count
    Range("B1").Value = myCount
    MsgBox "Number of rows =" & myCount

    Set mySelect = Selection

    If myCount > 8 Then
        myStems = Range("A" & findMCS1 + 2 & ":A" & findMCS2 - 9).Select

        Set mySelect = Selection

        For Each c In mySelect.Cells
            If firstcell = "" Then firstcell = c.Address(bRow, bCol)
            sArgs = sArgs + c.Text + " "

            c.Value = ""
        Next
        Range(firstcell).Value = sArgs
    End If

    Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    Application.ScreenUpdating = True

End Sub

Can you try this? 你可以试试这个吗? Ordinarily, Find would be the way to go but because you are deleting rows it's hard to keep track of which cells you've found. 通常,使用“ Find是一种方法,但是由于要删除行,因此很难跟踪找到的单元格。

Sub x()

Dim r As Long, n1 As Long, n2 As Long

With Range("A1", Range("A" & Rows.Count).End(xlUp))
    For r = .Count To 1 Step -1
        If .Cells(r).Value = "MCS" Then
            If n1 = 0 Then
                n1 = .Cells(r).Row
            Else
                n2 = .Cells(r).Row
            End If
            If n1 > 0 And n2 > 0 Then
                If n1 - n2 > 9 Then
                    .Cells(r + 1).Value = .Cells(r + 1).Value & .Cells(r + 2).Value
                    '.Cells(r + 2).EntireRow.Delete
                    'Call procedure to delete row 
                End If
                n1 = n2
                n2 = 0
            End If
        End If
    Next r
End With

End Sub

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

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