2014-04-17 2 views
0

셀을 워크 시트로 찾아야하지만 문자열을 찾는 것을 피하고 싶습니다.속성을 기반으로 셀 찾기

내가 가진 문제는 워크 시트가 내 클라이언트에 의해 편집된다는 것입니다. 혹시 그가 좋은 것을 찾기 전에 내가 찾고있는 문자열을 쓰려고 결정한다면, 앱은 추락 할 것이다.

Sub FindSpecificCell() 
    Dim ws As Worksheet 
    Set ws = ActiveWorkbook.Sheets("TP 1") 
    Dim myRange As Range 
    Dim rangeFinal As Range 
    Set monRange = ws.Range("A:AJ") 

    Set rangeFinal = myRange.Find("Description du test") 
    Debug.Print " " 
    Debug.Print "Looking for ""Description du test"" in TP 1 " 
    Debug.Print "column : " & rangeFinal.Column 
    Debug.Print "row : " & rangeFinal.Row 

End Sub 

좋은 셀에서 작동하는지 확인하기 위해 셀 안에 속성을 삽입하는 방법이 있습니까?

+1

을 나는이 이전과 같은 몇 가지 방법을 사용했습니다 범위 ("myCell") 또는 [myCell] –

답변

2

속성을 특정 셀과 직접 연결할 수는 없지만 워크 시트의 속성을 사용하여이 정보를 저장할 수 있습니다. 작성하고 값을 다시 읽어이 같은 당신이 할 수있는 그런

'Set the provided value of the custom property with the provided name in the provided sheet. 
Private Sub SetCustomPropertyValue(InSheet As Worksheet, WithPropertyName As String, WithValue As Variant) 
    Dim objCP As CustomProperty 
    Dim bolFound As Boolean 
    bolFound = False 'preset. 

    For Each objCP In InSheet.CustomProperties 
     'if this property's name is the one whose value is sought... 
     If (StrComp(objCP.Name, WithPropertyName, vbTextCompare) = 0) Then 
      objCP.Value = WithValue 
      bolFound = True 
      Exit For 
     End If 
    Next 

    'if the property didn't already exist on the sheet, add it. 
    If (Not bolFound) Then Call InSheet.CustomProperties.Add(WithPropertyName, WithValue) 
End Sub 

'Return the value of the custom property with the provided name in the provided sheet. 
Private Function GetCustomPropertyValue(InSheet As Worksheet, WithPropertyName As String) As Variant 
    Dim objCP As CustomProperty 
    GetCustomPropertyValue = Empty 

    For Each objCP In InSheet.CustomProperties 
     'if this property's name is the one whose value is sought... 
     If (StrComp(objCP.Name, WithPropertyName, vbTextCompare) = 0) Then 
      GetCustomPropertyValue = objCP.Value 
      Exit For 
     End If 
    Next 
End Function 

무엇인가 : 당신은 셀을 사용자 정의 이름을 지정하고 그 값을 얻을 수 있습니다

Sub test() 
    Dim strPropName As String 
    strPropName = "MyRange_" & Selection.Address 
    Dim strWhatIWantToStore As String 
    strWhatIWantToStore = "Here's what I want to store for this range" 

    Call SetCustomPropertyValue(ActiveSheet, strPropName, strWhatIWantToStore) 
    MsgBox GetCustomPropertyValue(ActiveSheet, strPropName) 
End Sub 
관련 문제