Consulting

Results 1 to 14 of 14

Thread: Advice how to edit values in non-contig. range

  1. #1

    Advice how to edit values in non-contig. range

    I want to change all values that are not 0:00 to 0:00. The range is R2:BS72. From the graphic you will see it is every third column and two rows, skip row, 2 rows, etc...
    I would like advice how to approach this, either a search and find or a loop through each.

    thanks

    YLP
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Looking for a reset of the sheet....like for starting over..?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    exactly
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Yelp,
    This will get you started....you will have to add the other columns to the union(Range........but it works for me. Not sure if there is a better way.
    [vba]Sub a()
    Dim CurCell As Object
    For Each CurCell In Union(Range("R2:R72"), Range("U:U"))
    If CurCell.Value > 0 Then CurCell.Value = "0:00"
    Next
    End Sub[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    doh....more precicely:
    [VBA]Sub a()
    Dim CurCell As Object
    For Each CurCell In Union(Range("R2:R72"), Range("U2:U72"))
    If CurCell.Value > 0 Then CurCell.Value = "0:00"
    Next
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Steve,
    Worked beautifully, :-) , thanks.
    But if you don't mind...
    Why Union?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it isn't contiguous.

  8. #8
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    You can also omit union and just use the range object for non-contiguous ranges...
    [VBA]
    Option Explicit
    '
    Sub a()
    Dim CurCell As Object
    For Each CurCell In Range("R2:R72, U2:U72")
    If CurCell.Value > 0 Then CurCell.Value = "0:00"
    Next
    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    Instead of naming each range, since it is a consistent structure, is there some way to to qualify an offset, x number of times if R2:R72 is the first range and it has an offset of 3 columns and if it needs to repeat 20 times?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If your bracketed figures are not numerical constants, you could try
    [VBA]Range("R2:BS72").SpecialCells(xlCellTypeConstants, 1).Value = "0:00"[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    MD,
    Could you elaborate or explain what you mean by bracketed figures or numerical constants please?

    BTW: Your solution was dead-on, I tried it after posting. I would like to understand the solution....

    Thanks,

    Doug

    Thanks also Steve and Johnske, these two solutions helped not only w/ the particular result but another little piece to the pie....
    Last edited by YellowLabPro; 05-04-2007 at 01:20 AM. Reason: Update Remark
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Doug
    In a speadsheet press Function Key F5, Click Special, and select Constants. You'll see the available choices.
    Re brackets, your screenshot shows (1), (2) etc. These could be custom formatted numbers, which would be changed by the SpecialCells method, or Text, which would not.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Thanks MD,
    That is really helpful. And really dumb luck on my part, the bracketed values are text, not sure why I set those up as text as I set this up a couple of years ago. Messing w/ the specialcells also helps uncover more excel knowledge, thanks.
    There is still a lingering question, Steve's, Johnske's and your solution definitely work. But I see a pattern in my worksheet and if I what I see and if it is possible, this could have other uses-

    The names going across in row one, which begin in Q,T,W, Z... which are every other two rows, are constants. These names are repeated in Column A, beginning in A2 and contiguous, down. I changed them from constants a while back to =Q1, =T1, =W1, etc...
    But I wonder if possible w/ the names in (A) could be found by a formula using offset.
    This is not correct, but the idea I have is something like this-
    =OFFSET(Q$1,0,0) and the cell reference would need to be relative and somehow be instructed to increase by 2, or the offset reference would need to increase by two columns.
    I might be overstretching the boundaries here, but at least like to explore it. Thanks for the help.

    Doug
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Another way to do it would be to do something like this. I try to stay away from VBA loops, just for perfomrance reasons, since this is VBA/Excel and not a stand-alone EXE. I figure that if Bill Gate$ was nice enought to put all that power in Excel, I might as well use it.

    This seems to work since your data / spreadsheet format was very regular and consistant. It just does some Range intersections to select the data that you want to make 0:00.

    Several lines are in for debug/validation, and several could be conbined

    HIH, Paul


    [vba]
    Sub aaa()
    Dim rAll As Range
    Dim rBlankRow As Range
    Dim rFinal As Range

    Set rAll = Worksheets("Sheet1").Range("A1").CurrentRegion
    Set rBlankRow = Intersect(rAll, rAll.Columns(1).SpecialCells(xlCellTypeBlanks)).EntireRow
    Set rFinal = rBlankRow.SpecialCells(xlCellTypeConstants)

    rFinal.Select
    MsgBox Selection.Address
    rFinal.Value = "0:00"
    End Sub

    [/vba]

Posting Permissions

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