VBA Macro Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rgrandle
    New Member
    • Aug 2006
    • 2

    #1

    VBA Macro Question

    I'm trying to figure out how to create a few handy macros for my job at work. I've done a little VBA programming before, but its been a few years since I've touched it, so I could really use some help. I'm looking to create a host of macros that will cycle through different formatting options. For example, I want to create a macro, link it to cntrl+shift+F that will modify the fill color the following way:

    cntrl+shift+F ---> when keyed the first time, background fill turns gray (25%)
    cntrl+shift+F ---> when keyed the second time, fill turns to light blue
    cntrl+shift+F ---> then light yellow
    cntrl+shift+F ---> then no fill
    cntrl+shift+F ---> then gray (25%) again, etc...looping through the options

    I want to do this for all sorts of formatting such as cycling through alignment options, text color, fill color, number format, borders, etc. I figure that if I can get some help on how the general format goes, I can go back and figure out the code for fill vs. text color vs. whatever.

    I also want to create macros that will increase/decrease the decimal place every time it’s keyed and zoom in/zoom out every time it’s keyed.

    Any help that you can give me would be great, and the sooner the better! Thanks!
  • BSOB
    New Member
    • Jul 2006
    • 77

    #2
    i hate to be the one to say this because youve offered tons of details... but "please be more specific". i understand your concepts but im not sure ill be of much help unless i know the application more.
    IF YOU OPERATING ENTIRELY INSIDE ONE VB PROGRAM then:
    you want a variable for each macro saying what the next option will produce. Each time you capture the event, you increase that variable by one:
    in the keydown event (or however you are capturing the keys)
    static FillStyle as byte
    select case fillstyle
    case 0
    'set background to gray
    fillstyle = 1
    case 1
    'set background light blue
    fillstyle = 2
    case 2
    'light yellow
    fillstyle =3
    case 3
    'no fill
    fillstyle =0 'note that it goes back to 0 so it will loop
    end select

    static FontColor as byte
    select case fontcolor...
    ...
    ...
    end select

    i think that helps on the rotation aspect, but that assumes that your program stays running so variables have proper scope and stay static. (static means the variable keeps it's value even after the sub is left and re-entered)

    as far as capturing the event of a key combination pressed... im not sure if you plan on having a visible form to work with or a background application macro sort of thing. i (and probably other people) wont have much to say until i (we) understand the gui (environment) you plan on using this in.

    the concept of a macro, as far as i know it, would be best acheived by using windows shortcut keys to execute your program. the program would run (invible) and then exit after doing it's job. inorder to hold the rotation point (where you are in your options so it can loop) for each macro job (if you plan on unloading your application) you would need to either store it in the registry or a text file. (do you need help with that if you are doing this sort of operation?)

    Comment

    • rgrandle
      New Member
      • Aug 2006
      • 2

      #3
      Thanks for the response, but I do think we misunderstood each other...The macros are only to be used in excel, only while the file is opened. I'm using the macros to save time in formatting different spreadsheets. Currently, I have:

      ----

      Sub Fill_Toggle()
      '
      ' Fill_Toggle Macro
      ' Macro recorded 8/2/2006 by rgrandle
      '
      ' Keyboard Shortcut: Ctrl+Shift+V
      '

      With Selection.Inter ior
      If .ColorIndex = xlNone Then
      .ColorIndex = 15 'gray 25%
      ElseIf .ColorIndex = 15 Then
      .ColorIndex = 37 'light blue
      ElseIf .ColorIndex = 37 Then
      .ColorIndex = 36 'light yellow
      ElseIf .ColorIndex = 36 Then
      .ColorIndex = xlNone 'no fill
      End If
      End With

      End Sub

      ----
      This macro successfully cycles through different fill colors when the keystroke cntrl+shift+v is pressed. If I want the background to be gray, I press it once. To be blue, I press it twice, etc. All the other macros should, in theory, be very similar to this. I'm running into some problems with the text color macro initiating:

      ----

      Sub Color_Toggle()
      '
      ' Color_Toggle Macro
      ' Macro recorded 8/2/2006 by rgrandle
      '
      ' Keyboard Shortcut: Ctrl+Shift+P
      '


      With Selection.Font
      If .ColorIndex = 1 Then
      .ColorIndex = 5 'blue
      ElseIf .ColorIndex = 5 Then
      .ColorIndex = 10 'green
      ElseIf .ColorIndex = 10 Then
      .ColorIndex = 3 'red
      ElseIf .ColorIndex = 3 Then
      .ColorIndex = 1 'black
      End If
      End With

      End Sub
      ----

      Any clues on why the font stays black? Thanks.

      Comment

      • BSOB
        New Member
        • Jul 2006
        • 77

        #4
        add this to the front of your macro code for text color.

        msgbox(selectio n.font.colorind ex)
        exit sub

        just make sure that 1 is black. if you get a number other than 1, then there is your problem. it might start with a number representing "default" istead of "black" which visually are the same, but in excel brain. other debugging tips. place brake point inside each of the if-statements. if it never breaks then you know the problem is in the testing, not the property setting.

        Comment

        Working...