Home » excel » variables – Unhiding row on cell change

variables – Unhiding row on cell change

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is a slight duplication in question, but I believe it may simplify the situation.

Object of code: When a user picks an entry from the cell validation (i.e. on change) code must

  • enter user name in H column
  • enter time stamp in I column
  • Unhide next row
  • (Unexpected next step, show lines that aren’t user interactive)

Steps 1 to 3 are now functional, step 4 is not

Current code:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ChangedCell As Object


       For Each ChangedCell In Target
            If ChangedCell.Column = 5 And ChangedCell <> "" Then
                Cells(ChangedCell.Row, 8) = Environ("USERNAME")
                Cells(ChangedCell.Row, 9) = Format(Now(), "HH:MM:SS")
                ChangedCell.Offset(1, 0).EntireRow.Hidden = False
                If Cells(ChangedCell.Offset(1, 0), 1).Value <> "HC" Then
                ChangedCell.Offset(1, 0).EntireRow.Hidden = False
                End If
            End If
       Next
End Sub

Rob.

How to&Answers:

Try this:

ChangedCell.Offset(1, 0).EntireRow.Hidden = False

N.B :Probable Reason why your code caused error: Obviously you cant write into a read-only value.

enter image description here

APPENDED ANSWER FOR APPENDED QUESTION (STEP 4_”show lines that aren’t user interactive”):

Option Explicit

”Assuming informative (non interactive) cells are merged and this is upto Column E’s last row.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ChangedCell As Object
Dim last_Row, iter
last_Row = Worksheets("Sheet2").Cells(Rows.count, 5).End(xlUp).Row      ''change sheet name if different

       For Each ChangedCell In Target
          If ChangedCell.Column = 5 And ChangedCell <> "" Then

                For iter = 1 To last_Row
                ChangedCell.Offset(iter, 0).EntireRow.Hidden = False
                On Error GoTo errhand
                   If ChangedCell.Offset(iter, 0).MergeArea.Address = ChangedCell.Offset(iter, 0).Address Then
                   On Error GoTo 0
                   Cells(ChangedCell.Row, 8) = Environ("USERNAME")
                   Cells(ChangedCell.Row, 9) = Format(Now(), "HH:MM:SS")
                   'MsgBox ChangedCell.Row
                   Exit Sub
                   End If
errhand:
                'MsgBox "info row"
                Next
          End If
       Next
End Sub