WHEN vba finds Row in header then places row number below it

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
188
Office Version
  1. 2016
Platform
  1. Windows
ShCO01 is the worksheets CodeName
These are the variables needed

VBA Code:
With ShCO01

Dim LVC1 As String: LVC1 = .Cells(4, Columns.Count).End(xlToLeft).Value (needed for cell contents)

‘Last Value in last header

Dim LDC1 As Long: LDC1 = .Cells(4, Columns.Count).End(xlToLeft).Column

‘Last Data Column

Dim LDR1 As Long: LDR1 = .Range(.Cells(5, "C"), .Cells(150, LDC1)).Find("*", , , , xlByRows, xlPrevious).Row

‘Last Data Row in varying range

MsgBox "Last header in 4th row is column # " & LDC1 & " (" & Split(Cells(, LDC1).Address, "$")(1) & ") with a data collection for """ & LVC1 & """ and Last Data Row is " & LDR1

End With
I have a header in row 4 that is growing in length and every once-in-awhile I add Row to my data sets.

I want the VBA code to search my header and when it finds Row it starts one row below it and places the row number down the column until the LDR1 (last data row).

My last attempt was……and of course it’s not working.


VBA Code:
With ShCO01
Dim cll as Range

For Each cll In .Range(.Cells(4, "C"), .Cells(4, LDC1)).Cell

Select Case cll.Value = "Row"

Case True

cll.Offset(1, 0).Resize(LDR1, 0).Value = "=Row()"

Case False

cll.value = “”

End Select

Next

 End With
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Maybe
VBA Code:
...
For Each cll In .Range(.Cells(4, "C"), .Cells(4, LDC1))
    If cll.Value = "Row" Then
        cll.Offset(1).Resize(LDR1 - 4).Formula = "=ROW()"
        Exit For
    End If
Next cll
...
 
Upvote 0
Solution
Or
Code:
Sub Maybe()
Dim lc As Long, lr As Long
lc = Range("XFD4").End(xlToLeft).Column
lr = ActiveSheet.UsedRange.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    With ActiveSheet.Rows(4).Find("Row", , , 1).Offset(1).Resize(lr - 4)
        .Formula = "=ROW()"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Maybe
VBA Code:
...
For Each cll In .Range(.Cells(4, "C"), .Cells(4, LDC1))
    If cll.Value = "Row" Then
        cll.Offset(1).Resize(LDR1 - 4).Formula = "=ROW()"
        Exit For
    End If
Next cll
...
Or
Code:
Sub Maybe()
Dim lc As Long, lr As Long
lc = Range("XFD4").End(xlToLeft).Column
lr = ActiveSheet.UsedRange.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    With ActiveSheet.Rows(4).Find("Row", , , 1).Offset(1).Resize(lr - 4)
        .Formula = "=ROW()"
        .Value = .Value
    End With
End Sub
jolivanes: I went with your code with a small change
VBA Code:
lr = ActiveSheet.Range(.Cells(5, "B"), .Cells(LDR1, LDC1)).Find("*", , xlValues, , xlByRows, xlPrevious).Row
this works, but just like Tetra201 code it works only on the first Row it comes to, then stops
 
Upvote 0
So for how many rows should this be implemented and at which row number should it start?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,728
Messages
6,132,363
Members
449,720
Latest member
NJOO7

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