2016-03-09 31 views
-1

我已經做了一個非常長的腳本來對excel文件進​​行reorganize。所以我正在與幾個工作,他們工作都很好。我想已瞭解了錯的代碼theese 2部分:VBA如果語句有時不起作用

If InStr(1, Cells(i, 5), "cass", 1) Then 
    Cells(i, 135).Value = Cells(i, 135).Value & "/cassetti.jpg::cassetti inox;;/cassetti2.jpg::cassetti inox;;" 
End If  

在單元格5我已經喜歡「卡塞蒂」或「Cassettiera」的話,現在用「中國社會科學院」字寫在細胞內,只是有時。這件事讓我從2天開始瘋狂。你看到我看不到的東西?

編輯: 這是我的代碼更重要組成部分:

Cells(1, 135) = "media_gallery" 
If InStr(1, Cells(i, 5), "tavol", 1) Or InStr(1, Cells(i, 5), "lav", 1) Or InStr(1, Cells(i, 5), "vasc", 1) Then 
Cells(i, 135).Value = img_tav 
End If 

If InStr(1, Cells(i, 5), "armadiat", 1) Then 
Cells(i, 135).Value = img_tav_arm 
End If 
If InStr(1, Cells(i, 5), "armadio", 1) Then 
Cells(i, 135).Value = img_arm 
End If 
If InStr(1, Cells(i, 5), "arm", 1) And InStr(1, Cells(i, 5), "cass", 1) Then 
Cells(i, 135).Value = img_cass_arm 
End If 
If InStr(1, Cells(i, 5), "caldo", 1) Then 
Cells(i, 135).Value = img_caldo 
End If 
If InStr(1, Cells(i, 5), "labirinto", 1) Then 
Cells(i, 135).Value = img_asp 
End If 
If InStr(1, Cells(i, 5), "lavatoio", 1) Or InStr(1, Cells(i, 5), "vasco", 1) Then 
Cells(i, 135).Value = img_lav 
End If 
If (InStr(1, Cells(i, 5), "lavat", 1) Or InStr(1, Cells(i, 5), "vasco", 1)) Then 
If InStr(1, Cells(i, 5), "armad", 1) Then 
     Cells(i, 135).Value = img_lav_arm 
End If 
End If 
If InStr(1, Cells(i, 5), "pens", 1) And InStr(1, Cells(i, 5), "scorr", 1) Then 
Cells(i, 135).Value = img_pensile 
End If 
If Cells(i, 5).Value Like "*cass*" Then 
Cells(i, 135).Value = Cells(i, 135).Value & "/cassetti.jpg::cassetti inox;;/cassetti2.jpg::cassetti  inox;;" 
End If 

而且theese是可變的開始腳本聲明:

img_tav = "/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;" 
img_tav_arm = "/anta.jpg::anta inox scorrevole;;/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/top-raggiato.jpg::Top raggiato opzionale;;/fianchi.jpg::fianchi inox lisci;;" 
img_arm = "/anta.jpg::anta inox scorrevole;;/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/fianchi.jpg::fianchi inox lisci;;" 
img_cass = "/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/top-raggiato.jpg::Top raggiato opzionale;;" 
img_cass_arm = "/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/top-raggiato.jpg::Top raggiato opzionale;;/fianchi.jpg::fianchi inox lisci;;" 
img_caldo = "/caldo.jpg::tavolo caldo;;/anta.jpg::anta inox scorrevole;;/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/top-raggiato.jpg::Top raggiato opzionale;;/fianchi.jpg::fianchi inox lisci;;" 
img_asp = "/labirinto.jpg::filtri a labirinto;;" 
img_lav = "/troppo-pieno.jpg::Troppo Pieno;;" 
img_lav_arm = "/troppo-pieno.jpg::Troppo Pieno;;/anta.jpg::anta inox scorrevole;;/piede.jpg::piede in acciaio inox;;/saldatura.jpg::saldatura;;/saldatura2.jpg::saldatura;;/top-raggiato.jpg::Top raggiato opzionale;;/fianchi.jpg::fianchi inox lisci;;" 
img_pensile = "/guide-pensili.jpg::Guide Anti Sporco;;/anta.jpg::anta inox scorrevole;;" 

所有這些都工作正常,但不是最後一個如果...

+1

大寫* C *和小寫* C *是不一樣的,你只正在測試爲小寫* c *。 * cass *匹配,因爲* c *是小寫; * Cassetti *不是因爲* C *是大寫。 –

+0

我還有其他的工作,即使如果我不使用大寫 – BigBlack

+0

顯示其餘的代碼。 .. –

回答

0

使用Option Compare Text位於放置此代碼的模塊的頂部,或者將您的字符串轉換爲一致的情況,例如:

Dim myString as Variant 
myString = LCase(Cstr(Cells(1,5).value)) 
If InStr(1, myString, "cass", 1) Then 
    Cells(i, 135).Value = Cells(i, 135).Value & "/cassetti.jpg::cassetti inox;;/cassetti2.jpg::cassetti inox;;" 
End If 
+0

我還有其他工作如果我不使用大寫 – BigBlack

0

嘗試使用Like

If myString Like "cass*" Then 

這是更具可讀性,更快,不區分大小寫

+0

即使是像它是一樣的...而我有其他如果使用相同的系統,他們正在工作,即使我在搜索字詞中有大寫字母 – BigBlack