The code below is in a sheet and it pops up a message box filled with details in column…
- User
The code below is in a sheet and it pops up a message box filled with details in column 'N'
I want a similar code where when I click on a cell that contains a sheet name, it picks up all the details in column N of that sheet and dispalys it in a similar pop up box
Sub DisplayServices() Dim cellValue1 As String Dim cellValue2 As String Dim cellValue3 As String Dim cellValue4 As String Dim cellValue5 As String Dim cellValue6 As String Dim cellValue7 As String Dim cellValue8 As String Dim cellValue9 As String Dim cellValue10 As String Dim cellValue11 As String Dim cellValue12 As String Dim cellValue13 As String Dim cellValue14 As String Dim cellValue15 As String
' Change the cell references to capture values from different cells cellValue1 = Range("N1").Value cellValue2 = Range("N2").Value cellValue3 = Range("N3").Value cellValue4 = Range("N4").Value cellValue5 = Range("N5").Value cellValue6 = Range("N6").Value cellValue7 = Range("N7").Value cellValue8 = Range("N8").Value cellValue9 = Range("N9").Value cellValue10 = Range("N10").Value cellValue11 = Range("N11").Value cellValue12 = Range("N12").Value cellValue13 = Range("N13").Value cellValue14 = Range("N14").Value cellValue15 = Range("N15").Value ' Display the captured values in a pop-up message box MsgBox "" & cellValue1 & vbCrLf & _ "" & cellValue2 & vbCrLf & _ "" & cellValue3 & vbCrLf & _ "" & cellValue4 & vbCrLf & _ "" & cellValue5 & vbCrLf & _ "" & cellValue6 & vbCrLf & _ "" & cellValue7 & vbCrLf & _ "" & cellValue8 & vbCrLf & _ "" & cellValue9 & vbCrLf & _ "" & cellValue10 & vbCrLf & _ "" & cellValue11 & vbCrLf & _ "" & cellValue12 & vbCrLf & _ "" & cellValue13 & vbCrLf & _ "" & cellValue14 & vbCrLf & _ "" & cellValue15End Sub
- Assistant
Here is a modified code that achieves the desired functionality:
Sub DisplayServices() Dim cellValue As String Dim ws As Worksheet
' Get the value of the active cell cellValue = ActiveCell.Value ' Check if the active cell contains a sheet name On Error Resume Next Set ws = Worksheets(cellValue) On Error GoTo 0 ' If the sheet exists, capture values from column N If Not ws Is Nothing Then cellValue1 = ws.Range("N1").Value cellValue2 = ws.Range("N2").Value cellValue3 = ws.Range("N3").Value cellValue4 = ws.Range("N4").Value cellValue5 = ws.Range("N5").Value cellValue6 = ws.Range("N6").Value cellValue7 = ws.Range("N7").Value cellValue8 = ws.Range("N8").Value cellValue9 = ws.Range("N9").Value cellValue10 = ws.Range("N10").Value cellValue11 = ws.Range("N11").Value cellValue12 = ws.Range("N12").Value cellValue13 = ws.Range("N13").Value cellValue14 = ws.Range("N14").Value cellValue15 = ws.Range("N15").Value ' Display the captured values in a pop-up message box MsgBox "" & cellValue1 & vbCrLf & _ "" & cellValue2 & vbCrLf & _ "" & cellValue3 & vbCrLf & _ "" & cellValue4 & vbCrLf & _ "" & cellValue5 & vbCrLf & _ "" & cellValue6 & vbCrLf & _ "" & cellValue7 & vbCrLf & _ "" & cellValue8 & vbCrLf & _ "" & cellValue9 & vbCrLf & _ "" & cellValue10 & vbCrLf & _ "" & cellValue11 & vbCrLf & _ "" & cellValue12 & vbCrLf & _ "" & cellValue13 & vbCrLf & _ "" & cellValue14 & vbCrLf & _ "" & cellValue15 Else ' If the sheet does not exist, display an error message MsgBox "Invalid sheet name!" End If - User
The cell that will contain the sheet name will always be cell G3
- Assistant
Here is the modified code where the sheet name is picked up from cell G3:
Sub DisplayServices() Dim cellValue As String Dim ws As Worksheet
' Get the value of cell G3 cellValue = Range("G3").Value ' Check if the value is a valid sheet name On Error Resume Next Set ws = Worksheets(cellValue) On Error GoTo 0 ' If the sheet exists, capture values from column N If Not ws Is Nothing Then cellValue1 = ws.Range("N1").Value cellValue2 = ws.Range("N2").Value cellValue3 = ws.Range("N3").Value cellValue4 = ws.Range("N4").Value cellValue5 = ws.Range("N5").Value cellValue6 = ws.Range("N6").Value cellValue7 = ws.Range("N7").Value cellValue8 = ws.Range("N8").Value cellValue9 = ws.Range("N9").Value cellValue10 = ws.Range("N10").Value cellValue11 = ws.Range("N11").Value cellValue12 = ws.Range("N12").Value cellValue13 = ws.Range("N13").Value cellValue14 = ws.Range("N14").Value cellValue15 = ws.Range("N15").Value ' Display the captured values in a pop-up message box MsgBox "" & cellValue1 & vbCrLf & _ "" & cellValue2 & vbCrLf & _ "" & cellValue3 & vbCrLf & _ "" & cellValue4 & vbCrLf & _ "" & cellValue5 & vbCrLf & _ "" & cellValue6 & vbCrLf & _ "" & cellValue7 & vbCrLf & _ "" & cellValue8 & vbCrLf & _ "" & cellValue9 & vbCrLf & _ "" & cellValue10 & vbCrLf & _ "" & cellValue11 & vbCrLf & _ "" & cellValue12 & vbCrLf & _ "" & cellValue13 & vbCrLf & _ "" & cellValue14 & vbCrLf & _ "" & cellValue15 Else ' If the sheet does not exist, display an error message MsgBox "Invalid sheet name!" End If - User
error in code CellValue1 not defined
- Assistant
Apologies for the oversight. Please find below the corrected code:
Sub DisplayServices() Dim cellValue As String Dim ws As Worksheet Dim cellValue1 As String, cellValue2 As String, cellValue3 As String Dim cellValue4 As String, cellValue5 As String, cellValue6 As String Dim cellValue7 As String, cellValue8 As String, cellValue9 As String Dim cellValue10 As String, cellValue11 As String, cellValue12 As String Dim cellValue13 As String, cellValue14 As String, cellValue15 As String ' Get the value of cell G3 cellValue = Range("G3").Value ' Check if the value is a valid sheet name On Error Resume Next Set ws = Worksheets(cellValue) On Error GoTo 0 ' If the sheet exists, capture values from column N If Not ws Is Nothing Then cellValue1 = ws.Range("N1").Value cellValue2 = ws.Range("N2").Value cellValue3 = ws.Range("N3").Value cellValue4 = ws.Range("N4").Value cellValue5 = ws.Range("N5").Value cellValue6 = ws.Range("N6").Value cellValue7 = ws.Range("N7").Value cellValue8 = ws.Range("N8").Value cellValue9 = ws.Range("N9").Value cellValue10 = ws.Range("N10").Value cellValue11 = ws.Range("N11").Value cellValue12 = ws.Range("N12").Value cellValue13 = ws.Range("N13").Value cellValue14 = ws.Range("N14").Value cellValue15 = ws.Range("N15").Value ' Display the captured values in a pop-up message box MsgBox "" & cellValue1 & vbCrLf & _ "" & cellValue2 & vbCrLf & _ "" & cellValue3 & vbCrLf & _ "" & cellValue4 & vbCrLf & _ "" & cellValue5 & vbCrLf & _ "" & cellValue6 & vbCrLf & _ "" & cellValue7 & vbCrLf & _ "" & cellValue8 & vbCrLf & _ "" & cellValue9 & vbCrLf & _ "" & cellValue10 & vbCrLf & _ "" & cellValue11 & vbCrLf & _ "" & cellValue12 & vbCrLf & _ "" & cellValue13 & vbCrLf & _ "" & cellValue14 & vbCrLf & _ "" & cellValue15 Else ' If the sheet does not exist, display an error message MsgBox "Invalid sheet name!" End If End Sub - User
I want to use the code to work on a selection change on cell G3. Can you please check it this will work: If Target.CountLarge > 1 Then Exit Sub If Target.Range = G3 Then Call DisplayServices End If
- Assistant
- User
Can you please write me a line of code so that 'cellValue' is the Offset(0, 1) of the cell I click on
- Assistant
- User
Maybe this explains it better. In column C are names of companies. The sheet name that holds the companies information is on the same row in column D. When I click on any company name in column C, it should use the sheet name on the same row in column D to capture the details in column N of the sheet name
- Assistant
Understood. Here's the modified code to capture the details in column N of the corresponding sheet based on the company name clicked in column C:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub If Target.Column = 3 Then Dim ws As Worksheet Dim companyName As String Dim companyDetails As String companyName = Target.Value On Error Resume Next Set ws = Worksheets(Target.Offset(0, 1).Value) On Error GoTo 0 If Not ws Is Nothing Then companyDetails = ws.Range("N" & Target.Row).Value MsgBox companyDetails Else MsgBox "Invalid sheet name!" End If End If End SubIn this code, the
Worksheet_SelectionChangeevent captures the company name clicked in column C. It then retrieves the corresponding sheet name from the cell in column D of the same row usingOffset(0, 1). The code then attempts to set thewsvariable to the worksheet with the obtained sheet name.If a valid sheet is found, it captures the details in column N of the sheet using , where represents the row of the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.