VBA Excel – select左值为variables的行

遇到这个代码的麻烦。 没有错误,但也似乎没有做任何事情。 在我的表中,列“M”有一些以字母“T”开头的值,我想为这些行select整行。 提前致谢。

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 

以书面forms回答这个问题的一种可能的方法:

 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 

如果您正在尝试select分配给variables“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 

“行(”我:我“)”将无法正常工作。 尝试收集一个string中的所有范围的所有地址,然后selectstring。 请注意分隔每个范围的逗号。

 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 

使用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 

expression方式

 Rows("i:i").Select 

抛出一个错误 – Rows()不会将文本值“i:i”识别为一个参数。

 Rows(i).Select 

将工作。 但它不会做任何你能看到的,除了最后一行应该在代码完成运行时select。 在进入下一步之前,您可能需要在代码中的Next i完成“T”行所需的任何操作。

编辑:好的,你想在代码完成时select多行。 这可以做到:

  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 

你想要结束的是一个expression式,看起来像这样:

 Range("9:9,12:12,16:16").Select 

你如何到达那里,当一行被确定为具有你想要的“T”时,将行号和一个冒号和行号再次添加一个逗号给RowDescriptstring。 所以在循环的最后,你会得到string

 9:9,12:12,16:16, 

在里面。 但是我们需要删除最后一个逗号,所以检查一个非零长度的string,删除最后一个字符,然后select这些行。

1.“T”不等于“t”,要删除“区分大小写”,需要使用LCase或UCase(小写或大写)

2.行(“我:我”)由行(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行

在评论中针对问题进行了更新

这将允许您select从“t”或“T”开始的行,但此方法允许select不超过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 

如果需要将行从一个表格复制到另一个表格,或者对范围进行sorting并select从第一行到最后一行的范围(单元格值从“T”开始),请使用复制/