Macro to fill excel columns on condition

Halley yenn

New Member
Joined
Mar 17, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a excel with multiple columns my main goal is to fill column A and column C as per Column B.
For ex:
COLA COLB COLC
1 18484 1
2 9848893 1
3 1234 1
3 1234 2
3 1234 3
.
.

In this if the the value in COLB is same then the respected cell values in COLA should be same as mentioned above and COLC value should increment .I will have only COLB data where the macro shold fill COLA and COLC ...COLA should increment to 1 2 3....if any repeated value then that must be repeated...COLC should be 1 for diff values in COLB and should increment for the Same numbers in COLB.
My explanation may not be clear so I am attaching the sample excel screenshot, where in sheet 1 you can see the actual data and in sheet2 the desired output with the macro.
Kindly help me on this as the data will be in thousands of rows which is time consuming.
KR,
Halley
example sheet.xlsx
ABCD
1orderkeyIDorder reference ID
210987
356843
46744521
589076213
689076213
789076213
889076213
9986
1073123
118761
1217283
1317283
1417283
Sheet1




example sheet.xlsx
ABCD
1orderkeyIDorder reference ID
21109871
32568431
4367445211
54890762131
64890762132
74890762133
84890762134
959861
106731231
11787611
128172831
138172832
148172833
Sheet2
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about
+Fluff 1.xlsm
ABC
1orderkeyIDorder reference ID
21109871
32568431
4367445211
54890762131
64890762132
74890762133
84890762134
959861
106731231
11787611
128172831
138172832
148172833
Main
Cell Formulas
RangeFormula
A2:A14A2=IF(B2<>B1,MAX(A$1:A1)+1,A1)
C2:C14C2=COUNTIFS(B$2:B2,B2)
 
Upvote 0
=IF(B2<>B1,MAX(A$1:A1)+1,A1)
Hi,
Thanks for the response.
I tried the below code:
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
For Each c In Range("A2:A" & Lastrow)
c.Value = "= IF(B2<>B1,MAX(A$1:A1)+1,A1)"
Next
Application.ScreenUpdating = True
End Sub

But it is filling all "1's" in Column A.
Can you please try once.

KR,
Halley
 
Upvote 0
Missed the fat that you wanted a macro, how about
VBA Code:
Sub Halleyyenn()
   With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
      .Formula = "= IF(B2<>B1,MAX(A$1:A1)+1,A1)"
      .Offset(, 2).Formula = "=COUNTIFS(B$2:B2,B2)"
   End With
End Sub
 
Upvote 0
Solution
Sub Halleyyenn() With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row) .Formula = "= IF(B2<>B1,MAX(A$1:A1)+1,A1)" .Offset(, 2).Formula = "=COUNTIFS(B$2:B2,B2)" End With End Sub
Perfect!!!Thanks alot:).
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,216
Members
449,091
Latest member
jeremy_bp001

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