Select cells relevant to a page, call macro, go to cells of next page and so on.

Singh_Edm

New Member
Joined
Dec 18, 2013
Messages
30
Hello Friends. I'm looking for a way/code to automatically select the cells in Column 2 that have the same page number associated with them in Column 1. Then I want to call a macro and then repeat the process. I have provided the link for the 15kb macro enabled excel file with which I need help. The macro is in the file. Many thanks for your time. https://www.filesanywhere.com/fs/v.aspx?v=8b6c648a5d6373af9da6
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hello Friends. I'm looking for a way/code to automatically select the cells in Column 2 that have the same page number associated with them in Column 1. Then I want to call a macro and then repeat the process. I have provided the link for the 15kb macro enabled excel file with which I need help. The macro is in the file. Many thanks for your time. https://www.filesanywhere.com/fs/v.aspx?v=8b6c648a5d6373af9da6
I do not understand your question... in your sample file, Columns B and C are identical... what exactly is your code supposed to be doing?
 
Upvote 0
Column 3 has same entries as in Column 2 because all entries in Column 2 having the same Page ID are merged together in Column 3. That's what I want the code to do. Column 3 is the desired result but in reality I'm fine with the cells being merged in Column 2 itself.
I want the code to start selecting cells downward from B2. Once code sees that the Page ID on the left is different, it should stop selecting. In this case, cell B4. Now it should merge cells B2 to B4.
Then it should start from B5 and go till cell B7 because B8 has a different Page ID. Merge B5 to B7.
And so on,
Is that clearer Rick?
 
Upvote 0
Hi Singh,

I am sure Rick will give you more simple code:), but for not check this one:

Code:
Sub JoinAndMerge()
Dim CValue As String
Dim irow, srow As Long


irow = 2
srow = Empty
CValue = Empty


Do Until Cells(irow - 1, 1) = Empty
    
    If Cells(irow, 1) = Cells(irow - 1, 1) Then
        CValue = CValue & Cells(irow, 2) & " "
    ElseIf Not srow = Empty Then
        Range(Cells(srow, 3), Cells(irow - 1, 3)).Merge
             With Cells(srow, 3)
                .Merge
                .HorizontalAlignment = xlGeneral
                .VerticalAlignment = xlCenter
                .WrapText = True
            End With
        Cells(srow, 3) = CValue
        
        srow = irow
        CValue = Cells(irow, 2) & " "
    Else
        srow = irow
        CValue = Cells(irow, 2) & " "
    End If
    
irow = irow + 1
Loop


End Sub
 
Last edited:
Upvote 0
sorry little correction.

Code:
Sub JoinAndMerge()
Dim CValue As String
Dim irow, srow As Long

irow = 2
srow = Empty
CValue = Empty

Columns(3).UnMerge

Do Until Cells(irow - 1, 1) = Empty
    
    If Cells(irow, 1) = Cells(irow - 1, 1) Then
        CValue = CValue & Cells(irow, 2) & " "
    ElseIf Not srow = Empty Then
        Cells(srow, 3).Clear
             With Range(Cells(srow, 3), Cells(irow - 1, 3))
                .Merge
                .Value = CValue
                .HorizontalAlignment = xlGeneral
                .VerticalAlignment = xlCenter
                .WrapText = True
            End With
        
        srow = irow
        CValue = Cells(irow, 2) & " "
    Else
        srow = irow
        CValue = Cells(irow, 2) & " "
    End If
    
irow = irow + 1
Loop

End Sub
 
Last edited:
Upvote 0
Hi ScorpionKZ
Thanks for replying and taking the effort to pen the code.
A question: Which cell should be selected before running the code? I selected cell B2 and in another attempt selected cell C2 before running the code and it gives an error at the following line "Do Until Cells(irow - 1, 1) = Empty"
Any cues on whats happening?
 
Upvote 0
did you copy whole code? it works fine for me.

Please remember that it checks if column A contains the data and code stops work when it will find the empty cell in column A.
if you have different columns than on the sample you sent you need to adjust the column numbers in the code
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top