我刚从本周开始使用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
感谢大家的努力。
对于m_Data中的每个I循环遍历单元格,所以'm_Data(I)'没有意义。 m_Data中的单元格也比其他两个范围中的单元格多,因此您也会遇到问题。 – Rory 2014-09-25 13:44:41
如果突出显示导致此问题的代码行,将有所帮助! – PaulFrancis 2014-09-25 13:44:46
@罗瑞只是在瞬间想到了。感谢仍然指出它 – AnyOneElse 2014-09-25 13:52:42