2017-07-05 112 views
0

我在寫一個VBA程序。 我有一列excel vba查找like或regex

For j = 0 To 4 
    For i = 2 To lastrow 
     If Cells(i, 12).Value = groupnames(j) And Cells(i, 8).Value Like "*" & "[BLOCKED]" & "*" Then 
      groupsum(j) = groupsum(j) + 1 
     End If 
    Next i 
Next j 

問題是我有96個細胞這個字符串,但程序中發現了500我怎麼能這樣對正在進行的工作找到這個字符串[BLOCKED]有問題嗎?
感謝您的幫助

+0

不知道我理解,您收到錯誤消息?沒有得到預期的結果?它會有點難以幫助,而不看到你的數據的樣本,你在哪裏設置和初始化數組''groupnames' –

+0

@ShaiRado我認爲他期望96返回最大值,但發現500. –

+0

我有一列數據。這個數據我讀到一個數組。我想找到每個單元格這個字符串[BLOCKED]。例如一個單元格有「[BLOCKED] - xyz」。 –

回答

0

您的語法Like操作不正確。用途:

... Like "*[[]BLOCKED]*" 

[...]是一個字符類。所以,你的問題寫在你的問題上,它會發現BLOCKED集中的任何單個字符。這顯然不是你想要的。

要匹配[]字符,請將其括在字符類中,如我所示。爲了匹配]字符,它必須在字符類外。

+0

它並不適合我 –

+0

* does not -'t work * mean?如果這意味着你得到的結果與以前相同(500場比賽),那麼你有一個問題,而不是你所說的。如果你的意思是別的,明白我無法讀懂你的想法。請閱讀[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve),因爲我無法根據您提供的內容重現任何錯誤。 –

+0

對不起..我的意思是我有0比賽。如果我只是發現封鎖,我得到100匹匹配,所以其他部分的代碼是好的 –

0

這裏是我的代碼

Sub blocked() 

Dim SfileUsers As String 
Dim path As String 
Dim pathread As String 
Dim sFileread As String 
Dim lastrow As Long 
Dim keres() As Variant 
Dim groupadd() As String 
Dim groupnames(4) As String 
Dim groupsum(4) As Long 




path = "C:\Users\uids9282\Desktop\" 
SfileUsers = "Users.xlsx" 
Workbooks.Open path & SfileUsers 
Dim hossz As Long 
hossz = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row 
ReDim keres(hossz) 
ReDim groupadd(hossz) 
For i = 2 To hossz 
keres(i) = Sheets(1).Cells(i, 2).Value 
groupadd(i) = Sheets(1).Cells(i, 4).Value 
Next i 


'fájlmegnyitás 
pathread = "C:\Users\uids9282\Desktop\20170703\" 
sFileread = "open.xml" 
     If Dir(pathread & sFileread) = sFileread Then 
      Workbooks.Open pathread & sFileread 
      lastrow = Workbooks(sFileread).Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row 

      Else 
      MsgBox ("Nincs ilyen nevű excel táblázat. Kérem próbálkozzon újra") 
     End If 

'groupok hozzáadása a fájlhoz 
Dim user As String 
For j = 2 To hossz 
    For i = 2 To lastrow 
    user = Trim(Cells(i, 5).Value) 
     If user = keres(j) Then 
      Cells(i, 12).Value = groupadd(j) 
     End If 
    Next i 
Next j 

'group szummázása és átírása 
ThisWorkbook.Activate 
For i = 2 To 6 
groupnames(i - 2) = Cells(i, 1).Value 
Next i 
Workbooks(sFileread).Activate 
For j = 0 To 4 
    For i = 2 To lastrow 
     If Cells(i, 12).Value = groupnames(j) And Cells(i, 8).Value Like "*[[]BLOCKED[]]*" Then 'itt van benne a hiba!!   groupsum(j) = groupsum(j) + 1 
     End If 
    Next i 
Next j 
ThisWorkbook.Activate 
For j = 2 To 6 
    Cells(j, 4).Value = groupsum(j - 2) 
Next j 

Workbooks(SfileUsers).Close SaveChanges:=False 
Workbooks(sFileread).Close SaveChanges:=True 


End Sub 

this is my excel file where i want to searching

+0

這屬於您的問題的更新(編輯您的問題以包含此問題),因爲它不是您的問題的答案。此外,我沒有工具將Excel頁面的屏幕截圖轉換爲可用的Excel工作表。你用什麼工具來完成轉換? –