Find next visible cell, add a formula and then fill column down

n0tgirl

New Member
Joined
Oct 8, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have tried searching through old posts but nothing seems to fit my scenario, and I am such a VBA novice that I don't know how to pick out the parts that could potentially work. I have a macro that is filtering column N for the word "done." Then I am inserting a new column S and want to add a formula (to find the clean date from column Q) in all visible cells in S.
VBA Code:
Range("S2").Select
ActiveCell.FormulaR1C1 = "=INT(RC[-2])"

I realized that it is not working properly because the range that I have selected is not looking at the visible cell in column S, just starting in cell S2 and then the rest of my macro is providing bad data. What code would I use to find that first visible cell in Column S, regardless of it starting in Cell S6 or cell S266?

The next problem that I am having is that I would like to copy that formula to get my clean date in that visible cell and fill down to the last row in column S, so that is providing the date that is in the corresponding row in column Q. I have declared lastRow as Long
VBA Code:
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Any insight will be appreciated.
 

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.
Could you please try and share the result?

VBA Code:
Sub InsertFormulaInVisibleCells()
    Dim ws As Worksheet
    Dim rngVisible As Range

    Set ws = ActiveSheet

    On Error Resume Next
    Set rngVisible = ws.Range("S2:S" & ws.Cells(ws.Rows.Count, "S").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Not rngVisible Is Nothing Then
        rngVisible.FormulaR1C1 = "=INT(RC[-2])"
    Else
        MsgBox "No visible cells found in Column S after filtering."
    End If
End Sub
 
Upvote 0
Could you please try and share the result?

VBA Code:
Sub InsertFormulaInVisibleCells()
    Dim ws As Worksheet
    Dim rngVisible As Range

    Set ws = ActiveSheet

    On Error Resume Next
    Set rngVisible = ws.Range("S2:S" & ws.Cells(ws.Rows.Count, "S").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Not rngVisible Is Nothing Then
        rngVisible.FormulaR1C1 = "=INT(RC[-2])"
    Else
        MsgBox "No visible cells found in Column S after filtering."
    End If
End Sub
I pasted in the code above and when I step through, it just continues on to my next line of code after the end if. I am not getting any messages and S6 (my next visible cell) does not have the formula in it. It is just blank.
 
Upvote 0
Could you try it this way?

VBA Code:
Sub InsertFormulaInVisibleCells()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    Set ws = ActiveSheet

    If ws.Range("S1").EntireColumn.Hidden Then
        ws.Columns("S").Hidden = False
    ElseIf Application.WorksheetFunction.CountA(ws.Columns("S")) = 0 Then
        ws.Columns("S").Insert Shift:=xlToRight
    End If

    lastRow = ws.Cells(ws.Rows.Count, "Q").End(xlUp).Row

    For i = 2 To lastRow
        If Not ws.Rows(i).Hidden Then
            ws.Cells(i, "S").FormulaR1C1 = "=INT(RC[-2])"
        End If
    Next i
End Sub
 
Upvote 0
VBA Code:
Sub AddFormulaToVisibleRows()
Dim wb As Workbook, sht As Worksheet
Dim cell As Range, rng As Range

Set wb = Workbooks("Book1.xlsx")
Set sht = wb.Sheets("Sheet1")
Set rng = sht.Range("S2", Cells(sht.UsedRange.Rows.Count, "S"))

For Each cell In rng
    If sht.Rows(cell.Row).Hidden = False Then
        cell.FormulaR1C1 = "=INT(RC[-2])"
    End If
Next cell

End Sub
 
Upvote 0
Solution
VBA Code:
Sub AddFormulaToVisibleRows()
Dim wb As Workbook, sht As Worksheet
Dim cell As Range, rng As Range

Set wb = Workbooks("Book1.xlsx")
Set sht = wb.Sheets("Sheet1")
Set rng = sht.Range("S2", Cells(sht.UsedRange.Rows.Count, "S"))

For Each cell In rng
    If sht.Rows(cell.Row).Hidden = False Then
        cell.FormulaR1C1 = "=INT(RC[-2])"
    End If
Next cell

End Sub
 
Upvote 0
This worked. I had to tweak the wb and sht to match my info, but I was able to loop through and get the formula to populate in all visible cells in column S.

VBA Code:
    Set wb = ActiveWorkbook
    Set sht = ActiveSheet
    Set rng = sht.Range("S2", Cells(sht.UsedRange.Rows.Count, "S"))

    For Each cell In rng
    If sht.Rows(cell.Row).Hidden = False Then
        cell.FormulaR1C1 = "=INT(RC[-2])"
    End If
    Next cell

This worked! Thank you so much
 
Upvote 0
Could you try it this way?

VBA Code:
Sub InsertFormulaInVisibleCells()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    Set ws = ActiveSheet

    If ws.Range("S1").EntireColumn.Hidden Then
        ws.Columns("S").Hidden = False
    ElseIf Application.WorksheetFunction.CountA(ws.Columns("S")) = 0 Then
        ws.Columns("S").Insert Shift:=xlToRight
    End If

    lastRow = ws.Cells(ws.Rows.Count, "Q").End(xlUp).Row

    For i = 2 To lastRow
        If Not ws.Rows(i).Hidden Then
            ws.Cells(i, "S").FormulaR1C1 = "=INT(RC[-2])"
        End If
    Next i
End Sub
I tried this but it was unsuccessful. It never put the formula in column S and just went to my next line of code. It looks like Skyybot came up with a solution that seems to work for me. I appreciate you trying to help me out!!!
 
Upvote 0
fill down to the last row in column S
@n0tgirl how can it fill down to the last row in column S if you have just inserted the column, it would be blank?
Then I am inserting a new column S
Are you sure you don't mean to the last row in column N or Q?

If yes then change the red S in the code pitchoute posted in post 2 to the correct column
Rich (BB code):
Set rngVisible = ws.Range("S2:S" & ws.Cells(ws.Rows.Count, "S").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,653
Members
452,992
Latest member
TokugawaIesuma

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