Consulting

Results 1 to 5 of 5

Thread: Setting values to a whole range at once

  1. #1

    Setting values to a whole range at once

    I'm doing (more or less) the following.
    [VBA]
    For Each rn In .Range(.Cells(1, 1), .Cells(3, 1))
    rn.Value = rn.Value - 1
    Next rn
    [/VBA]

    I wonder, however, if it's possible to perform such an assignment to all the range at once. I.e. - perform a drop of every cell's value by one.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is it the number of lines of code that botehr you, or the number of iterations?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Isn't this basically the same question you asked here? The answer is pretty much the same.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Quote Originally Posted by xld
    Is it the number of lines of code that botehr you, or the number of iterations?
    Lines of code, i guess. I was hoping for a neat way to express the almost exactly same operation on all cells in a given range. I can always go for-looping but some of the sytax of VB is so nice and humanly canonical. It makes one hope to see such solutions mroe and more.

  5. #5
    Quote Originally Posted by rory
    Isn't this basically the same question you asked here? The answer is pretty much the same.
    Do you mean something like this?
    [vba]
    With Range("B1:B10")
    .Formula = "=A1+1"
    .Value = .Value
    End With
    [/vba]
    I think i was hoping for something even shorter code-wise. Taking your suggestion, shutting up and moving along, hehe. Thanks.

    I'm always on a hunt for smoother coding lines. There should be a section with professionally written code so a newcommer could gaze in awe and learn the better sytax.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •