我有一个我一直在研究的vba脚本(几百行)。一段代码应该遍历一行单元格,并添加一个"字符串"在其余数据之前。我无法弄清楚为什么这不起作用,但我怀疑我用原始值以某种方式覆盖我的数据。非常感谢帮助。谢谢。
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For y = 1 To 26
If UCase(Cells(1, y).Value) Like "*MISC COMMENTS*" Then
Cells(1, y).Value = "Comments"
For Q = 2 To lastRow
If Cells(Q, y).Value <> "" Then
Cells(Q, y).Value = "Misc Comment - " & Cells(Q, y).Value
End If
Next Q
End If
Next y
数据看起来像
Misc Comments etc. '<-- Header'
This is a simple '<-- Data'
Problem but I '<-- Data'
cannot figure '<-- Data'
it out. '<-- Data'
我相信我的脚本应该将数据转换为
Comments '<-- Header'
Misc Comment - This is a simple '<-- Data'
Misc Comment - Problem but I '<-- Data'
Misc Comment - cannot figure '<-- Data'
Misc Comment - it out. '<-- Data'
没有错误的实际结果(数据未更改)
Misc Comments etc. '<-- Header'
This is a simple '<-- Data'
Problem but I '<-- Data'
cannot figure '<-- Data'
it out. '<-- Data'
我相信我的错误在以下块中
If Cells(Q, y).Value <> "" Then
Cells(Q, y).Value = "Misc Comment - " & Cells(Q, y).Value
End If
虽然我无法弄清楚我是如何错误地调用它的。
答案 0 :(得分:3)
MISC COMMENTS
与If UCase(Cells(1, y).Value) Like "*MISC COMMENTS*"
不同。您需要使用通配符比较字符串。
Dim y As Long, q As Long, lastRow As Long, lastRowString As String
lastRowString = "last row of activesheet code"
With Worksheets("sheet2")
For y = 1 To 26
If UCase(.Cells(1, y).Value) Like "*MISC COMMENTS*" Then
.Cells(1, y).Value = "Comments"
lastRow = .Cells(Rows.Count, y).End(xlUp).Row
For q = 2 To lastRow
If .Cells(q, y).Value <> "" Then
.Cells(q, y) = "Misc Comment - " & .Cells(q, y).Value
End If
Next q
Exit For '<~~ really want to exit the loop?
End If
Next y
End With
这是一个重写,还有一些更改。目前还不清楚您想要对包含&#39;活动表代码的最后一行&#39; 的var做什么。
{{1}}