+ Reply to Thread
Results 1 to 4 of 4

Multiple cell address inputs to a macro

  1. #1
    Registered User
    Join Date
    01-22-2007
    Location
    Melbourne, Florida
    Posts
    2

    Multiple cell address inputs to a macro

    Hi,

    First post! I'm new to Excel VBA and have created a simple macro using a checkbox that will color a selected range within a row. However, the macro requires a specific address range to change the color of the cells. Is there a to cause the coloring effect on other address ranges using other checkboxes without having to duplicate the macro and changing the address range to another specific address range? If I end up having several hundred rows, each served by a checkbox, do I have to have a separate macro for each checkbox? Seems to me that I should be able to designate a checkbox to control a specific range using a single macro that can accept different ranges. That is, if I click on a specific checkbox, the macro should be able to determine which address range to color.

    Below is the macro, but is for a single address range:

    Sub Set_Row_color()
    '
    If Range("B5") = "True" Then //here the cell is selected for the
    TRUE/FALSE
    Range("A5:F5").Select //This is range to color
    With Selection.Interior
    .ColorIndex = 14
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    End With

    Else
    Range("A5:I5").Select
    Selection.Interior.ColorIndex = xlNone //this removes the color when
    the checkbox is unchecked.

    End If

    End Sub

    If I want to place another checkbox on the next row, how can I use this same macro without having to rewrite it using B6 for the T/F of checkbox and
    A6:F6 for colored range/

    Thanks,

    Dave Rogers
    Last edited by chanteur; 01-22-2007 at 06:06 PM. Reason: adjust spelling

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Dave

    this macro uses the worksheet change event to trigger the macro.
    When you select your checkbox and it updates the entry in column B this macro will change cell colours as required.

    The macro goes in the Worksheet module sheet not the normal module sheet

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-22-2007
    Location
    Melbourne, Florida
    Posts
    2
    Thanks for responding, Mudraker. I'm sorry though, I don't know the difference between a worksheet module sheet and a normal module sheet. When I select a sheet from the tree and right click, I can insert a module, but it doesn't show up under the sheet in the tree. It shows up and an additional project module. Can you tell me the difference and how to build a worksheet module sheet?

    Dave

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Dave

    A worksheet module is created automatically for every sheet that is in your workbook. There is also a workbook module

    From the Visual Basic Editor you should have a Project - VBA Project Window that lists your normal module sheets, forms etc. You will also have kistings of Sheet1 (Your Sheet Name As Seen In Excel) - This is the Worksheet Module. -
    Use Ctrl + R to open Project Window or View Menu

    Another way to get to the worksheet module is from Excel >Right Click on Sheet Name Tab > Select View Code.

    The workbook and Worksheet modules allow for macros that are triggered to run automatically by events occuring in the workbook or worksheet.

    To see these event trigers from the Worksheet module change the Left drop down list from General to Worksheet - The Right Dropdown list will then have a list of available Trigger events.
    For worksheets the events only effect the one sheet that the macro is on.

    You can not call a macro on a Workbook or Worksheet Module without using the full module sheet name unless the calling macro is on the same sheet.

    The Private Sub Worksheet_Change macro needs to go on the Worksheet Module for the sheet that has all your check boxes.

    When you Check or Uncheck a check box that updates rows in Column B triggers the Worksheet Change macro.

    This macro checks if the change was to a cell in column B if it is it then sets cell colur based on if it has a true value or some other value.

    It is possible to have code within the maco that updates the sheet that will cause the macro to re triggered which is why used Application.EnableEvents = False. If the macro stops running before it gets to Application.EnableEvents = True no future event changes will trigger the macro.

    If this is occuring place Application.EnableEvents = False in another macro on the same sheet and run it manually

+ 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