+ Reply to Thread
Results 1 to 4 of 4

Range object/property problem.

  1. #1
    Registered User
    Join Date
    06-01-2009
    Location
    Seoul
    MS-Off Ver
    Excel 2003
    Posts
    4

    Range object/property problem.

    Hail Experts!

    I'm trying to write a code that calculates 2nd column based on 3rd column's number or vice versa based on the condition set on the 1st column.

    Below, there are two procedures. "SimpleCalc" and "SimpleCalc2".
    I first wrote SimpleCalc2, but it isn't working, so I worked around the error by writing SimpleCalc, which selects a cell and moves around by offset. I personally find it hard to read and error prone as I develop more logic around it.

    I'm trying to develop more function based on this code, so I need to make it neat and flexible.

    Can anyone tell me what I'm doing wrong in SimpleCalc2? Or do you have any suggestion to improve the code "SimpleCalc"?

    I'm using Excel 2003.

    Thanks!

    ---------------------

    Sub SimpleCalc()

    Dim SimpleMethodRng, SimpleMethod As String, i As Integer

    ActiveWorkbook.Worksheets("Dashboard").Range("P5").Select

    SimpleMethodRng = ActiveWorkbook.Worksheets("Dashboard").Range("N5:P12")

    For i = 1 To 8

    SimpleMethod = SimpleMethodRng(i, 1)

    If SimpleMethod = "per pyung" Then _
    Selection.Offset(i - 1, 0).Value = SimpleMethodRng(i, 2) * Range("GLA") _
    Else Selection.Offset(i - 1, -1).Value = SimpleMethodRng(i, 3) / Range("GLA")

    Next i

    End Sub


    Sub SimpleCalc2()

    Dim SimpleMethodRng As Range, SimpleMethod As String, i As Integer

    SimpleMethodRng = ActiveWorkbook.Worksheets("Dashboard").Range("N5:P12")

    For i = 1 To 8

    If SimpleMethodRng(i, 1) = "per pyung" Then _
    SimpleMethodRng(i, 3).Value = SimpleMethodRng(i, 2) * Range("GLA") _
    Else SimpleMethodRng(i, 2).Value = SimpleMethodRng(i, 3) / Range("GLA")

    Next i

    End Sub
    Last edited by mjsheen; 06-02-2009 at 03:28 AM. Reason: solved

  2. #2
    Registered User
    Join Date
    06-01-2009
    Location
    Seoul
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Range object/property problem.

    Solution to my own problem... I'm a newbie to VBA coding and I seem to have forgotten using "Set" for defining range. I'm not sure if "Set" is required for the range but that solved the problem. Anyways, better, refined code below.

    Sub CrossCalculation()

    Dim myRange As Range

    Set myRange = ActiveWorkbook.Worksheets("Dashboard").Range("N5:N12")

    For Each Cell In myRange

    Select Case Cell.Value

    Case "per pyung"
    Cell.Offset(0, 2) = Cell.Offset(0, 1) * Range("GLA")

    Case Else
    Cell.Offset(0, 1).Value = Cell.Offset(0, 2).Value / Range("GLA")

    End Select

    Next

    End Sub
    Last edited by mjsheen; 06-02-2009 at 03:28 AM. Reason: tag

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Range object/property problem.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  4. #4
    Registered User
    Join Date
    06-01-2009
    Location
    Seoul
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Range object/property problem.

    I'll keep that in mind

    Quote Originally Posted by DonkeyOte View Post
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1