Determine column "I" value of the ActiveCell row

492 Views Asked by At

This code identifies the ActiveCell and sends the value of that cell to "I6". With the change of the values ​​that appear in "I6", the corresponding photographs of the students will change.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("I6").Value = ActiveCell.Value
End Sub

I get the value of the cell to appear in "I6" whenever I click on a cell in the table. As long as I click on the "I" column where the student numbers are.

The problem appears when I click on any other table cell ("I14:X43") outside this "I" column.

I need the code to always identify column "I" (I4:I43) of the active cell row.

Thus, whenever you are entering a record in any cell of the table ("I14:X43"), the code will identify the column "I" of that line and its value (student number). Identifying the value found in column "I" of the active cell row, it will appear in "I6", as it is in this formula and was changing the students' photographs.

At this moment, when I write a value inside the table, it will be this value that will appear in "I6" carrying a photograph that corresponds to the value that was registered/written and not to the number of the student of the line where I am.

A friend helped me with this code. It works up to column "Z".

Sub_ Selectionchange

    Dim x As String
    Dim and As String

    x = ActiveCell.Address(0, 0)
    y = WorksheetFunction.Replace(x, 1, 1, "=I")

    Range("I6").Value = y

End Sub

Clicking after that "Z" column, the code stops working and 0 appears in the "I6" column.

Trying to send the column "I" value of the ActiveCell row to cell "I6".

2

There are 2 best solutions below

6
wrbp On

Use the ActiveCell row and always use 9 => "I" column with Cells to get the value in column I in the same row of ActiveCell

Sub_ Selectionchange

Dim x As String

    Dim and As String
    Dim row as long
    row = ActiveCell.row
    If row >12 and row <44 Then
        Range("I6").Value = cells(row,9).value
    End If
End Sub


3
VBasic2008 On

A Worksheet Selection Change: Trigger Worksheet Change

enter image description here

Option Explicit

Private Const ID_CELL As String = "I6"

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Me.Range(ID_CELL), Target) Is Nothing Then Exit Sub

    ' Your code for showing the student's image.
    
    MsgBox "Like showing student's image for ID number " _
        & CStr(Me.Range(ID_CELL).Value) & ".", vbInformation
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const ID_RANGE As String = "I14:I43"

    Dim srg As Range: Set srg = Me.Range(ID_RANGE)
    Dim rrg As Range: Set rrg = srg.EntireRow ' srg.EntireRow.Columns("I:X")
    Dim irg As Range: Set irg = Intersect(rrg, Target)

    If irg Is Nothing Then Exit Sub
    
    Dim sCell As Range: Set sCell = Intersect(srg, irg.EntireRow).Cells(1)
    Dim dCell As Range: Set dCell = Me.Range(ID_CELL)
    
    ' If not equal, write, triggering the Worksheet Change event.
    If sCell.Value <> dCell.Value Then dCell.Value = sCell.Value
    
End Sub