Skip to main content
added 2 characters in body
Source Link
Ike
  • 13.4k
  • 6
  • 20
  • 38

I have table with data that I need to transfer and everything is working fine but I need a control check before the actual code starts. The user needs to specify himself if he wants to process the data from a specific row by selecting 'process' in the specific column. But you should only be allowed to process it if the movement is the same on each selected row.

For example | Row | Movement | Amount | Action | | -------- | ---------- | ----------- | -----------| | 1 | Income | 1000 | Process | | 2 | Other | 500 | | | 3 | Expenses | 200 | Process | | 4 | Income | 300 | |

RowMovementAmountAction
1Income1000Process
2Other500
3Expenses200Process
4Income300

In this case an error needs to pop up stating that the action cannot be done as we have selected 'income' in row 1 and 'expenses' in row 3.

My thought was to compare the previous movement type with the current but it's not working because the movement of row 1 'is already forgotten'.

My code was as follows:

Dim Movement As String
Dim Movement_previous as String

For i = 2 To LastRow
    Movement = Range("Movement").Cells(i).Text
    Movement_previous = Range("Movement").Cells(i-1).Text
    If ActiveSheet.Range("Action").Cells(i).Value = "Process" Then
        If Movement <> Movement_previous Then
            MsgBox ("You can only select one movement type per booking.")
            Exit Sub
        End If
        
        'Actual code
        
    End If
Next i

Is there a workaround to 'remember' the previous variable string? Or is there a better solution overall?

I have table with data that I need to transfer and everything is working fine but I need a control check before the actual code starts. The user needs to specify himself if he wants to process the data from a specific row by selecting 'process' in the specific column. But you should only be allowed to process it if the movement is the same on each selected row.

For example | Row | Movement | Amount | Action | | -------- | ---------- | ----------- | -----------| | 1 | Income | 1000 | Process | | 2 | Other | 500 | | | 3 | Expenses | 200 | Process | | 4 | Income | 300 | |

In this case an error needs to pop up stating that the action cannot be done as we have selected 'income' in row 1 and 'expenses' in row 3.

My thought was to compare the previous movement type with the current but it's not working because the movement of row 1 'is already forgotten'.

My code was as follows:

Dim Movement As String
Dim Movement_previous as String

For i = 2 To LastRow
    Movement = Range("Movement").Cells(i).Text
    Movement_previous = Range("Movement").Cells(i-1).Text
    If ActiveSheet.Range("Action").Cells(i).Value = "Process" Then
        If Movement <> Movement_previous Then
            MsgBox ("You can only select one movement type per booking.")
            Exit Sub
        End If
        
        'Actual code
        
    End If
Next i

Is there a workaround to 'remember' the previous variable string? Or is there a better solution overall?

I have table with data that I need to transfer and everything is working fine but I need a control check before the actual code starts. The user needs to specify himself if he wants to process the data from a specific row by selecting 'process' in the specific column. But you should only be allowed to process it if the movement is the same on each selected row.

For example

RowMovementAmountAction
1Income1000Process
2Other500
3Expenses200Process
4Income300

In this case an error needs to pop up stating that the action cannot be done as we have selected 'income' in row 1 and 'expenses' in row 3.

My thought was to compare the previous movement type with the current but it's not working because the movement of row 1 'is already forgotten'.

My code was as follows:

Dim Movement As String
Dim Movement_previous as String

For i = 2 To LastRow
    Movement = Range("Movement").Cells(i).Text
    Movement_previous = Range("Movement").Cells(i-1).Text
    If ActiveSheet.Range("Action").Cells(i).Value = "Process" Then
        If Movement <> Movement_previous Then
            MsgBox ("You can only select one movement type per booking.")
            Exit Sub
        End If
        
        'Actual code
        
    End If
Next i

Is there a workaround to 'remember' the previous variable string? Or is there a better solution overall?

Source Link

Remember old string during loop

I have table with data that I need to transfer and everything is working fine but I need a control check before the actual code starts. The user needs to specify himself if he wants to process the data from a specific row by selecting 'process' in the specific column. But you should only be allowed to process it if the movement is the same on each selected row.

For example | Row | Movement | Amount | Action | | -------- | ---------- | ----------- | -----------| | 1 | Income | 1000 | Process | | 2 | Other | 500 | | | 3 | Expenses | 200 | Process | | 4 | Income | 300 | |

In this case an error needs to pop up stating that the action cannot be done as we have selected 'income' in row 1 and 'expenses' in row 3.

My thought was to compare the previous movement type with the current but it's not working because the movement of row 1 'is already forgotten'.

My code was as follows:

Dim Movement As String
Dim Movement_previous as String

For i = 2 To LastRow
    Movement = Range("Movement").Cells(i).Text
    Movement_previous = Range("Movement").Cells(i-1).Text
    If ActiveSheet.Range("Action").Cells(i).Value = "Process" Then
        If Movement <> Movement_previous Then
            MsgBox ("You can only select one movement type per booking.")
            Exit Sub
        End If
        
        'Actual code
        
    End If
Next i

Is there a workaround to 'remember' the previous variable string? Or is there a better solution overall?