I have a number of data sources that I need to bring into excel for analysis. I start each data set by pasting a one line header on Row 1 starting in Column B that will go to Column "X" (each source has a different number of columns). Column A Row 1 I manually type a reference code for the page the data was found.
Each page of the original data set contains multiple lines of data, while each line is formatted the same there is different values.
I can manually paste the data into excel starting on Column B, then go to Column A by the first new row and put the page number then auto fill down or drag to the end of that set.
However I would like to set some VBA code to auto fill the page number based on an incremental value increas of 1.
Not all paste operations will have the same number of rows but each set is the same page. For example page 5 might be 9 rows that need to be labeled page 5, then page 6 might have 25 rows.
I tried the code below...which may be repetitive and not the most refined but it almost got me there.
This code allows me to paste my header Row into Row 1 starting in Cell B1 and type in A1 without triggering any errors.
I can then paste my rows of data into Excel into cell B2 and Excel understands that the paste operation is multiple rows and labels A2-A# with the value 1 based on the number of rows I inserted.
However this is where I am stuck if I paste the next set of data into B# ( the next blank row below where my last set ended) Excel still puts a 1 next to all the new rows instead of a 2.
I would like the code to verify the value used in Column A for the last set then increase by an increment of 1 for the next set.
Any help would be appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim i As Integer
Dim lastRowB As Long
Dim firstBlankCellA As Range
Dim firstBlankCellB As Range
Set rng = Range("B:B")
' Exit if the row being edited is row 1 (Title Row)
If Target.Row = 1 Then Exit Sub
' Find the last row with data in column B
lastRowB = Cells(Rows.Count, "B").End(xlUp).Row
' Find the next blank cell in column A
Set firstBlankCellA = Cells(lastRowB + 1, "A")
' Find the next blank cell in column B
Set firstBlankCellB = Cells(lastRowB + 1, "B")
If Not Intersect(Target, rng) Is Nothing Then
' If the cell two rows above the first blank cell in Column A exists and is preceded by a number,
' use the next numerical value for all rows with new text
If firstBlankCellA.Row > 2 And IsNumeric(firstBlankCellA.Offset(-2, 0).Value) Then
i = firstBlankCellA.Offset(-2, 0).Value + 1
Else
i = 1
End If
' Add the numerical value to each row with new text
For Each cell In Target.Cells
If cell.Value <> "" And cell.Offset(0, -1).Value = "" Then
cell.Offset(0, -1).Value = i
End If
Next cell
' Select the first blank cell in column A for the next paste job
If Target.Rows.Count > 1 Then
i = i + Target.Rows.Count - 1
End If
firstBlankCellA.Offset(1, 0).Select
firstBlankCellB.Offset(1, 0).Select
' Select the first blank cell in column B
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Select
End If
End Sub