Excel 2010 F2-Enter Macro Button - vba

I have a chart with two columns that count horizontally across the grid cells with a specific color fill. (=CountCellsByColor(##:##,#) There are 24 Rows in each column.
I now want to create a macro button in a particular cell so the user can click the button and the columns will auto update. Currently this is accomplished by clicking into a cell, pressing F2 then Enter, which is fine but if I can make it simpler all the better.
This is what I have, I created a commandButton from the Developer tab (Insert, Button); Then in the VBA screen the following code;
Sub RefreshCells()
Dim r As Range, rr As Range
Set rr = Selection
For Each r In rr
r.Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
DoEvents
Next
End Sub
I get a restricted use error that pops up after I click the button, it works once but that's it then locks the rest of the cells from editing. I have a number of VBA codes running on the sheet that allow users of the sheet to simply click certain cells to turn them different colors.

I was making this far too complicated! If I may attempt to answer my own question, here is what I did to solve this,
Under Developer Tab, I hit Record Macro Then F2 Enter followed by Stop Record and saved that function. Then I created a Button under the developer tab and assigned it that macro.
Sub Macro5()
'
' Macro5 Macro
'
'
Range("D33:D34").Select
ActiveCell.FormulaR1C1 = ""
Range("D35").Select
End Sub
It's probably not right but it seems to have the same effect of clicking into a cell, hitting F2 + Enter . The columns that don't have an automatic count update perfect, no errors, no bugs!

Related

MS Access Move visible selected cell to first cell of active table active row

I am trying to position the active/selected cell on the active record in an open MS Access table. No forms are involved - I am just looking at a normal table in DataSheet view. I need VBA to move the selected cell up, down, to the first cell on the record, and so on.
I have tried SendKeys {Up},{Home}, etc, but that doesn't move the selected cell.
Is there a VBA way to move the active/selected cell on an open table in DataSheet view mode? Thank you.
Sub MoveSelectedCell()
Dim tbl As AccessObject, tblname As String
tblname = Application.CurrentObjectName
' I need to move the selected cell that receives keyboard input.
' SendKeys does nothing
' I need something like Selection.Field = ActiveRecord.Field(0)
'
' I can't find Selection.Move like in Word/Excel VBA
' Is there a way to move the selected cell in Access? Thank you
' SendKeys "{Home}"
End Sub

Cancel alt + down arrow: remaining effect from VBA code

I have a pivot table with page field. I wrote a few lines of code under the Pivot Table update event to do something.
When the page field filter is used via the keyboard, the filter form is accessed with the shortcut alt + down arrow typed on a page field cell.
Code works fine and at end moves focus to a specific cell.
Problem is that alt + down arrow effect remains! The cell is shown with a drop down list displayed and the operator needs to hit the escape key to continue working. I try to avoid this by adding a sendkeys {esc} line after, but the sendkeys numlock bug is more annoying than having to directly hit the escape key.
My question is:
Is there another way to close, using vba code, the drop down list displayed in final cell?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Sheet1.ListObjects("precomanda").Resize Range(Sheet1.ListObjects("precomanda").HeaderRowRange, _
Sheet1.ListObjects("precomanda").HeaderRowRange.Offset(Sheet1.PivotTables("Istoric").PivotFields("Produs").DataRange.Count, 0))
Sheet1.Range("A5").Activate
Application.SendKeys ("{ESC}")
End Sub

excel: use keyboard to move column in data validation mode

Usually in excel, when we press the "->" in keyboard, the cell will move from "A1" to "B1". However, it does not work on a drop-down list.
I create this drop-down list by data validation. When I press "->", nothing happen. I have to use mouse to click it every times, it is trouble. Is there any way, or any key, to let me move to new cell?
Assuming you have your drop-down box on a worksheet, you are right that it doesn't work.
Although you may have lined the drop-down box with the cells around it, it is an object sitting over the cell, when you push -> (the right arrow?) or the tab button the selection will go to the next cell, which is under the drop-down box.
The only way to get focus onto the drop down box is to click on it, from there you can use up and down keys to change selection and enter to select an item. After selection focus is given back to the selected cell in the worksheet.
You have tagged this question as 'vba' so to that end what you could do (although it may be a little excessive) is
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Replace(Target.Address, "$", "") = "C2" Then
UserForm1.Show 1
End If
End Sub
The above code is placed with the required sheet, if the selection is equal to C2 then a UserForm opens, within that you could build the selection that are keyboard compatible so you don't need to reach for the mouse.

Excel macro to remove rows that have been added via another macro

I have recently added a macro to add a selection from Sheets("Activities") and insert it into a new row positioned at row 25 on Sheets("EP2"). - Thus adding new pre-written "activities" into the sheet.
There are a number of different selections (pre-written activities) from Sheets("Activities") that have been assigned their own buttons.
When the button is pressed, the appropriate selection from Sheets("Activities") is inserted into the same position (row 25 on Sheets("EP2"))
This means that each time another activity is added, the last addition moves down the page. This is all fine.
HOWEVER, I am wanting to add coinciding buttons to then remove certain activities (from Sheets("EP2") at a later date. this poses a problem because when a certain activity is added to the sheet it may not have the same cell reference as when it was 1st added via the macro. Is there a way around this?
Basically, this form has to be for people that have almost no understanding of excel whatsoever and needs to have these prewritten activities add-able at a click of a button and then removable at the click of a button. The code I am using is as follows..
Sub Insert_CV()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you sure you want to insert a CV agreement" & vbNewLine & "This cannot be undone.", vbOKCancel, "CV Agreement")
If Answer = vbOK Then
Sheets("Activities").Select
Rows("1:4").Select
Selection.Copy
Sheets("EP2").Select
Rows("24:24").Select
Range("C24").Activate
Selection.Insert Shift:=xlDown
Range("D20:U20").Select
End If
End Sub
Thanks for your help

How to reset Excel internal button count?

I have an an Excel sheet that uses VBA to generate some form control buttons on the fly.
the buttons are cleared and then new buttons are created.
I have noticed that even though old buttons are deleted Excel is keeping an internal register of each button. New buttons have button name of over 11K
I don't know if there is some sort of limit excel will allow for this and I don't want to run out of buttons.
I am not sure if this growing registry of buttons past is causing the file size to grow.
I would like to be able to reset the increment back to 0
Anyone have any idea how I can go back to button_0 ? (without starting a whole new Excel sheet)
Seems internal button count is sheet specific. Solution is to copy sheet, rename old sheet, then rename new sheet to old sheet name. Then delete old sheet. Viola! button count reset.
I found the answer somewhere in a forum, but I couldn't retrace it.
You could also pragmatically create the button by a function/sub-routine as a workaround.
Example: below function adds a button and limits the count to a fixed number (in my case, it is the total buttons available).
In my sheet, the first button is named "Button 687" (before I use the macro) and the second button is named "Button 2".
But, it is quite not dynamic when you want to add Drop Down or other form control etc. Macro recording helps you figure out the syntax, methods and properties of the form control you want to add though.
I am not sure why "buttons" is not listed in Properties/Methods after you typed "Activesheet." but Activesheet.Buttons(1).Name or Activesheet.buttons.add are valid codes.
Public Sub Add_Button(ButtonLabel$, ButtonSize#, BFontSize#, BFontName$)
Dim Button__ As Object
Dim One_Unit#: Per_Unit = Application.CentimetersToPoints(1)
'Creates a button at the sheet's first cell (row 1, col 1) with the height and width being 1cm.
Set Button__ = ActiveSheet.Buttons.Add(1, 1, One_Unit, One_Unit)
'button count will be restricted to the number set by user.
With Button__
.Name = "Button " & ActiveSheet.Buttons.Count
With .Characters
.Text = UCase(BLabel)
.Font.Name = BFontName
.Font.Size = BFontSize
End With
End With
End Sub
Sub AddAButton()
Add_Button BLabel:="SAVE"
Debug.Print ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name
End Sub