我正在尝试循环并在一长串代码列表中找到一些组件,这些代码可以转到不同的单元格。
现在我有了这个
Sub przenoszenie()
Dim wb As Workbook
Dim komorka As Range
Dim komorka2 As Range
Set wb = ThisWorkbook
Set komorka = wb.Worksheets("Dane").Range("B2")
Set komorka2 = wb.Worksheets("Dane").Range("J2")
For i = 1 To 10000
If InStr(1, komorka.Value, "width=109") And InStr(1, komorka.Value, "colSpan=8") Then
komorka2.Value = komorka.Value
Else
If InStr(1, komorka.Value, "width=90") Then
komorka2.Value = komorka.Value
Else
If InStr(1, komorka.Value, "width=374") Then
komorka2.Value = komorka.Value
End If
End If
End If
komorka = komorka.Offset(1, 0)
komorka2 = komorka2.Offset(1, 0)
Next i
我知道,我正在寻找的字符串的某些部分在我Excel的B列中。
但是我在J列中没有得到任何结果,我知道我在那些If
语句中遗漏了一些东西。只是搞不清楚;/
发布于 2021-01-22 12:26:20
可能的解决方案
尝试将Instr
条件更改为Instr(...) > 0
,如下所示:
Sub przenoszenie()
Dim wb As Workbook
Dim komorka As Range
Dim komorka2 As Range
Set wb = ThisWorkbook
Set komorka = wb.Worksheets("Dane").Range("B2")
Set komorka2 = wb.Worksheets("Dane").Range("J2")
For i = 1 To 10000
If InStr(1, komorka.Value, "width=109") > 0 And InStr(1, komorka.Value, "colSpan=8") > 0 Then
komorka2.Value = komorka.Value
Else
If InStr(1, komorka.Value, "width=90") > 0 Then
komorka2.Value = komorka.Value
Else
If InStr(1, komorka.Value, "width=374") > 0 Then
komorka2.Value = komorka.Value
End If
End If
End If
Set komorka = komorka.Offset(1, 0)
Set komorka2 = komorka2.Offset(1, 0)
Next i
如果这不能正常工作,那么另一个可能的问题是大小写匹配。若要使搜索不区分大小写,请使用例如Instr(1, komorka.Value, "width=374", vbTextCompare)
。
编辑:刚刚阅读@JvdV的评论(现在删除)并添加了Set
关键字
https://stackoverflow.com/questions/65844886
复制相似问题