Search code examples
vbaexcelmacrosmessagebox

How to create a message box for more than 160 rows (10836), to display text in multiple columns depending on which row is selected?


I'm using the code below to create a message box which displays when cells A1, A2, A3 ... until A9 are selected which will then display the text in the quotes as well as text in cells V1 (+21) , W1 (+22) and X1 (+23) for when A1 is selected for example, when A2 is selected it will display the string of text in the code below as well as text in the cells V2, W2 and X2 and so on. I need to do this for all 10836 rows but it seems as though 160 cases is the limit using this method. Is there a more efficient way to do this? Any help would be appreciated

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 Then
Select Case Target.Row
   Case 1, 2, 3, 4, 5, 6, 7, 8, 9
       MsgBox "Changes : " & Cells(Target.Row, Target.Column + 22) & vbNewLine & " ABC Comments: " & Cells(Target.Row, Target.Column + 23) & vbNewLine & "XYZ Comments: " & Cells(Target.Row, Target.Column + 21), vbInformation, "Comments"
   Case Else:
End Select
End If
End Sub

Solution

  • I was thinking along the same lines as Darren Bartrup-Cook; here it is in full:

       Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
       Dim LastRow As Integer
       LastRow = 10836
    
       If Target.Count > 1 Then Exit Sub
       If Target.Column = 1 Then
    
        Select Case Target.Row
           Case 1 To LastRow
               MsgBox "Changes : " & Cells(Target.Row, Target.Column + 22) & vbNewLine & " ABC Comments: " & Cells(Target.Row, Target.Column + 23) & vbNewLine & "XYZ Comments: " & Cells(Target.Row, Target.Column + 21), vbInformation, "Comments"
           Case Else:
        End Select
        End If
    
        End Sub