How to loop a range in for-next statement using the VBA?

teezarromy

New Member
Joined
Jun 24, 2009
Messages
4
Hello,

Basically, I have 5 different columns in my table (Firstname, lastname, data1, data 2 and data3. If the firstname and lastname are duplicated, I want to merge the corresponging cells. However, I have problem with the syntax of the range in the for-next statement. Can Anyone help? Below is my code.


Sub CondenseTable()
Dim i As Integer
i = 2
For i = 2 To 8
If Worksheets("Sheet1").Range("A" & i).Value = Worksheets("Sheet1").Range("A" & i + 1) And _
Worksheets("Sheet1").Range("B" & i).Value = Worksheets("Sheet1").Range("B" & i + 1) Then

Range("A" & i:"A" & i+1).Select
Selection.Merge

Range("B" & i:"B" & i+1).Select
Selection.Merge

Range("C" & i:"C" & i+1).Select
Selection.Merge

Range("D" & i:"D" & i+1).Select
Selection.Merge

Range("E" & i:"E" & i+1).Select
Selection.Merge

End If

Next i
End Sub

Thanks,
Teezar
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Andrew,

Thanks for your help and warm welcome. The syntax works. By the way, I keep getting a message box saying that only the left data will be saved if I merged the columns. How can I write a macro to automate the clicking the "ok" button?

Thanks,
Teezar
 
Upvote 0
Allow me to make my problem clear. When merging two cells with different data, I get the message box saying "The selection contains multiple data value". Merging into one cell will only kep the most upper-left data only" To allow the merge, I have to click ok.

I hope someone can help me with the macro to automate the clicking "OK" button.

Thnks
Tzar
 
Upvote 0
Try putting:

Application.DisplayAlerts = False

at the beginning of your code. Set it back to True at the end.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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