+ Reply to Thread
Results 1 to 5 of 5

Method `Range of object_'Worksheet' failed - Trying to write variable to cell

Hybrid View

  1. #1
    Registered User
    Join Date
    11-13-2013
    Location
    Aberdeenshire, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Method `Range of object_'Worksheet' failed - Trying to write variable to cell

    Hi All - Hoping someone can assist.

    I am trying to update a spreadsheet I have used for carrying out a wall thickness calculation on piping, and thought it would be a great idea to try and learn VBA at the same time. I have managed to put together some code that generally does what I need (basically finding values in a table of data and interpolating to get intermediate figures) but I am getting an error trying to write the result to a cell.

    Error:
    Run-time error '-2147417848 (80010108)':
    Method `Range of object_'Worksheet' failed

    I then get `Not Enough System Resources to Display Completely'

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'Define General Variables
        
        Dim SIZE
        Dim CLASS
        Dim MATERIAL
        Dim PIPETYPE
        Dim GROUP
        Dim SCH
        Dim PRES_D
        Dim TEMP_DU
        
        Dim TEMP_DL
        Dim MATFAM
        Dim GROUP_PT
    
    'Define Pressure/Temperature Calculation Variables
    
        Dim TEMP_PTL
        Dim TEMP_PTU
        Dim PRES_PTL
        Dim PRES_PTU
        Dim PRES_PTDIFF
        Dim TEMP_PTDIFF
        Dim RATIO_PT
        Dim TEMP_INC 'Temperature Increase above TEMP_PTL
            
    'Define Coordinate Calculation Variables
    
        Dim ROW_S
        Dim COL_S1
        Dim COL_S2
        Dim ROW_PT
        Dim COL_PT1
        Dim COL_PT2
    
    '   Set-up Known Variables
        
        CLASS = Worksheets("ASME B31.3 CALCULATION").Cells(4, "D").Value
        GROUP = Worksheets("ASME B31.3 CALCULATION").Cells(14, "D").Value
        GROUP_PT = GROUP & " - " & CLASS
        
    With Application.WorksheetFunction
    
        ROW_PT = .Match(GROUP_PT, Worksheets("ASME B16.5 TABLES").Range("C3:C142"), 0) 'Determine Row Number for Group
        COL_PT1 = .Match(TEMP_DU, Worksheets("ASME B16.5 TABLES").Range("D2:AF2"), 1) ' Determine Column Number for Lower Temperature
        COL_PT2 = COL_PT1 + 1 ' Determine Column Number for Upper Temperature
    
        TEMP_PTL = .Index(Worksheets("ASME B16.5 TABLES").Range("D2:AF2"), 1, COL_PT1, 1) ' Determine Lower Temperature Value
        TEMP_PTU = .Index(Worksheets("ASME B16.5 TABLES").Range("D2:AF2"), 1, COL_PT1 + 1, 1) ' Determine Lower Temperature Value
    
        PRES_PTU = .Index(Worksheets("ASME B16.5 TABLES").Range("D3:AF142"), ROW_PT, COL_PT1) 'Determine Lower Pressure Value
        PRES_PTL = .Index(Worksheets("ASME B16.5 TABLES").Range("D3:AF142"), ROW_PT, COL_PT2) ' Determine Upper Pressure Value
    
    End With
    
        PRES_PTDIFF = PRES_PTU - PRES_PTL 'Determine Pressure Differential
        TEMP_PTDIFF = TEMP_PTU - TEMP_PTL 'Determine Temperature Differential
    
        RATIO_PT = PRES_PTDIFF / TEMP_PTDIFF 'Increase in Pressure Per Deg C
    
        TEMP_INC = TEMP_DU - TEMP_PTL
        
        Sheets("ASME B31.3 CALCULATION").Range("M16") = (PRES_PTU - (RATIO_PT * TEMP_INC))
            
       
    End Sub
    Any help/pointer would be very much appreciated :-)

    Thanks

    Steve
    Last edited by stevemcd1; 12-17-2013 at 04:00 PM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Complete Newbie Need HELP!

    You got way too many unsued variable and you didn't actually define any of them. They are all set to variant as default. The big issue is that you are running this as a worksheet change event rather than a macro.

    Edit: Holding off on answer till you comply with mod's request.

  3. #3
    Registered User
    Join Date
    11-13-2013
    Location
    Aberdeenshire, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Method `Range of object_'Worksheet' failed - Trying to write variable to cell

    Apologies, Title updated :-)

    stnkynts, there are other calculations that I need to add once I fully understand this first one - the variables relate to future calculations so probably should have removed them for this exercise. :-) Regarding defining them, I am reading up on that now.

    I thought the event change would be the best was as I want it to update the calculations as I change the parameters via drop downs and manual entries (pressures, temperatures, materials etc).

    Thanks

    Steve

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Method `Range of object_'Worksheet' failed - Trying to write variable to cell

    One of the problems that you are going to run into is a constant loop of worksheet changes. Example: You enter a value in A1 which trigger your macro which put a value in M16. By putting the value in M16 it trigger the macro to occur again, and so on and so on. This can be disabled by setting Application.EnableEvents = False. The best way would be to code the worksheet change event to be range specific so that it only occurs on the change of certain drop downs and manual entries. That information was not provided.

  5. #5
    Registered User
    Join Date
    11-13-2013
    Location
    Aberdeenshire, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Method `Range of object_'Worksheet' failed - Trying to write variable to cell

    stnkynts, appreciate the responses :-)

    I have attached a copy of the file as it currently stands for additional information.

    Many thanks

    Steve

    00 - NEW WALL CALC - VBA.xlsm

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Using If Statement to format a bookmark (complete newbie)
    By kiwigal in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2013, 06:41 AM
  2. A complete newbie - please help with a password problem
    By alexisemma31 in forum Excel General
    Replies: 9
    Last Post: 08-02-2012, 02:36 AM
  3. Replies: 4
    Last Post: 06-13-2012, 01:34 AM
  4. complete macro newbie
    By savfam02 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2010, 09:14 PM
  5. If then:complete newbie
    By Cydney in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2009, 01:31 PM

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