1

I have a PLC sending information to an Excel sheet, I have 5 cells receiving that information and I need to copy them into their specific column. Example

In A1 appears "hello world" and I need that to go to B2 (B1 has a title). Then in A1 appears "Bye world" and that get to B3 (B2 has hello world") Then in A1 "hello world" appears again and that needs to go to B4. Then in A1 another "hello world appears and that needs to go to B5, etc., etc., etc.

And like that forever if there's no empty spaces before, for example if B2 gets erased then the next thing that appears in A1 instead of going to B6 it will go to B2.

I need that for 5 cells of information A1 to B2~, A2 to C2~, A3 to D2~, A4 to E2~, A5 to F2~

I have a code but the problem is that it only detects changes when you do them manually, if you put a formula in A1 for example, the macro doesn't act, but if I type it, it does.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim column, columnInitial, columnCheck As Integer
    columnInitial = 1 'Columna donde van llegando los diferentes valores/ columns where the values arrive
    columnCheck = 2 'Columna INICIAL en numero que quieres que vaya usando para checar valores (no en letras because fuck you)/ Initial column in number for excel to check values

    Set KeyCells = Range("A1:A6") 'Rango de celdas en la columna definida donde llegaran los valores/cells range where the values arrive
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
            Dim i, col As Long
            Dim found, freeCell, switch As Integer
            switch = Cells(10, 1).Value ' Celda switch que activa o desactiva la comparacion de valores en la columna/Switch cell that activates or deactivates the comparison between values
            found = 0
            freeCell = 1
            For i = 1 To Rows.Count
                If switch = 0 Then
                    If Cells(i, columnCheck + (Target.Row - 1)).Value = Cells(1 + col, columnInitial).Value And Not IsEmpty(Cells(i, columnCheck + (Target.Row - 1)).Value) Then
                        found = 1
                    End If
                End If
                If IsEmpty(Cells(i, columnCheck + (Target.Row - 1)).Value) Then
                    freeCell = i
                    Exit For
                End If
            Next i
            If found = 0 Then
                Cells(freeCell, columnCheck + (Target.Row - 1)).Value = Cells(Target.Row, Target.column).Value
            End If
    End If
End Sub
M--
  • 25,431
  • 8
  • 61
  • 93
  • As you noticed, `Worksheet_Change` events do not work with formulas. If you are looking for a change in formula you will have to trigger your marco when any cell that changes your formula updates – urdearboy Feb 28 '20 at 23:16
  • I used the formula as an example since that also didnt work, what i do is send information to a cell via this =RSLINX|EXCEL!'Part_Number.DATA[0],L1,C1' I guess that you can take this as a formula, im not sure, so which "Worksheet_X" Should i use? Thanks in advance – Pedro Figueroa Feb 28 '20 at 23:27
  • Are you in the actual `Worksheet_Change` event? or did you copy/paste the code and subroutine name into a place where it is being treated as just a normal sub routine? It only gets triggered if it's in the right place. – braX Feb 28 '20 at 23:44
  • As already noted, the Change event doesn't trigger on a formula update, and a DDE link counts as a formula. The linked A shows you how to use the Calculate event – chris neilsen Feb 28 '20 at 23:57

0 Answers0