Shading selected cell with a certain colour at key press in Excel - excel

I'm a student and I have to learn a lot of stuff/questions, so I came up with an idea and I don't know how to realize it.
So all I am asking is for someone kind to show me how I can program Excel to specifically shade the selected cell with a certain color with a press of a button, as the title suggests.
I want to get in a special mode, where:
I can move around the cells with the arrow keys
by pressing 1 or 2 or 3 or 4, then the selected cell gets shaded with a specific color. (each number for a different color)
The question is, how do I do it? Is something like that possible at all in Excel? I have some background with code, but not even close to a professional. I just did VB Stuff for my GCE A Levels in Computing.
Macros are also possible as I can see but really don't know how to use them.
So here I am, asking this question, hoping someone kind will maybe see it as a challenge and help me realize it. I would invest the time to learn how to do this myself, but with the free time I have now, something like that would take months, just because I'm thorough and I want to learn from the absolute basics.

To enter this special mode you can run a macro to assign new purposes to those keys.
In a module in the VBE Editor, you can add:
Sub EnterSpecialMode()
Call Application.OnKey("1", "color1")
Call Application.OnKey("2", "color2")
Call Application.OnKey("3", "color3")
Call Application.OnKey("4", "color4")
End Sub
Sub ExitSpecialMode()
Call Application.OnKey("1")
Call Application.OnKey("2")
Call Application.OnKey("3")
Call Application.OnKey("4")
End Sub
Sub color1()
Selection.Interior.Color = RGB(255, 0, 0)
End Sub
Sub color2()
Selection.Interior.Color = RGB(0, 255, 0)
End Sub
Sub color3()
Selection.Interior.Color = RGB(0, 0, 255)
End Sub
Sub color4()
Selection.Interior.Color = RGB(255, 255, 0)
End Sub
Self-explanatory, but to enter the special mode you have to run the EnterSpecialMode. And to exit run the ExitSpecialMode sub

Related

Change Font from return value of UDF in VBA using Range.Characters property

( old question )
Hello everyone,
I've written a user defined function and want to change the font format of a defined character range, of the return value.
It doesn´t seem to work the way i expekt, for cells with functions "= ..." . I only got 2 scenarios, first formated the whole return value and second doesn't format anything. For "normal" cells, is works, as you can see in the screenshot.
try to change font format of first char to purple: top cell with function, botton cell without function
Anyone have an idea, how to do it correctly?
Iam thankful about every answer / hint i can get! Greetings Leo
Edit 03.12.2020: Thank you for all your answers and hints, some of you asked for code, here it is:
Public Function format(val As String)
With Application.Caller
.Characters(1, 1).Font.Color = RGB(255, 0, 255)
End With
With Application.Caller.Offset(1, 0)
.Characters(1, 1).Font.Color = RGB(255, 0, 255)
End With
format = val
End Function
So if you type some chars manually under the "function cell", for example "abcd", you should get following result (screenshot) [top before running function, bottom after running function][2]
Greetings Leo 1: https://i.stack.imgur.com/kh9AG.png [2]: https://i.stack.imgur.com/mGIXD.png

Is it possible to address a shape with VBA code?

I am replacing all my ActiveX controls with Excel shapes, because of this well know problem. Therefore I replaced each ActiveX Button with a rectangular shape assigning a macro to each shape:
My question is if I can address those 'shape buttons' with my vba code. Something simple like change the backgroung color of the "Review Start" button should be possible, right?
I'm thinking of something like:
Activesheet.shapes("Review Start").background.colorindex = 1
(This code is obviously not working)
One way is this. Assign a variable to the shape and then you can access its properties and methods easily. I'm not sure there's a way without using RGB.
By declaring the variable as Shape type, Intellisense will show you the properties and methods. Also you can use the Object Browser (F2).
Sub x()
Dim s As Shape
Set s = ActiveSheet.Shapes("Review Start") 'better to specify a sheet name
With s
.Fill.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Font.Color = vbBlack
.TextFrame.Characters.Text = "Fred"
End With
End Sub

VBA: Change the Background of a Created Button

Hello Developers and other, :)
I'm developing a macro to create a lot of button automatically, and I wants to give them a specific background color.
With Worksheets("Sheet 1")
Set r_BtnRange = .Range("O3:O4")
For Each r_Cell In r_BtnRange.Cells
Set b_Btn = .Buttons.Add(r_Cell.Left, r_Cell.Top, _
r_Cell.Width, r_Cell.Height)
With b_Btn
.Caption = "Email End-User"
.OnAction = "Email_EndUser"
.Font.Color = RGB(255, 255, 255)
'.BackColor = 16711680
End With
Next
End With
The .Button.Add return a Button Object, witch has no BackColor Proprety (according to this: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.button?view=excel-pia and the fact I receive an error when tying to uncomment the line)
please see Here
I could use a Shape to do it, but I would know if there is really any method to do it (btw, had to disable it and modify multiple propriety of it.
Thanks a lot for your answers,
Antoine

Lock Select Box Size In Excel

This seems like a simple thing, but I cannot figure it out, or find it online.
If I select 5 cells in a column(say A1:A5), and I would like to move this selection shape(column 1:5) over (to B1:B5); Is there shortcut to do this? Currently I hit the left arrow, and the select box changes size to just B1, and I have to hit shift and select B2:B5. Ideally I would like to discover a hot key that "locks" the shape of the select box.
It has been suggested by colleagues to write a macro, but this is ineffective in many cases. For example what if instead of a column I wanted to do the same thing with a row, or with a different sized shape. It seems likely that excel has this feature built in.
I'm not sure how a macro would be ineffective. I would write procedures similar to what's below, then assign them to hotkeys. Let me know if it works for you.
Option Explicit
Public rowDim As Long
Public colDim As Long
Public putSel as Boolean
Sub togglePutSel()
putSel = Not putSel
End Sub
Sub GetSelShape()
rowDim = Selection.Rows.Count
colDim = Selection.Columns.Count
putSel = True
End Sub
Sub PutSelShape()
Selection.Resize(rowDim, colDim).Select
End Sub
If you want to make it work for whenever you hit the arrow keys, then in your Sheet code, you can use this. You may want to do a quick check that rowDim and colDim aren't 0. The only issue with this is that you'd be stuck with that behavior unless you create a trigger to stop calling PutSelShape. So, I'd suggest one macro (hotkeyed to GetSelShape) to toggle it, and another hotkey for togglePutSel.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If putSel Then
Call PutSelShape
End If
End Sub

Can't close userform

Let me set up the environment.
This is VBA code running in Excel.
I have a userform that contains a msflexgrid. This flexgrid shows a list of customers and the customer', salesperson, csr, mfg rep, and territories, assignments. When you click in a column, let's say under the Territory column, another userform opens to show a list of Territories. You then click on the territory of your choice, the userform disappears and the new territory takes the place of the old territory.
This all works great until you click on the territory of your choice the 'Territory' userform does not disappear (it flickers) and the new territory does not transfer the underlying userform.
I should mention that when I'm stepping through the code it works great.
I'm assuming it has something do to with the flexgrid as all the other userform (that don't have flexgrids) that open userform work just fine.
Following is the some code sample:
** Click event from flexgrid that shows Territory userform and assignment of new territory when territory userform is closed.
Private Sub FlexGrid_Customers_Click()
With FlexGrid_Customers
Select Case .Col
Case 0
Case 2
Case 4
Case 6
UserForm_Territories.Show
Case Else
End Select
If Len(Trim(Misc1)) > 0 Then
.TextMatrix(.Row, .Col) = Trim(Misc1)
.TextMatrix(.Row, .Col + 1) = Trim(Misc2)
End If
End With
End Sub
** The following Subs are used in the Territory userform
Private Sub UserForm_Activate()
Misc1 = ""
Misc2 = ""
ListBox_Territory.Clear
Module_Get.Territories
End Sub
Private Sub UserForm_Terminate()
Set UserForm_Territories = Nothing
End Sub
Private Sub ListBox_Territory_Click()
With ListBox_Territory
Misc1 = Trim(.List(.ListIndex, 0))
Misc2 = Trim(.List(.ListIndex, 1))
End With
Hide
UserForm_Terminate
End Sub
I know this a long winded explanation but I'm a fairly decent VBA programmer and this has me stumped.
Any help would be greatly appreciated.
I'm not going to say what you're doing is wrong (in that it won't ever work), but it scares the heck out of me. This is not the way I'd deal with forms.
Firstly, you're using UserForm_Territories (the class/form name) to refer to an implicitly-created instance of the form. This is something I've always avoided doing. I would always create an instance of a form explicitly, so instead of:
UserForm_Territories.Show
I would do:
Dim oTerritoriesForm As UserForm_Territories
Set oTerritoriesForm = New UserForm_Territories
oTerritoriesForm.Show vbModal
' get the values from the form here
Unload oTerritoriesForm
Next, and much more worryingly, you're subverting the UserForm_Terminate behaviour by calling it explicitly. Why you're doing this I can't imagine, unless you thought that it would work around your stated problem. My advice: don't do that.
Worse, you're attempting to assign to the implicitly-created instance of the form within that Terminate method. You shouldn't be doing that, either. I'm surprised that even compiles.
It seems like you're trying to force the implicitly-created instance of the form to mimic an explicitly-created one. In which case, create it explicitly, as shown above.

Resources