Home > database >  VBA for column "I" value of the ActiveCell row
VBA for column "I" value of the ActiveCell row

Time:11-24

Goodnight,

I have this code that 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

With this code, 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, everything is fine.

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

I needed 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.

However, a friend helped me with this code which works for what I intended but only 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.

Can anyone help, please?

Try to getting a vba code to send the column "I" value of the ActiveCell row to cell "I6"

CodePudding user response:

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

    Range("I6").Value = cells(ActiveCell.row,9).value

End Sub

CodePudding user response:

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
  • Related