VBA to create the column and populate the value based on a set of data

Binh Tran

New Member
Joined
Jun 17, 2021
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
Dear all,

I have this below data set:

Input data set:

PositionHeadcount
A2
B3
C2
D2
E4

I would like to use VBA to create another table as below in which it populate value of each position in accordance to the headcount column and when i change the headcount value, the table below will be auto updated. I am new to VBA and so this is too difficult for me. Hope that you can help me on this since i spent half of my day learning without any luck.

Desired output
Position
A
A
B
B
B
C
C
D
D
E
E
E
E
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Edit: Sorry, I forgot about the auto-update. I will address that in another post shortly.

Welcome to the MrExcel board!

Assuming your input data is on the active sheet, starting in cell A1, try this with a copy of your workbook. Results come in column D

VBA Code:
Sub Position_Head_Count()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
 
  a = Range("A1").CurrentRegion.Value2
  ReDim b(1 To Rows.Count, 1 To 1)
  For i = 2 To UBound(a)
    For j = 1 To a(i, 2)
      k = k + 1
      b(k, 1) = a(i, 1)
    Next j
  Next i
  Range("D1").Value = "Position"
  Range("D2").Resize(k).Value = b
End Sub

My data and results:

Binh Tran.xlsm
ABCD
1PositionHeadcountPosition
2A2A
3B3A
4C2B
5D2B
6E4B
7C
8C
9D
10D
11E
12E
13E
14E
15
Sheet1
 
Upvote 0
when i change the headcount value, the table below will be auto updated.
Try this Worksheet_Change event code. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test by altering a value in column A or B.
4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  
  If Not Intersect(Target, Columns("A:B")) Is Nothing Then
    a = Range("A1").CurrentRegion.Value2
    ReDim b(1 To Rows.Count, 1 To 1)
    For i = 2 To UBound(a)
      For j = 1 To a(i, 2)
        k = k + 1
        b(k, 1) = a(i, 1)
      Next j
    Next i
    Application.ScreenUpdating = False
    Range("D1", Range("D" & Rows.Count).End(xlUp)).ClearContents
    Range("D1").Value = "Position"
    Range("D2").Resize(k).Value = b
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Hi Peter,

Thank you very much for your answer. It worked perfectly.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,923
Members
449,094
Latest member
teemeren

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