使用变量选择多行

时间:2014-05-14 23:40:42

标签: excel vba excel-vba

我有一个贯穿工作表的循环,并跟踪这个人被命名的作品" bill"然后记住那些行,我想把它们复制到下一张表。

我的问题是  '作品     '范围(" 525:525504:504502:502497:497496:496495:495494:494493:493492:492491:491478:478471:4​​71453:453450:450449:449448:448447:447446:446445:445444: 444443:443442:442441:441440:440439:439438:438437:437436:436427:427129:129&#34)选择

但是当我使用变量来创建行列表时,它不会选择行

Dim accountsArray(2, 2) As String

accountsArray(1, 0) = "test"
accountsArray(1, 1) = "bill"

Dim rngCounter As String
rngCounter = Chr(34)
For i = ActiveSheet.Cells(ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row, 1).Row To 2          Step -1

    'save rows with bill
    If InStr((ActiveSheet.Cells(i, 2).Text), accountsArray(1, 1)) <> 0 Then
        If Len(rngCounter) < 2 Then rngCounter = rngCounter & i & ":" & i Else rngCounter = rngCounter & "," & i & ":" & i
    End If
Next i

rngCounter = rngCounter & Chr(34)

'works
'Range("525:525,504:504,502:502,497:497,496:496,495:495,494:494,493:493,492:492,491:491,478:478,471:471,453:453,450:450,449:449,448:448,447:447,446:446,445:445,444:444,443:443,442:442,441:441,440:440,439:439,438:438,437:437,436:436,427:427,129:129").Select

'does not work
Range(rngCounter).Select

Selection.Copy
Sheets.Add after:=ActiveSheet
ActiveSheet.Paste

1 个答案:

答案 0 :(得分:2)

当前编写代码的方式您有效地称之为:

Range("""525:525,504:504,502:502,497:497,496:496,495:495,494:494,493:493,492:492,491:491,478:478,471:471,453:453,450:450,449:449,448:448,447:447,446:446,445:445,444:444,443:443,442:442,441:441,440:440,439:439,438:438,437:437,436:436,427:427,129:129""").Select

所以请取出要添加到字符串开头和结尾的Chr(34)(双引号),它就能正常工作。