2016-10-11 82 views
0

我想從單元格D14開始的所有單元格中第一次出現的開括號之後刪除所有單詞。我不斷提出一個Object Required錯誤,並不確定問題出在哪裏。VBA刪除字符串後的字

Dim startRow As Integer 
Dim pos_of_parenth As Integer 
Dim resultString as String 

startRow = 15 

With Windows(NewTemplateName) 
    .Activate 
    .ActiveSheet.Range("D" & startRow).Select 
    Do While .ActiveSheet.Range("D" & startRow).Value <> "" 
     resultString = "" 
     pos_of_parenth = InStr(1, ActiveSheet.Range("D" & startRow).Text, "(", CompareMethod.Text) 
     resultString = Left(.ActiveSheet.Range("D" & startRow).Value, pos_of_parenth) 
     startRow = startRow + 1 
    Loop 
End With 

回答

2

變化CompareMethod.TextVbCompareMethod.vbTextCompare

+1

相關的修復它:始終指定'選項Explicit'。 'CompareMethod.Text'會彈出一個未聲明的標識符,這應該是OP的第一個線索。 –

+0

Option Explicit是一個好習慣! –

0

我也結束了通過簡單地擺脫 「CompareMethod.Text」 完全