此代码出现问题。没有错误但似乎也没有做任何事情。 在我的工作表中,列“M”有一些以字母“T”开头的值我想为这些选择整行。提前谢谢。
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then Rows("i:i").Select
Next i
End Sub
答案 0 :(得分:2)
回答问题的一种可能方式:
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
Dim SelectedRows As Range
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Not SelectedRows Is Nothing Then
If Left(Range("M" & i).Value, 1) = trace Then Set SelectedRows = Union(SelectedRows, Rows(i))
Else
If Left(Range("M" & i).Value, 1) = trace Then Set SelectedRows = Rows(i)
End If
Next i
SelectedRows.Select 'Replace with .Copy if that's what you really wanted.
End Sub
答案 1 :(得分:1)
如果您尝试选择分配给变量" i"的行,您可以使用:
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then Rows(i).Select
Next i
End Sub
答案 2 :(得分:1)
"行(" I:")"不会工作。尝试收集一个字符串中所有范围的所有地址,然后选择字符串。请注意用于分隔每个范围的逗号。
Sub trace1()
Dim sRange As String
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then sRange = sRange & "," & i & ":" & i
Next i
Range(Mid(sRange, 2)).Select
End Sub
答案 3 :(得分:1)
使用AutoFilter
为您提供范围或实际地址
避免慢速循环
Sub trace2()
Dim strTrace As String
Dim strAddress
Dim rng1 As Range
strTrace = "T"
Set rng1 = Range([m1], Cells(Rows.Count, "M").End(xlUp))
With rng1
.AutoFilter 1, strTrace & "*"
Set rng1 = rng1.Cells(1).Offset(1, 0).Resize(rng1.Rows.Count - 1, 1)
strAddress = rng1.SpecialCells(xlVisible).EntireRow.Address
End With
MsgBox "rows that start with " & strTrace & vbNewLine & strAddress
ActiveSheet.AutoFilterMode = False
End Sub
答案 4 :(得分:0)
表达式
Rows("i:i").Select
抛出错误 - Rows()不会将文本值“i:i”识别为参数。
Rows(i).Select
会奏效。但除了最后一行应该在代码完成运行时选择时,它不会做任何你能看到的东西。在进入Next i
步骤之前,您可能希望在代码的下一步中对“T”行执行任何操作。
编辑: 好的,您希望在代码完成时选择多行。这可以做到:
Dim RowsDescript As String
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then RowsDescript = RowsDescript & i & ":" & i & ","
Next i
If Len(RowsDescript) > 0 Then
RowsDescript = Left(RowsDescript, Len(RowsDescript) - 1) ' removes the last comma
Range(RowsDescript).Select
End If
您最终想要的是一个如下所示的表达式:
Range("9:9,12:12,16:16").Select
如何到达那里,当一行被识别为具有您想要的“T”时,再次添加行号,冒号和行号以及逗号到字符串RowDescript。所以在循环结束时,你最终会得到
的字符串9:9,12:12,16:16,
在里面。但是我们需要去除最后一个逗号,所以检查一个非零长度的字符串,删除最后一个字符,然后选择那些行。
答案 5 :(得分:0)
1.“T”不等于“t”,删除“区分大小写”需要使用LCase或UCase(小写或大写)
2.Rows(“i:i”)由Row(i)替换
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If UCase(Left(Range("M" & i).Value, 1)) = UCase(trace) Then Rows(i).Select
Next i
End Sub
还有一条评论,在最后将只选择范围的最后一行,例如第1,5和10行将从“T”开始,所以最后将只选择第10行
这将允许您选择从“t”或“T”开始的行,但此方法允许选择不超过45行。
Sub Macro1()
Dim trace$, LR&, i&, Rng$
trace = "T": Rng = ""
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If UCase(Left(Range("M" & i).Value, 1)) = UCase(trace) Then Rng = Rng & i & ":" & i & ","
Next i
Rng = Left(Rng, Len(Rng) - 1)
Range(Rng).Select
End Sub
如果需要将行从一个工作表复制到另一个工作表,或者从第一行到第一行选择范围,其中单元格值从“T”开始
,请逐行复制/粘贴