Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88126

Ignores macro if not typed

$
0
0

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

Viewing all articles
Browse latest Browse all 88126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>