我试图确定列H中的单元格值(每个文件的行数是否变化)是“Words”还是“Words2”。不包含“Words”或“Words2”的单元格根据空格分隔符分割为单独的列,而包含“Words”和“Words2”的单元格则没有变化。目前我的代码不会分隔任何单元格。我是使用Dim作为Range和InStr的新手,我不完全确定使用它们是否最有效。我尝试了多种代码变形无济于事。 ARGNAME是H列中的每个单元格(或者至少是我的目标)。
例如:H1 =“Words”没有变化,而H2 =“我不关心的词”分为H2:L2
Sub WordSplit
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim SrchRng As Range, ARGNAME As Range
Set SrchRng = Range("H1:H" & lastRow)
For Each ARGNAME In SrchRng
If InStr(1, ARGNAME.Value, "Words") Or InStr(1, ARGNAME.Value, "Words2") < 0 _
Then Columns("H:H").TextToColumns Destination:=Range ("H1"), DataType _
:=xlDelimited, ConsecutiveDelimiter:=True, Space:=True, _
FieldInfo:=Array(Array(1, 1)),TrailingMinusNumbers:=True
End If
Next ARGNAME
End Sub
答案 0 :(得分:0)
Option Explicit
Public Sub WordSplit1()
Dim ws As Worksheet
Set ws = Sheet1
Dim lastRow As Long
lastRow = ws.Range("H" & Rows.Count).End(xlUp).Row
Dim srchRng As Range, argName As Range
Set srchRng = ws.Range("H1:H" & lastRow)
For Each argName In srchRng
If InStr(1, argName.Value2, "Words") = 0 And _
InStr(1, argName.Value2, "Words2") = 0 Then
argName.TextToColumns Destination:=argName, _
DataType:=xlDelimited, _
ConsecutiveDelimiter:=True, _
Space:=True, _
FieldInfo:=Array(Array(1, 1)), _
TrailingMinusNumbers:=True
End If
Next
End Sub
您的初始代码是在正确的轨道上,但我更新了以下内容:
Option Explicit
InStr
相关的问题:
InStr
表达式的返回值(不仅仅是最后一个)If InStr(1, ARGNAME.Value, "Words") Or InStr(1, ARGNAME.Value, "Words2") < 0
If InStr(1, argName.Value, "Words") = 0 And InStr(1, argName.Value, "Words2") = 0
If
语句被分隔到无效位置的第二行(语法错误)") < 0 _
Then Columns("H:H")
代码暗示除了H列之外没有数据
答案 1 :(得分:0)
尝试这样的事情
Sub WordSplit()
Dim lastRow As Long
Dim r As Long
lastRow = Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Count
For r = 1 To lastRow
If Cells(r, 8) = "Words" Or Cells(r, 8) = "Words2" Then
Else
Cells(r, 8).TextToColumns Destination:=Cells(r, 8),
DataType:=xlDelimited,TextQualifier:=xlDoubleQuote,
ConsecutiveDelimiter:=True
End If
Next r
End Sub