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

n0tgirl

New Member
Joined
Oct 8, 2024
Messages
4
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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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

Forum statistics

Threads
1,222,564
Messages
6,166,818
Members
452,074
Latest member
Alexinho

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