+ Reply to Thread
Results 1 to 5 of 5

Excel VBA working with inactive Sheets

  1. #1
    Registered User
    Join Date
    11-07-2014
    Location
    International Falls, MN
    MS-Off Ver
    2010
    Posts
    3

    Arrow Excel VBA working with inactive Sheets

    I am trying to create a macro to graph some data I have collected, by making a bunch of smaller macros to build up to this. One of the macros I am trying to create is as follows:
    I have data from rows 5 to 370 in columns B to M. The macro I have written (as seen below) loops from row 5 to 370 and calculates an average of the data that it finds, I then want it to display that number in row 4; this will repeat all the way from column B to column M
    The Problem: I have two sheets in this: “Turbidity” and “Color;” if “Turbidity” is open, it will display the result with no problem but it won’t work for “Color” and vice versa. I also get a pop up that says: Error1004 application-defined or object-defined error. I click “debug” and it highlights the row that starts with “Set Data” I can’t figure out what the problem is here. Any suggestions?????


    Dim Turbdata As Range
    Dim x As Integer
    For x = 2 To 13
    Set Turbdata = Worksheets("Turbidity").Range(Cells(5, x), Cells(370, x))
    Worksheets("Turbidity").Cells(4, x).Value = WorksheetFunction.average(Turbdata)
    Next x

    ' averages for color
    Dim Coldata As Range
    For x = 2 To 13
    Set Coldata = Worksheets("Color").Range(Cells(5, x), Cells(370, x))
    Worksheets("Color").Cells(4, x).Value = WorksheetFunction.average(Coldata)
    Next x

  2. #2
    Registered User
    Join Date
    11-07-2014
    Location
    International Falls, MN
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel VBA working with inactive Sheets

    Basically, why does this code work for only one sheet and not both?

  3. #3
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Excel VBA working with inactive Sheets

    Try adding 'application.screenupdating = false' at the beginning of your macro. Then, before you try to change a cell's value on sheet "Color", you need to make that the active sheet. So, before "Worksheets("Color").cells(4,x).value =... add 'Sheets("Color").activate'. Then add 'application.screenupdating = true as the last line before the sub exits. You can test this theory by activating sheet "Color" and running the macro from there. You should now be getting an error for sheet "Turbidity".

    Greg
    Just a guy trying to make work stuff easier.

  4. #4
    Registered User
    Join Date
    11-07-2014
    Location
    International Falls, MN
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel VBA working with inactive Sheets

    That worked out brilliant! Thanks man!

  5. #5
    Registered User
    Join Date
    04-12-2011
    Location
    Bay Lake, FL
    MS-Off Ver
    Excel 2016 / 365
    Posts
    66

    Re: Excel VBA working with inactive Sheets

    You shouldn't have to activate a sheet in order to write data to a cell on that sheet. You also can do this with only 1 For-Next statement.

    Please Login or Register  to view this content.

+ 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. working with MANY sheets in a single excel file
    By sami770 in forum Excel General
    Replies: 10
    Last Post: 09-19-2011, 03:08 AM
  2. Replies: 1
    Last Post: 10-29-2009, 08:23 PM
  3. Close excel if inactive
    By LAWDAWG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2008, 06:44 PM
  4. Working with sheets in Excel 2003
    By Americo in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-15-2006, 10:50 AM
  5. Addressing cells on inactive sheets
    By John Duffus in forum Excel General
    Replies: 4
    Last Post: 10-22-2005, 02:05 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