473,748 Members | 9,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA Macro Question

2 New Member
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!
Aug 2 '06 #1
3 4504
BSOB
77 New Member
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?)
Aug 3 '06 #2
rgrandle
2 New Member
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.
Aug 3 '06 #3
BSOB
77 New Member
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.
Aug 7 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
3254
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a clarification on what 'macro' means. A Lisp macro is a way of modifying code when that code is first defined. It can rearrange the structure of the code, and add and remove parts of it. Unlike C's #define macro language, Lisp macros understand the...
699
34064
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
5
1905
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all", but I still would like to know because I am trying to encapsulate a third-party C-based callback-oriented gui toolkit inside C++ classes and I would like to try something. So say you have: long some_address = some_valid_address;
0
1214
by: Spiro Trikaliotis | last post by:
Hello, assume I define a macro like #if <somecondition> # define MACRO(_x, _y) _x #else # define MACRO(_x, _y) _y #endif
5
5335
by: Mason | last post by:
I'm having some problems converting VBA for Word 2000 to code that VB.Net understands. I recorded a macro in Word to add numbering (a. b. c.) to my paragraphs. I managed to translate quite a bit of it, but I'm having trouble with two pieces: ListGalleries - I can't find any way to make VB.Net understand this. When I do a mouseover, the error message it gives is, "Interface 'Word.ListGalleries' cannot be indexed because it has no...
17
7094
by: sounak | last post by:
How could we get a macro name from a macro value such that in a header file #define a 100 #define b 200 now the source file will be such that the user gives 100 then the value is outputted as a the user gives 200 then the value is outputted as b
7
23542
by: fei.liu | last post by:
#define ONCFILE_ERR1(funcname, name) \ { \ #ifdef DEBUG\ cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " << funcname << " " << name << endl; \ #endif \ } I want to have conditional macros inside a macro. When I compile this
11
1574
by: Remo | last post by:
Hi All I Have Quation About Macro's plz help why do we have # symbol before any macro line #define STOP 0 ... why con't we use the any other symbol like $, @,, plz answer if any one knows reason.
5
3566
by: krbyxtrm | last post by:
Hi is there a way to 'manage' function execution using macros? #define MY_CALL_MACRO(MacroName) { g_MacroStack.push_back(MacroName); <some code here...>} such that when i use the macro like this: MY_CALL_MACRO("MyMacroName")
6
2609
by: pedroalves | last post by:
Hi all, This is not a question about how to #define COMMA , Please keep reading. Recently in binutils, we introduced a macro like this: #define STRING_COMMA_LEN(STR) \ (STR), ((STR) ? sizeof (STR) - 1 : 0)
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.