Consulting

Results 1 to 5 of 5

Thread: VBA Cell value mutiplication

  1. #1
    VBAX Regular
    Joined
    Mar 2008
    Posts
    78
    Location

    VBA Cell value mutiplication

    The code i have works but make the sheet messy, My goal is to convert all values in a column (except the header row) from a millimeter value to an inch value by multipling by .0393700787

    [VBA]
    Sub increase()
    For Each Cell In Range("J2:K100")

    Cell.Value = Cell.Value * 0.0393700787

    Next Cell

    End Sub
    [/VBA]

    I want it to skip blank cells and the the first row and leave 0's as blanks.

    Hope that makes sence,
    Thanks

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]For Each Cell In Range("J2:K100").SpecialCells(xlCellTypeConstants) [/VBA]
    will skip the formulas and blanks and convert only the constant entries in your range. It will a) replace 0 with 0 and b) error if cell.value is a string.

    To deal with b) you could either test for IsNumeric(Cell.Value) before multiplying (which witll slow the routine v. slightly)
    or use [VBA]Cell.Value = Val(Cell.Value) * 0.0393700787 [/VBA], which will replace any non-numeric text values with 0.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    cell.Value = Evaluate("Convert(" & cell.Address & ",""m"",""in"")/1000")
    [/vba]
    ____________________________________________
    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

  4. #4
    VBAX Regular
    Joined
    Mar 2008
    Posts
    78
    Location
    I inserted the code from xld into my sheet but i may not have done it right,

    [VBA]
    Sub increase()
    For Each Cell In Range("J2:J10000,K2:10000")

    Cell.Value = Evaluate("Convert(" & Cell.Address & ",""m"",""in"")/1000")

    Next Cell

    End Sub
    [/VBA]

    Its not working.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do you really want to go that far down the rows?

    [vba]

    Sub increase()
    Dim Cell As Range

    For Each Cell In Range("J2:K10000").SpecialCells(xlCellTypeConstants)

    Cell.Value = Evaluate("Convert(" & Cell.Address & ",""m"",""in"")/1000")

    Next Cell

    End Sub
    [/vba]
    ____________________________________________
    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

Posting Permissions

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