VBA to create a table range based on a list

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Not sure if this is even possible but for every id in the list in worksheet 1, can I copy the data from workbook 2 cells A2 to V10 and create this for ID. Example below:

WORKSHEET 1
IDName
1585Dave
1938Jim
2033Steve
974Sarah
2323James
3070Helen
3070Helen

WORKSHEET 2
IDNameEarning/CodeAmount 1Amount 2Amount 3Amount 4Deduction/CodeDeduction/AmountDeduction/QuantityDeduction/PercentageDeduction/AdditionalContribution/CodeContribution/AmountContribution/QuantityContribution/PercentageContribution/AdditionalOverrides/Cost CentreOverrides/GL Business CodeOverrides/Transaction DateOverrides/CommentOverrides/Target Tax Period
1585​
DaveFuel
1585​
DaveComs1
1585​
DaveComs2
1585​
DaveComs3
1585​
DaveComs4
1585​
DaveAHP
1585​
DaveBonus
1585​
DaveMotab
1585​
DaveDedt
1938​
JimFuel
1938​
JimComs1
1938​
JimComs2
1938​
JimComs3
1938​
JimComs4
1938​
JimAHP
1938​
JimBonus
1938​
JimMotab
1938​
JimDedt
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Assuming Sheet2 already exists with headings, try this with a copy of your workbook.

VBA Code:
Sub ws2()
  Dim a As Variant, b As Variant, ec As Variant
  Dim i As Long, j As Long, k As Long
  
  ec = Split("Fuel|Coms1|Coms2|Coms3|Coms4|AHP|Bonus|Motab|", "|")
  
  With Sheets("Sheet1")
    a = .Range("A2", .Range("B" & Rows.Count).End(xlUp)).Value
  End With
  ReDim b(1 To UBound(a) * 9, 1 To 8)
  For i = 1 To UBound(a)
    For j = 0 To 8
      k = k + 1
      b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = ec(j)
    Next j
    b(k, 8) = "Dedt"
  Next i
  Sheets("Sheet2").Range("A2:H2").Resize(k).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,489
Messages
6,160,131
Members
451,621
Latest member
roccanet

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