Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

excel - display all row values of that cell by clicking on it

I have this macro which takes a search term if the search term is found displays all values of the same row in a pop up window -- if the value is not empty the search term needs to be inserted into a cell and then the macro gets activated by clicking on a button attached to it
I want to modify the macro to do the same thing with the exception to just click on the cell and display the other row values, without typing in a search term and clicking on the button

also have a question about the pop up window in the macro -- why is the text not selectable ?

Option Explicit

Const rangeForSearch = "G2"

Const rowTitles = 4

Dim arrTmp
Dim lastRow As Long, lastColumn As Long
Dim textForSearch As String, textForSearch_withoutSpaces As String
Dim strTmp As String
Dim i As Long, j As Long

Sub searchPerson()
    Application.ScreenUpdating = False
    With ActiveSheet
        textForSearch = .Range(rangeForSearch)
        If textForSearch = "" Then
            MsgBox "Input text in cell """ & rangeForSearch & """ and try again!", vbCritical
            Application.ScreenUpdating = True
            Exit Sub
        End If
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastColumn = .Cells(rowTitles, .Columns.Count).End(xlToLeft).Column
        If lastRow <= rowTitles Or lastColumn <= 2 Then
            MsgBox "Dataset is wrong! Check it and try again!", vbCritical
            Application.ScreenUpdating = True
            Exit Sub
        End If
        
        arrTmp = .Range(.Cells(rowTitles, "A"), .Cells(lastRow, lastColumn))
    End With
    '---------------------------------------
    textForSearch_withoutSpaces = Replace(textForSearch, " ", "")
    
    For i = LBound(arrTmp, 1) + 1 To UBound(arrTmp, 1)
        strTmp = Replace(arrTmp(i, 1) & arrTmp(i, 2), " ", "")
        If StrComp(textForSearch_withoutSpaces, strTmp, vbTextCompare) = 0 Then Exit For
    Next i
    If i = UBound(arrTmp, 1) + 1 Then
            strTmp = textForSearch & vbCrLf & vbCrLf & "No dataset!"
        Else
            strTmp = textForSearch
            
            For j = 3 To lastColumn
            If Not IsEmpty(arrTmp(i, j)) Then strTmp = strTmp & vbCrLf & vbCrLf & arrTmp(1, j) & ": " & arrTmp(i, j)
            Next j
            
            
            
    End If
    Application.ScreenUpdating = True
    MsgBox strTmp, , "Result"
End Sub




与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...