Create records

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
I have a list of values in column A/ Serial1, I have a list of values in Column E/ Name.

What I want to do is have the records populate like the 2 last columns NAMED OUTPUT SERIAL 1 AND OUTPUT NAME, so for each record in the name column it will have the serial next to it.
Sorry I didn't know how to explain it better, hopefully the example will help.

This is just an example, there are thousands of records to process.

It would be good if I could have an inputbox to ask which column holds the serial1 data and which column holds the name data, just in case the data is entered in a different column.
There are values in columns B to D but it is not required in the output.

I am using excel 2007 and 2010.

SERIAL 1NAMEOUTPUT SERIAL 1OUTPUT NAME
124800001248000
6358200412480000
92582060312482004
7347772526124820603
1200368731247772526
87387312436873
87387124873873
54312487387
8736124543
2131311248736
876124213131
888756124876
87124888756
12487
6358000
63580000
63582004
635820603
6357772526
63536873
635873873
63587387
635543
6358736
635213131
635876
635888756
63587

<tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Jaye7,

The following macro will search the active worksheet, row 1, for the titles SERIAL 1, and, NAME.

If it does not find both titles in row 1, you will receive a message to that fact, and, the macro will terminate.

Sample raw data:


Excel 2007
ABCDEFGH
1SERIAL 1NAME
212480000
363582004
4925820603
57347772526
6120036873
7873873
887387
9543
108736
11213131
12876
13888756
1487
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Sheet1


After the macro using three arrays in memory:


Excel 2007
ABCDEFGH
1SERIAL 1NAMEOUTPUT SERIAL 1OUTPUT NAME
21248000012480000
36358200412482004
4925820603124820603
573477725261247772526
612003687312436873
7873873124873873
88738712487387
9543124543
1087361248736
11213131124213131
12876124876
13888756124888756
148712487
15
1663580000
1763582004
18635820603
196357772526
2063536873
21635873873
2263587387
23635543
246358736
25635213131
26635876
27635888756
2863587
29
3092580000
3192582004
32925820603
339257772526
3492536873
35925873873
3692587387
37925543
389258736
39925213131
40925876
41925888756
4292587
43
4473480000
4573482004
46734820603
477347772526
4873436873
49734873873
5073487387
51734543
527348736
53734213131
54734876
55734888756
5673487
57
58120080000
59120082004
601200820603
6112007772526
62120036873
631200873873
64120087387
651200543
6612008736
671200213131
681200876
691200888756
70120087
71
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub CreateRecords()
' hiker95, 09/19/2014, ME806514
Dim s As Variant, n As Variant, o As Variant
Dim i As Long, j As Long, k As Long
Dim lrs As Long, lrn As Long, lc As Long, nlr As Long
Dim sn As Range, na As Range
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set sn = Rows(1).Find("SERIAL 1", LookAt:=xlWhole)
Set na = Rows(1).Find("NAME", LookAt:=xlWhole)
If (Not sn Is Nothing) * (Not na Is Nothing) Then
  Application.ScreenUpdating = False
  lrs = Cells(Rows.Count, sn.Column).End(xlUp).Row
  s = Range(Cells(1, sn.Column), Cells(lrs, sn.Column))
  lrn = Cells(Rows.Count, na.Column).End(xlUp).Row
  n = Range(Cells(1, na.Column), Cells(lrn, na.Column))
  nlr = (lrs * 2) * lrn
  ReDim o(1 To nlr, 1 To 2)
Else
  MsgBox "One, or, both titles 'SERIAL 1' 'NAME' were not found in row 1 - macro terminated!"
  Exit Sub
End If
k = k + 1
o(k, 1) = "OUTPUT SERIAL 1"
o(k, 2) = "OUTPUT NAME"
For i = 2 To lrs Step 1
  For j = 2 To lrn Step 1
    k = k + 1
    o(k, 1) = s(i, 1)
    o(k, 2) = n(j, 1)
  Next j
  k = k + 1
Next i
Cells(1, lc + 3).Resize(nlr, 2).Value = o
Columns(lc + 3).Resize(, 2).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CreateRecords macro.
 
Upvote 0
Great code, thanks very much.
Sorry I should have specified why I wanted the input box for the columns, the reason is that the names may change, are you able to help with this?.
 
Upvote 0
Jaye7,

Great code, thanks very much.

Thanks for the feedback.

You are very welcome. Glad I could help.

It would be good if I could have an inputbox to ask which column holds the serial1 data and which column holds the name data, just in case the data is entered in a different column.

I can do the above.

1. Can I have some examples of what the two column titles could be for testing purposes?

2. What row in the worksheet will contain the two column titles?

3. The two results columns, OUTPUT SERIAL 1, and, OUTPUT NAME, will be placed beginning in the third column after the last used column in the worksheet. Is that acceptable?
 
Last edited:
Upvote 0
Hi Hiker95.

The first column could be named PDescription or LNum, Second Column Could Be Named PNumber Or LDecim.
Row 1 would always have the Titles.
The output column in the 3rd empty column after the last column would be great.
 
Upvote 0
Jaye7,

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro code including the code line Option Compare Text
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Compare Text
Sub CreateRecords_V2()
' hiker95, 09/19/2014, ME806514
Dim s As Variant, n As Variant, o As Variant
Dim i As Long, j As Long, k As Long
Dim lrs As Long, lrn As Long, lc As Long, nlr As Long
Dim sn As Range, na As Range
Dim InSerial As String, InName As String
InSerial = InputBox("What is the column title for the 'Serial' data?")
InName = InputBox("What is the column title for the 'Name' data?")
If InSerial = "" Or InName = "" Then
  MsgBox "One, or, both titles '" & InSerial & "'  '" & InName & "' were not found in row 1 - macro terminated!"
  Exit Sub
End If
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set sn = Rows(1).Find(InSerial, LookAt:=xlWhole)
Set na = Rows(1).Find(InName, LookAt:=xlWhole)
If (Not sn Is Nothing) * (Not na Is Nothing) Then
  Application.ScreenUpdating = False
  lrs = Cells(Rows.Count, sn.Column).End(xlUp).Row
  s = Range(Cells(1, sn.Column), Cells(lrs, sn.Column))
  lrn = Cells(Rows.Count, na.Column).End(xlUp).Row
  n = Range(Cells(1, na.Column), Cells(lrn, na.Column))
  nlr = (lrs * 2) * lrn
  ReDim o(1 To nlr, 1 To 2)
Else
  MsgBox "One, or, both titles '" & InSerial & "'  '" & InName & "' were not found in row 1 - macro terminated!"
  Exit Sub
End If
k = k + 1
o(k, 1) = "OUTPUT SERIAL 1"
o(k, 2) = "OUTPUT NAME"
For i = 2 To lrs Step 1
  For j = 2 To lrn Step 1
    k = k + 1
    o(k, 1) = s(i, 1)
    o(k, 2) = n(j, 1)
  Next j
  k = k + 1
Next i
Cells(1, lc + 3).Resize(nlr, 2).Value = o
Columns(lc + 3).Resize(, 2).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CreateRecords_V2 macro.
 
Upvote 0
It does work, sorry to be a pain, but rather than having to type in the names of the fields (as the descriptions can be very long) are you able to change the input to just the column references, i.e. you just type column A or B or C etc... (for first column field name) and E or F or G (for second column field name).
 
Upvote 0
Jaye7,

1. Please explain in detail the purpose of/for this project/exercise?

2. Is this a homework assignment?
 
Upvote 0
Hi Hiker95, haha, no I am a bit old for homework, just a function that someone asked me for at work and I didn't have one in my bank of scripts.
 
Upvote 0
Jaye7,

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub CreateRecords_V3()
' hiker95, 09/20/2014, ME806514
Dim s As Variant, n As Variant, o As Variant
Dim i As Long, j As Long, k As Long
Dim lrs As Long, lrn As Long, lc As Long, nlr As Long
Dim ss As String, sn As String
Dim ns As Long, nn As Long
ss = InputBox("What is the column reference 'A, B, C....' for the 'Serial' data?")
sn = InputBox("What is the column reference 'A, B, C....' for the 'Name' data?")
ns = Asc(UCase(ss)) - 64
nn = Asc(UCase(sn)) - 64
lc = Cells(1, Columns.Count).End(xlToLeft).Column
lrs = Cells(Rows.Count, ns).End(xlUp).Row
lrn = Cells(Rows.Count, nn).End(xlUp).Row
If lrs <= 1 Or lrn <= 1 Then
  MsgBox "One, or, both columns '" & ss & "' or '" & sn & "' were blank - macro terminated!"
  Exit Sub
End If
Application.ScreenUpdating = False
s = Range(Cells(1, ns), Cells(lrs, ns))
n = Range(Cells(1, nn), Cells(lrn, nn))
nlr = (lrs * 2) * lrn
ReDim o(1 To nlr, 1 To 2)
k = k + 1
o(k, 1) = "OUTPUT SERIAL 1"
o(k, 2) = "OUTPUT NAME"
For i = 2 To lrs Step 1
  For j = 2 To lrn Step 1
    k = k + 1
    o(k, 1) = s(i, 1)
    o(k, 2) = n(j, 1)
  Next j
  k = k + 1
Next i
Cells(1, lc + 3).Resize(nlr, 2).Value = o
Columns(lc + 3).Resize(, 2).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CreateRecords_V3 macro.
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,084
Members
448,943
Latest member
sharmarick

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