2014-09-25 81 views
0

我剛從本週開始使用VBA,並且正在使用我的代碼。我嘗試了一些方法,但它歸結爲總是相同的問題:當我想從範圍中分配一些值到字符串時,鍵入Missmatch錯誤13。VBA錯誤13&94,比較字符串

 orig_String = m_Data(counter).Text 
     repl_String = var_src(counter).Text 
     repl_unit_String = unit_src(counter).Text     
     If repl_String <> orig_String Then 

Typecasting CStr(...)或使用.Text而不是.Value也不起作用。

編輯:代碼調整從意見建議後,我碰到: 運行時錯誤「94」:在同一個地方無效使用空

的。

因此,在我糾正該問題之前,沒有機會測試代碼的其餘部分。

任何人都可以給我一個暗示我做錯了什麼嗎?花了兩個小時閱讀和實驗,我相信它一定是顯而易見的,但它不是我。

Sub rename_Channel(ByRef WB As Workbook, ByRef WS As Worksheet) 
     ' Compare Variables from var_src with Variables of m_Data (Row 1), if var_src VarName not found 
     ' in m_Data, make new column fill values with "MISSING", if found but in different order, 
     ' make it next to ensure data always has the same pattern. 

     'pseudocode - what do I want to do here? 
     'from worksheet get: items_to_rename 
     'for column in originalWS do: 
     ' if find column.entry in items_to_rename: 
     ' copy column to new_Worksheet.last+1 
     ' new_column.name = channelstoRename.replace(name) 
     ' new_column.unit = channelsToRename.replacement(unit) 


     Dim Variablen As Worksheet 
     Dim m_DataStripped As Worksheet 

     Dim var_src As Range 
     Dim unit_src As Range 
     Dim m_Data As Range 
     Dim orig_String As String 
     Dim repl_String As String 
     Dim repl_unit_String As String 
     Dim counter As Integer 

     Set WS_Vars = WB.Worksheets("Variablen") '"lookup" table 
     Set WS_Orig = WS 
     Set var_src = WS_Vars.Range("B4:B261") 'column with replacement names 
     Set unit_src = WS_Vars.Range("D4:D261") 'column with replacement units 
     Set m_Data = WS_Orig.Rows(1) 'row with original names 
     counter = 0 


     For Each I In m_Data 

     counter = counter + 1 
     If counter = 259 Then 
      Exit Sub 
     End If 

     orig_String = m_Data(counter).Text 
     repl_String = var_src(counter).Text 
     repl_unit_String = unit_src(counter).Text     
     If repl_String <> orig_String Then 
       If Not m_Data.Find(repl_String) Is Nothing Then 
        m_DataStripped.Cells(1, 1).End(xlToRight).Value = repl_String 
        m_DataStripped.Cells(2, 1).End(xlToRight).Value = repl_unit_String 
        'Copy Values into first empty Col to the right 
        m_Data.Find(repl_String).Copy (m_DataStripped.Cells(3, 1).End(xlToRight)) 
       Else 
        If m_Data.Find(repl_String) Is Nothing Then 
        m_DataStripped.Cells(1, 1).End(xlToRight).Value = repl_String 
        m_DataStripped.Cells(3, 1).End(xlToRight).Value = "MISSING" 
        End If 
       End If 
      End If 
     Next 

    End Sub 

編輯:感謝提意見和答案,我設法得到它的工作,更換

For Each I In m_Data 

有了:

For Each I In m_Data.Columns 

而且

orig_String = m_Data(counter).Text 
    repl_String = var_src(counter).Text 
    repl_unit_String = unit_src(counter).Text    

With:

string_to_replace = var_src.Cells(counter, 1).Text 
    repl_unit_String = unit_src.Cells(counter, 2).Text 
    orig_String = I.Text 

感謝大家的努力。

+1

對於m_Data中的每個I循環遍歷單元格,所以'm_Data(I)'沒有意義。 m_Data中的單元格也比其他兩個範圍中的單元格多,因此您也會遇到問題。 – Rory 2014-09-25 13:44:41

+0

如果突出顯示導致此問題的代碼行,將有所幫助! – PaulFrancis 2014-09-25 13:44:46

+0

@羅瑞只是在瞬間想到了。感謝仍然指出它 – AnyOneElse 2014-09-25 13:52:42

回答

0

您在FOR循環For Each I In m_Data中分配的變量I最終成爲單個單元格。這就是for循環語句所說的「對於m_data範圍中的每個單元格」。一個單元格也是一個range對象。

而且你的線條

orig_String = m_Data(counter).Text 
    repl_String = var_src(counter).Text 
    repl_unit_String = unit_src(counter).Text 

說的 「分配M_DATA(1)的.text到orig_string」 這是沒有意義的。沒有m_data(1)這樣的東西。有可能是一個m_data("A" & counter),或m_data.offset(0,counter)m_data.cells(counter, 1) ...而不是僅僅指的是你爲循環分配中的I範圍:那說:「對於範圍‘我’,讓文字和

orig_String = I.Text 
    repl_String = I.Text 
    repl_unit_String = I.Text 

將它粘貼在orig_string中「

+0

好的...所以我想,因爲我分配了一維數組到m_data(Row1的內容),我可以像訪問數組一樣訪問它。整個單元格和行和列的東西對我來說是全新的...... – AnyOneElse 2014-10-01 06:43:05

+0

但是,如果我將I.Text分配給每個變量,它們都將是相同的。不過,我希望將行I中特定列的值複製到變量中。還是我在這裏讓你完全錯了? – AnyOneElse 2014-10-01 06:50:39