Hi,
Backup your workbook first in case the code does not perform exactly as you
require.
Note: The code is written to run on the Active worksheet.
If it does not do exactly as you want then let me know. I have included
comments to help you follow what is occurring.
Sub Insert_Numbers()
Dim rngBranch As Range
Dim strBranch As String
Dim lngBranchNo As Long
Dim rngTarget As Range
Dim lngMinValue As Long
Dim lngMaxValue As Long
'Edit following 2 lines if start/finish numbers change
lngMinValue = 240229
lngMaxValue = 240241
lngBranchNo = 1
Do
'Create a string variable with Branch and Number.
strBranch = "Branch " & lngBranchNo
'Find Branch and assign cell to a variable
Set rngTarget = Columns("A:A") _
.Find(What:=strBranch, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not rngTarget Is Nothing Then
'Branch Number found
'Ensure first value is at correct starting value
If rngTarget.Offset(1).Value > lngMinValue Then
rngTarget.Offset(1).EntireRow.Insert
Set rngTarget = rngTarget.Offset(1)
rngTarget.Value = lngMinValue
Else
Set rngTarget = rngTarget.Offset(1)
End If
Do While rngTarget.Value < lngMaxValue
If Abs(rngTarget.Offset(1).Value - rngTarget.Value) > 1 Then
rngTarget.Offset(1).EntireRow.Insert
Set rngTarget = rngTarget.Offset(1)
rngTarget.Value = rngTarget.Offset(-1).Value + 1
Else
Set rngTarget = rngTarget.Offset(1)
End If
Loop
Else
'Branch number not found so exit sub
Exit Sub
End If
'Increment the Branch number to find
lngBranchNo = lngBranchNo + 1
Loop
End Sub
--
Regards,
OssieMac
"PVT" wrote:
> Hello,
>
> I am looking for a macro or other solution which can loop through my
> worksheet. The worksheet looks as follows:
>
> Branch 1
> 240230 x $22
> 240239 y $25
> 240240 z $26
>
> Totals
>
> Branch 2
> 240230 x $30
> 240231 a $58
> 240232 b $22
> 240241 c $18
>
> Totals
>
> Branch 3
> 240229 d $20
> 240231 a $22
> 240232 b $23
> 240240 z $22
>
> Totals
>
> I would like it to compare the value of the number in column A with
> that of the number above/below it. If there is a difference of more
> than 1, I would like it to insert a row with that number, so that
> eventually all the numbers are sequential. The inserted row should
> only have the number, no other values in that row.
>
> There should be a minimum (240229) and a maximum (240241) found for
> all values in column A of sheet 1 and everything in between there for
> each branch should be filled in.
>
> For example:
>
> Branch 1
> 240229
> 240230 x $22
> 240231
> 240232
> 240233
> 240234
> 240235
> 240236
> 240237
> 240238
> 240239 y $25
> 240240 z $26
> 240241
>
> Totals
>
> Branch 2
> 240229
> 240230 x $30
> 240231 a $58
> 240232 b $22
> 240233
> 240234
> 240235
> 240236
> 240237
> 240238
> 240239
> 240240
> 240241 c $18
>
> Totals
>
> Branch 3
> 240229 d $20
> 240230
> 240231 a $22
> 240232 b $23
> 240233
> 240234
> 240235
> 240236
> 240237
> 240238
> 240239
> 240240 z $22
> 240241
>
> Totals
>
> Any help would be greatly appreciated!
>
|