creating a number index for row records

AussieSteve

New Member
Joined
Dec 28, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi guys.
This table is an example of what my ws looks like, I would like some help and information on how to code the action of creating the numbers you see along side all the records in column "C". I have tried with a macro, but it does not give me enough of an understanding of what is going on and how to proceed to modify the code. Any help is always appreciated.
Steve.


Company Before After Transactions
Company11Record1
2Record2
3Record3
4Record4
5Record5
6Record6
7Record7
8Record8
9Record9
10Record10
Company21Record1
2Record2
3Record3
4Record4
5Record5
6Record6

<tbody>
</tbody>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Steve
Maybe this then, assuming company names in "A" and required numbering in "B"

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
n = 1
For r = 2 To lr
    If Range("A" & r).Value <> "" Then
        Range("B" & r).Value = 1
    ElseIf Range("A" & r).Value = "" Then
        Range("B" & r).Value = Range("B" & r - 1).Value + 1
    End If
Next r
End Sub
 
Upvote 0
Hi Steve
Maybe this then, assuming company names in "A" and required numbering in "B"

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
n = 1
For r = 2 To lr
    If Range("A" & r).Value <> "" Then
        Range("B" & r).Value = 1
    ElseIf Range("A" & r).Value = "" Then
        Range("B" & r).Value = Range("B" & r - 1).Value + 1
    End If
Next r
End Sub

Hi Michael.
Thanks for your reply, I copied your code into a new wb and module saved wb as macro enabled wb.
Your code when run, I stepped through it, as soon as it hits sub it goes into break mode there are no syntax or run time errors. No number printed in column B. My coding and de-bugging skills are not sufficient see the problem.
 
Upvote 0
Hello Steve
I think this should create a data file that resembles the sample you gave in posting #1 . It takes the data from the "Transactions" column 'D', and places the number part in the 'After' column 'C', I hope this is what you wanted.

Code:
[COLOR=black][FONT=Arial]Sub CreateNumberIndex()[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   ' https://www.mrexcel.com/forum/excel-questions/1087125-creating-number-index-row-records.html[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   ' Created February 12, 2019[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Dim NumDataRows As Long       ' Number of data rows NOT counting headings[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Dim dTransactions As String       ' Each record in column 'D'[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Dim dChar As String                    ' Alphabetic characters[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Dim dRow As Long                      ' Counts the data row number[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Dim LenCtr As Long                    ' Counts number of characters in a 'D' column cell[/FONT][/COLOR]
  
[COLOR=black][FONT=Arial]Application.ScreenUpdating = False[/FONT][/COLOR]
[COLOR=black][FONT=Arial]NumDataRows = Cells(Rows.Count, "D").End(xlUp).Row[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   For dRow = 1 To NumDataRows[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]         dTransactions = Cells(dRow, "D")[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]             For LenCtr = 1 To Len(dTransactions)[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                 dChar = Asc(Mid(dTransactions, LenCtr, 1))[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                     If (dChar >= 65) Then          'Will be a letter[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                     Else                                      'Will be a number[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                         Cells(dRow, "C") = Right(dTransactions, Len(dTransactions) - (LenCtr - 1))[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                         LenCtr = Len(dTransactions)[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]                    End If[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]             Next LenCtr[/FONT][/COLOR]
  [COLOR=black][FONT=Arial]   Next dRow
Application.ScreenUpdating = True
End Sub[/FONT][/COLOR]

TotallyConfused
 
Last edited:
Upvote 0
Hello Steve
I think this should create a data file that resembles the sample you gave in posting #1 . It takes the data from the "Transactions" column 'D', and places the number part in the 'After' column 'C', I hope this is what you wanted.
The layout was not that clear to me either. Using your assumptions, here is a more compact macro to do what your code does...
Code:
[table="width: 500"]
[tr]
	[td]Sub CreateNumberIndex()
  Dim Ar As Range
  For Each Ar In Range("A1:A" & Cells(Rows.Count, "D").End(xlUp).Row).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1, 2).Resize(Ar.Count + 1) = Evaluate("ROW(1:" & Ar.Count + 1 & ")")
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
The layout was not that clear to me either. Using your assumptions, here is a more compact macro to do what your code does...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CreateNumberIndex()
  Dim Ar As Range
  For Each Ar In Range("A1:A" & Cells(Rows.Count, "D").End(xlUp).Row).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1, 2).Resize(Ar.Count + 1) = Evaluate("ROW(1:" & Ar.Count + 1 & ")")
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Hi Rick.
Your code works well enough for what I need to do, only two minor problems, the first is: if the company only has one record the program leaves that cell blank (no number recorded ) the second is : on the last company name it will not record numbers for, because it doesn't have a reference for it. So one cell down from the row of the last record I put the text, LastRowStop for the column it references. That works.
Thanks Steve.
 
Upvote 0
Hi Rick.
Your code works well enough for what I need to do, only two minor problems, the first is: if the company only has one record the program leaves that cell blank (no number recorded ) the second is : on the last company name it will not record numbers for, because it doesn't have a reference for it. So one cell down from the row of the last record I put the text, LastRowStop for the column it references. That works.
The following code fixes the first problem. As for the second problem... I am not sure what you are referring to as my code appears to number all the way down to the last company's record.
Code:
[table="width: 500"]
[tr]
	[td]Sub CreateNumberIndex()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells(Rows.Count, "D").End(xlUp).Row
  For Each Ar In Range("A1:A" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1, 2).Resize(Ar.Count + 1) = Evaluate("ROW(1:" & Ar.Count + 1 & ")")
  Next
  On Error GoTo NoSingles
  Range("C1:C" & LastRow).SpecialCells(xlBlanks).Value = 1
NoSingles:
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
The following code fixes the first problem. As for the second problem... I am not sure what you are referring to as my code appears to number all the way down to the last company's record.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CreateNumberIndex()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells(Rows.Count, "D").End(xlUp).Row
  For Each Ar In Range("A1:A" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1, 2).Resize(Ar.Count + 1) = Evaluate("ROW(1:" & Ar.Count + 1 & ")")
  Next
  On Error GoTo NoSingles
  Range("C1:C" & LastRow).SpecialCells(xlBlanks).Value = 1
NoSingles:
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Hi Rick.
Thanks for all your help, I will trust you in what say about your code, I am the one learning from you and the people that contribute to this forum. And thanks to Michael, and Totally Confused for their contribution as well.
Steve.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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