+ Reply to Thread
Results 1 to 5 of 5

Syntax Correction: Combine text with variable

Hybrid View

cazooo Syntax Correction: Combine... 05-08-2009, 04:44 PM
shg Re: Syntax Correction 05-08-2009, 04:47 PM
shg Re: Syntax Correction:... 05-08-2009, 08:33 PM
cazooo Re: Syntax Correction:... 05-08-2009, 08:44 PM
shg Re: Syntax Correction:... 05-08-2009, 10:36 PM
  1. #1
    Registered User
    Join Date
    04-15-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Syntax Correction: Combine text with variable

    I have the following VBA code and try to sum the range from B2 to CY2 for DA2; then B3 to CY3 for DA3, etc. Therefore I use the for loop. It results in "Application defined or Object defined error".

    Could anybody help me to correct the syntax?

    -----------------------------------------------------------------------
    Sub CalculatePhi()
    For i = 2 To 103
        Cells(i, 104) = Application.WorksheetFunction.Sum(Range("B &i& ", "CY &i& ")) / 101
        Cells(104, i + 1) = Application.WorksheetFunction.Sum(Range(Cells(i + 1, 2), Cells(i + 1, 103))) / 101
    Next i
    End Sub
    Last edited by cazooo; 05-08-2009 at 08:46 PM. Reason: Put code fragment in code tag

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Syntax Correction

    Please read the forum rules, and then edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Syntax Correction: Combine text with variable

    Thanks.

    This may fix the first line; I'm not sure what you're tring to do with the second.
    Sub CalculatePhi()
        Dim i As Long
        
        For i = 2 To 103
            Cells(i, "DA") = WorksheetFunction.Sum(Cells(i, "B"), Cells(i, "CY")) / 101
            Cells(104, i + 1) = WorksheetFunction.Sum(Range(Cells(i + 1, 2), Cells(i + 1, 103))) / 101
        Next i
    End Sub

  4. #4
    Registered User
    Join Date
    04-15-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Syntax Correction: Combine text with variable

    Thank you, it's solved. Actually it should have Range in between to sum from cell B2 to cell CY2 and the result should be pasted to cell CZ2 as shown in the code.
    Sub CalculatePhi()
    For i = 2 To 103
        Cells(i, "CZ") = WorksheetFunction.Sum(Range(Cells(i, "B"), Cells(i, "CY"))) / 101
        Cells(104, i + 1) = Application.WorksheetFunction.Sum(Range(Cells(i + 1, 2), Cells(i + 1, 103))) / 101
    Next i
    End Sub

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Syntax Correction: Combine text with variable

    I question whether you don't want the average instead of the sum over a constant, which is subject to error.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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