Pls correct my below comparison code

gopdeep

Board Regular
Joined
Apr 24, 2012
Messages
94
i tried to write VBA code for below condtion which doesn't make any sense :(

code is for comparison - that is if C[1] is having the value same as C[3] then A[1] & A[3] values should be combined together with comma. for instance: consider that C[1] & C[3] having "split" then corresponding A[1] am & A[3] - "lright" should be combined and displayed in msg box as "am, alirgh". likewise code should do the same till the end of range.

below is my code which didn't work as expected:confused:

Code:
Sub cal()
Dim i As Integer
Dim msg As String, str As String
'msg = ""
For i = 7 To 11
If ActiveSheet.Range("c" & i).Value = ActiveSheet.Range("c" & i + 1).Value Then
msg = ActiveSheet.Range("b" & i).Value
str = msg + msg
'msg = ""
End If
Next i
'MsgBox str, vbOKOnly
End Sub

:rolleyes:
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It is difficult to understand what the actual parameters are for the code, but here is the basic form for it. If you want the comparison to begin in row 1 then change the line:

For i = 7 To 11

To:

For i = 1 To (Whatever the last row number you want to compare)

Also, the code compares adjacent rows, i.e. 1 to 2, 2 to 3, etc. The narrative indicates a comparison of 1 to 3, 2 to 4, etc. So if you want to change that then the line to adjust is:

ActiveSheet.Range("c" & i + 1).Value

And

Range("A" & i) & ", " & Range("A" & i + 1)

By changing the + value which will in turn change the row number on each iteration of the for loop.

Here is the modified code:



Code:
Sub cal()
Dim i As Integer
Dim msg As String, str As String
'msg = ""
For i = 7 To 11
If ActiveSheet.Range("c" & i).Value = ActiveSheet.Range("c" & i + 1).Value Then
MsgBox Range("A" & i) & ", " & Range("A" & i + 1)
End If
End If
Next i
'MsgBox str, vbOKOnly
End Sub
Code
 
Upvote 0
It is difficult to understand what the actual parameters are for the code, but here is the basic form for it. If you want the comparison to begin in row 1 then change the line:

For i = 7 To 11

To:

For i = 1 To (Whatever the last row number you want to compare)

Also, the code compares adjacent rows, i.e. 1 to 2, 2 to 3, etc. The narrative indicates a comparison of 1 to 3, 2 to 4, etc. So if you want to change that then the line to adjust is:

ActiveSheet.Range("c" & i + 1).Value

And

Range("A" & i) & ", " & Range("A" & i + 1)

By changing the + value which will in turn change the row number on each iteration of the for loop.

Here is the modified code:



Code:
Sub cal()
Dim i As Integer
Dim msg As String, str As String
For i = 7 To 11
If ActiveSheet.Range("c" & i).Value = ActiveSheet.Range("c" & i + 1).Value Then
MsgBox Range("A" & i) & ", " & Range("A" & i + 1)
End If
Next i
End Sub
Code


Sorry, this should be the cleaned up version:

Code:
Sub cal()
Dim i As Integer
For i = 7 To 11
If ActiveSheet.Range("c" & i).Value = ActiveSheet.Range("c" & i + 1).Value Then
MsgBox Range("A" & i) & ", " & Range("A" & i + 1)
End If
Next i
End Sub
Code
 
Upvote 0
it doesn't displaye any msg box :(

The code says that if a cell in column C is equal to the cell immediately beneath it in the same column then display a message box that combines two cells in Column A on the same rows that were compared in column C.

So if the values of the compared cells in column C are not equal, then no message box will be displayed.

If the two cells in column C are equal then the message box will be displayed.
 
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,416
Members
449,099
Latest member
COOT

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