简体   繁体   English

Excel 2007 VBA可选择单元格范围

[英]Excel 2007 VBA to select range of cells

I am attempting to select a range of cells. 我试图选择一系列细胞。 I have done this before but am having trouble with the syntax. 我以前做过这个,但是语法有问题。

Sub ChgDateX()

Range("A41").Select

Do
    If ActiveCell.Value = "Last Updated" Then
    mydate = ActiveCell.Offset(-40, 0).Value

    'Cells(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 9)).Select
    ActiveCell.Offset(0, 1).Value = mydate
    ActiveCell.Offset(0, 2).Value = mydate
    ActiveCell.Offset(0, 3).Value = mydate
    ActiveCell.Offset(0, 4).Value = mydate
    ActiveCell.Offset(0, 5).Value = mydate
    ActiveCell.Offset(0, 6).Value = mydate
    ActiveCell.Offset(0, 7).Value = mydate
    ActiveCell.Offset(0, 8).Value = mydate
    ActiveCell.Offset(0, 9).Value = mydate

    ActiveCell.EntireRow.Select
    Selection.NumberFormat = "m/d/yyyy"
    ActiveCell.Offset(1, 0).Select
        Else: ActiveCell.Offset(1, 0).Select
    End If
Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""

End Sub

I am trying to get away from the individual offset = mydate type coding. 我试图摆脱个别offset = mydate类型编码。

Couple of things: 几件事:

This code: 这段代码:

Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""

Doesn't work as expected because the correct operator you're looking for is And and not & 不能按预期工作,因为您正在寻找的正确的运算符是And而不是&

You don't have to "Select" anything. 您不必“选择”任何内容。 You can just put a reference to a cell in a variable (see my code). 您可以在变量中添加对单元格的引用(请参阅我的代码)。

Also, since you are always moving down to the next cell in the loop, you can put that outside of the IF statement. 此外,由于您总是向下移动到循环中的下一个单元格,因此可以将其放在IF语句之外。

Based on your code I think you're looking for something like this: 根据你的代码,我认为你正在寻找这样的东西:

Option Explicit

Sub test()
    Dim r As Range
    Dim myDate As Date

    Set r = Range("A41")

    Do
        If (r.Value = "Last Updated") Then
            myDate = r.Offset(-40, 0).Value

            With Range(r.Offset(0, 1), r.Offset(0, 9))
                .Value = myDate
                .NumberFormat = "m/d/yyyy"
            End With
        End If

        Set r = r.Offset(1, 0)
    Loop Until r.Value = vbNullString And r.Offset(-3, 0).Value = ""

End Sub

尝试这个:

ActiveCell.Range("B1:J1").Value = MyDate

How about this ... with 1 note. 这个怎么样......有1个音符。 I wouldn't advise the looping like this. 我不会像这样建议循环。 Perhaps you can filter on Column A <> "" or something, then just loop through the visible cells? 也许你可以过滤Column A <> ""或其他东西,然后只是循环通过可见细胞? Hard to say without knowing what you are doing. 很难说不知道你在做什么。

Option Explicit

Sub ChgDateX()

Range("A41").Select

Do
    If ActiveCell.Value = "Last Updated" Then

        Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 9)).Value = ActiveCell.Offset(-40, 0).Value

        ActiveCell.EntireRow.NumberFormat = "m/d/yyyy"

    End If

    ActiveCell.Offset(1).Select

Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM