473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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!
Aug 2 '06 #1
3 4491
BSOB
77
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
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.Interior
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
add this to the front of your macro code for text color.

msgbox(selection.font.colorindex)
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
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...
699
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...
5
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",...
0
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
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...
17
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...
7
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...
11
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...
5
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...
6
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) ?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.