VBA: Code to skip or ignore rule If Cell data is not Identical

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Good day!

I just wanted to ask for your insight as I found a code to merge accounts, however if the cell data is not the same, it will skip the code.

Example:

NoNameData 1Data 2
001Math10095
002Math10095
003Science9590


'VBA Code for Merging the Data 1 and Data 2

Dim i As Long

'Row Selection

Rows("2:3").Select

' Go or Ignore
For i = 3 To Selection.Columns.Count
Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
Next i
Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp

End Sub

= = = =

In the initial code, I think I need to check first if cell B2 & B3 are both "Math". If yes, the code will push through but if the cell text is different then ignore .

Appreciate the help. :)
 

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.
maybe
VBA Code:
Dim i As Long, n As Long

Rows("2:3").Select 'Row Selection
For n = 1 To 3
  If Selection.Cells(n, 2) = Selection.Cells(n + 1, 2) Then
    For i = 3 To 6 'Selection.Columns.count
      Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
    Next i
  End If
Next n
    
Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp
NOTES:
Do you really want to loop over 16,384 columns?
I used 6 columns and 3 rows as a test. However, re: rows, I think you'd want to use a descending counter that is = to a variable that is the number of used rows to 2 (?) step -1 and adjust the variable value after each row is removed. I haven't used loop counters based on depleting rows for a long time - if ever, so I might be a bit fuzzy on that logic. Easy enough to Google though.
 
Upvote 0
maybe
VBA Code:
Dim i As Long, n As Long

Rows("2:3").Select 'Row Selection
For n = 1 To 3
  If Selection.Cells(n, 2) = Selection.Cells(n + 1, 2) Then
    For i = 3 To 6 'Selection.Columns.count
      Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
    Next i
  End If
Next n
   
Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp
NOTES:
Do you really want to loop over 16,384 columns?
I used 6 columns and 3 rows as a test. However, re: rows, I think you'd want to use a descending counter that is = to a variable that is the number of used rows to 2 (?) step -1 and adjust the variable value after each row is removed. I haven't used loop counters based on depleting rows for a long time - if ever, so I might be a bit fuzzy on that logic. Easy enough to Google though.
It's working! Thanks!
 
Upvote 0
maybe
VBA Code:
Dim i As Long, n As Long

Rows("2:3").Select 'Row Selection
For n = 1 To 3
  If Selection.Cells(n, 2) = Selection.Cells(n + 1, 2) Then
    For i = 3 To 6 'Selection.Columns.count
      Selection.Cells(1, i) = Selection.Cells(1, i) + Selection.Cells(2, i)
    Next i
  End If
Next n
   
Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp
NOTES:
Do you really want to loop over 16,384 columns?
I used 6 columns and 3 rows as a test. However, re: rows, I think you'd want to use a descending counter that is = to a variable that is the number of used rows to 2 (?) step -1 and adjust the variable value after each row is removed. I haven't used loop counters based on depleting rows for a long time - if ever, so I might be a bit fuzzy on that logic. Easy enough to Google though.
Apologies, I noticed that even if the text in Cell B2 & C2 is not the same, the macro delete a row. I was wondering to ignore this as well "Selection.Cells(2, 1).EntireRow.Delete Shift:=xlUp" if this is the case.
 
Upvote 0
Working on it with more data rows than I used before - I think I see another issue; not sure yet...
🏗️ 👷‍♂️🚧
 
Upvote 0
Just moving that line could fix that issue, but if the left side is what you have and the right side is what you want to end up with by removing rows, I don't see how using the Selection method like what you started with could ever work. As rows are removed, the selection doesn't change?
NameData 1Data 2NameData 1Data 2
Math
100​
95​
Math
175​
190​
Math75
95​
Science
95​
75​
Science95
75​
Math
75​
85​
Math75
85​
Science
180​
185​
Science85
90​
Science95
95​
Still didn't answer my question re looping over 16+k columns...
Better to set the counter to the number of used rows/columns in the used range?
 
Upvote 0
Just moving that line could fix that issue, but if the left side is what you have and the right side is what you want to end up with by removing rows, I don't see how using the Selection method like what you started with could ever work. As rows are removed, the selection doesn't change?
NameData 1Data 2NameData 1Data 2
Math
100​
95​
Math
175​
190​
Math75
95​
Science
95​
75​
Science95
75​
Math
75​
85​
Math75
85​
Science
180​
185​
Science85
90​
Science95
95​
Still didn't answer my question re looping over 16+k columns...
Better to set the counter to the number of used rows/columns in the used range?
Its's all good. I just tweak the code you provided. Thanks again! :)
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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