簡體   English   中英

函數返回單元格值而不是單元格位置-VBA

[英]Function returns cells value not cells location - VBA

我正在嘗試為Regex設置一個返回cell.address 99%的工作正常,只是它似乎返回了單元格test值與單元格位置的相對值,我無法弄清楚。

調試:

如果我將Dim celladdr As Range ,則表示Object variable or with block variable not set

但是,如果我將其注釋掉,則對Object doesn't support this property or method的錯誤更改Object doesn't support this property or method並且可以看到celladdr = test

然后,我嘗試Set celladdr = Range(celladdr.Address)並獲取Object Required

有人可以指出錯誤嗎?

這是一些簡化的代碼:注意,由於該函數按預期工作,因此我對RegEx模式進行了硬編碼,問題似乎出在RegExSearch函數中,但是如果需要,我可以添加更多代碼。

Public Sub TESTING()
  Dim celladdr As Range
  celladdr = RegExFunc("TEST")
  ActiveSheet.celladdr.Select
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function RegExFunc(var) As Variant
  RegExSearchPattern = RegExPattern(var)
  RegExFunc = RegExSearch(RegExSearchPattern)
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function RegExPattern(my_string) As Variant
  RegExPattern = "([a-z]{4})"
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function RegExSearch(strPattern) As Range
    Dim regexp As Object
    Dim rcell As Range, rng As Range
    Dim strInput As String

    Set regexp = CreateObject("vbscript.regexp")
    Set rng = Range("A1:Z255")

    For Each rcell In rng.Cells

        If rcell <> "" Then

            If strPattern <> "" Then
            strInput = rcell.Value

            With regexp
                .Global = False
                .MultiLine = False
                .ignoreCase = True
                .Pattern = strPattern
            End With

            If regexp.Test(strInput) Then
                MsgBox rcell & " Matched in Cell" & rcell.Address
                Set RegExSearch = Range(rcell.Address)
                MsgBox RegExSearch
            End If
            End If
        End If
    Next
End Function

單元地址只是一個字符串; 您無需設置它,只需將其分配給=

RegExSearch = rcell.Address

...將返回絕對單元格地址。

如果找到了模式,您可能要考慮退出For Each rcell In rng.Cells循環中的For Each rcell In rng.Cells 除非您想要所有匹配單元格的並集的單元格地址,否則繼續執行似乎沒有任何意義。

        If regexp.Test(strInput) Then
            MsgBox rcell & " Matched in Cell" & rcell.Address
            RegExSearch = rcell.Address
            MsgBox RegExSearch
            Exit For
        End If

您正在循環內設置相同的RegEx參數。 將參數分配For Each rcell In rng.Cells循環中的For Each rcell In rng.Cells

    With regexp
        .Global = False
        .MultiLine = False
        .ignoreCase = True
        .Pattern = strPattern
    End With

    For Each rcell In rng.Cells

在很多情況下,您需要Set對象,而在其中需要一個Dim 由於您幾乎將所有內容都聲明為變體,因此很難確定故障原因。 根據我的經驗,最好總是在VBA中專門聲明變量。

我運行了以下調整后的代碼(如上所述,添加了一些設置),並且該代碼運行時沒有任何反編譯或運行時錯誤。

Public Sub TESTING()
  Dim celladdr As Range
  Set celladdr = RegExFunc("TEST")
  celladdr.Select
End Sub

Public Function RegExFunc(var As String) As Range
Dim RegExSearchPattern As String
  RegExSearchPattern = RegExPattern(var)
  Set RegExFunc = RegExSearch(RegExSearchPattern)
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function RegExPattern(my_string As String) As String
  RegExPattern = "([a-z]{4})"
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function RegExSearch(strPattern As String) As Range
    Dim regexp As Object
    Dim rcell As Range, rng As Range
    Dim strInput As String

    Set regexp = CreateObject("vbscript.regexp")
    Set rng = Range("A1:Z255")

    For Each rcell In rng.Cells

        If rcell <> "" Then

            If strPattern <> "" Then
            strInput = rcell.Value

            With regexp
                .Global = False
                .MultiLine = False
                .ignoreCase = True
                .Pattern = strPattern
            End With

            If regexp.Test(strInput) Then
                MsgBox rcell & " Matched in Cell" & rcell.Address
                Set RegExSearch = Range(rcell.Address)
                MsgBox RegExSearch
            End If
            End If
        End If
    Next
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM