本周我开始使用VBA,而且我的代码有点麻烦。我尝试了一些方法,但它归结为始终相同的问题:当我想将我的范围中的一些值分配给字符串时,输入Missmatch Error 13。
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也没有工作。
编辑:在将代码调整为评论中的建议之后,我遇到了: 运行时错误&#39; 94&#39;:无效使用Null
在同一个地方。
所以在我纠正这个问题之前,没有机会测试剩下的代码。
任何人都可以给我一个关于我做错的提示吗?花了两个小时阅读SO并进行实验,我确信它一定是显而易见的,但对我来说并非如此。
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
使用:
string_to_replace = var_src.Cells(counter, 1).Text
repl_unit_String = unit_src.Cells(counter, 2).Text
orig_String = I.Text
感谢大家的努力。
答案 0 :(得分:0)
您在FOR循环I
中指定的变量For Each I In m_Data
最终成为单个单元格。这就是for循环语句所说的“对于m_data范围内的每个Cell”。单元格也是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)
...而只是引用您在for循环中指定的I范围:
orig_String = I.Text
repl_String = I.Text
repl_unit_String = I.Text
说“对于范围'我',获取文本并将其粘贴在orig_string”