vba 在vba问题中选择一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4597651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:03:07  来源:igfitidea点击:

selecting a row in vba problem

excelvba

提问by JOE SKEET

i am programmatically selecting a row in VBA but it is giving me a TYPE MISMATCH error on this:

我正在以编程方式在 VBA 中选择一行,但它给了我一个 TYPE MISMATCH 错误:

Rows(Str(i) & ":" & Str(i)).Select

what am i doing wrong?

我究竟做错了什么?

For i = 5 To 1000
    If Worksheets("5470").Cells(i, 2) = "" Then
        Rows(Str(i) & ":" & Str(i)).Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Delete Shift:=xlUp
        Exit For
    End If
Next i

回答by GolezTrol

Why not use

为什么不使用

Rows(i).Select

Afaik, rows can be indexed by the row number too.

Afaik,行也可以按行号索引。

Alternative:

选择:

Cells(i, 1).EntireRow.Select

回答by MarcelDevG

The error you are getting is because the STR functions prepends a space to the number. So, when i=100, you'll get " 100: 100". You could the method of GolezTrol, or use cstr() instead of str(). The space is prepended to account for a possible negative value.

您得到的错误是因为 STR 函数在数字前面加上了一个空格。所以,当 i=100 时,你会得到“100:100”。您可以使用 GolezTrol 的方法,或者使用 cstr() 而不是 str()。前面的空格是为了说明可能的负值。