2014-09-25 80 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