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

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

Related

How to add specific number in last row from UserForm using VBA?

I am the beginner of VBA and need some help from you in code.
My problem is that I want to make some kind of UserForm using VBA in Excel to generate new numbers for new ticket.
The UserForm will be needed to manage tickets about received problems.
The case is that I want to make new number of ticket by clicking button "New Ticket". After that, in UserForm, the "Ticket Number" field will be blocked and show assigned new ticket number, "Date Opened" field will show current date automatically and "Accept" button will change text to "Create" button and will be enabled. After clicking "Create" button, UserForm will close and write line about ticket with details in last row in Excel sheet.
I also want to have ticket number starting with specific number e.g. 60555, 60556, 60557 etc. The problem is that now I am receiving numbers 1, 2, 3 etc.
Below you can see some screenshots of problem and part of created code.
UserForm "Manage Tickets"
After "Create" button, what is showing in last row in Excel Sheet
What I want to show in last row in Excel Sheet after "Create" button
If there is a way also to show text "[BHD#?????]" in UserForm and add it by UserForm, it will be very helpful for me!
Part of code which is responsible for "New Ticket" button:
Private Sub btwNewTicket_Click()
ClearFields
LastRow = ThisWorkbook.Sheets("Issue Log").Cells(Rows.Count, 2).End(xlUp).End(xlUp).Row - 4
Me.tbTicketNo.Value = LastRow
Me.tbTicketNo.Locked = True
Me.tbDateOpen = Date
Me.btnAccept.Caption = "Create"
Me.btnAccept.Enabled = True
End Sub
Thanks in advance!
You can concatenate strings with the & symbol. Hence a possible solution for your needs would be:
Me.tbTicketNo.Value = "[BHD#" & Format(LastRow,"00000") &"]"
The Format(LastRow, "00000") makes the value always five digits long. With LastRow=1 the output would be [BHD#00001].
If you want to start by a value of 60555 than just add this value
Me.tbTicketNo.Value = "[BHD#" & 60555+LastRow &"]"
With LastRow=1 the output would be [BHD#60556]

Dynamic conditions related to radio buttons in excel

How to make the conditions of radio button dynamic? for eg. if i have one radio button "yes" in A100 and the condition related to it is appearing in A103. And, if i am inserting the row i want both(button and its value) to get shifted accordingly .
Sub OptionButton21_Click()
Range("A103") = " Not Applicable "
End Sub
Sub DynamicRange()
'Best used when only your target data is on the worksheet
'Refresh UsedRange (get rid of "Ghost" cells)
Worksheets("Sheet1").UsedRange
'Select UsedRange
Worksheets("Sheet1").UsedRange.Select
End Sub
Thanks in advance
Use a named range
1 -Select the target cell (i.e. A103) and give it a name in the "Name Box" on the top-left of the screen. Suppose you chose the name "MyTargetName"
2- Make the control to move with cells.
Right-click the control --> Format control --> Properties --> Move but dont size with cells
3- In VBA code, refer to the target cell by its name instead of its address:
Sub OptionButton21_Click()
Range("MyTargetName") = " Not Applicable "
End Sub
This will make the referenced target shift correctly, along with the control, whenever new rows are inserted.

Excel 2010 F2-Enter Macro Button

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!

Order of Events for the SheetFollowHyperlink Event

I have a workbook that lists people's names and overall performance for the past few weeks. I was asked to make it so that when their names are clicked, the details for that person would show. The solution I came up with at the time was to point all of the hyperlinks to a sheet that reminded them to turn on their macros.
In the WorkSheet_Activate event, I redirected them to another sheet that populated with the details for the person they had selected. This works fine. If the user doesn't have their macros turned on, they get a friendly reminder, but if they do, they immediately get redirected. There's just one problem. Because the hyperlink takes them somewhere else first, it causes the infamous "screen flicker".
On researching, I found the FollowHyperlink Events (I think the workbook level event would be most suitable for my purposes, though). However, before I get started rebuilding it, I wanted to make sure that this would solve the "screen flicker".
On MSDN, it states that the Event occurs when the user clicks the hyperlink. I can't seem to find anywhere that directly states whether this Event is triggered before or after the user is directed to the other sheet, though. If it gets triggered right after, that wouldn't really help me, but if it gets triggered before, I could put an Application.ScreenUpdates = False in the event, and it would solve my problem.
TL;DR (Get to the point already):
Does the Workbook_SheetFollowHyperlink Event happen before or after the user is directed to where ever the hyperlink is pointed?
To answer your question, the event is triggered after the hyperlink is followed. You can demonstrate this using the code below:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
Debug.Print "Workbook level: " & ActiveSheet.Name
End Sub
When the event is triggered, the ActiveSheet is the sheet directed to by the hyperlink.
I'm not sure of a way to solve your problem directly, even the Click() event won't be raised when you click a hyperlink. However, the Worksheet level hyperlink event handler will be called before the Workbook level handler and this may speed the process up enough to not see the flicker.
You can prove this if you leave the Workbook level event as is and add the following code to the Worksheet containing the hyperlinks:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print "Worksheet level: " & ActiveSheet.Name
Sleep 1000
Debug.Print "Leaving worksheet level event"
End Sub
After a few experiments (see below) I concluded that:
Allow the user to use the hyperlink columns when macros are disabled.
When macros are enabled, hide the current hyperlink column and show another column that gives the user a different "hyperlink" that takes them directly to where you want them to go.
The 2nd hyperlink can easily be "simulated" such that it picks up the other columns hyperlink. (See below)
I hope his helps
Interesting, here's a few things I tried
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' This does not fire if the user clicks directly on the hyperlink text
' it only fires if when click on the cell space that is not text
With ActiveCell
If Hyperlinks.Count Then
MsgBox "hi" & .Hyperlinks(1).Range
.Hyperlinks(1).Follow
End If
End With
End Sub
However you could make the text in the cells look like a hyperlink but actually just be blue underlined text. You could then use the Worksheet_SelectionChange to go to a related cell.
The question then become how to store the related cell.
You want to store it so that if row and column are inserted on the destination sheet, the reference will adjust. (eg NOT in comments, or the description of named ranges etc..)
Depending on how much data you have there's a variety of ways you could choose from.
I think I would favour this:
Having a hidden column next to the cell on display that has a hyperlink. The cell on display has a formula that is set to pick up the value from the hidden column (I quite like the sound of this as you have the hyperlink column already - so just modify the above code to get the hyperlink from the column next to the clicked cell using offset perhaps)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' This does not fire if the user clicks directly on the hyperlink text
With ActiveCell.offset(0,1)
If Hyperlinks.Count Then
.Hyperlinks(1).Follow
End If
End With
End Sub
You could mess around with named ranges, but it will be a pain.
I've just realised that what I tried won't help much as you need the hyperlink to work when macros have not been enabled!
So to get the above to work you could change the columns displayed when the user enables macros. ie the hyperlink column is shown when macros are disabled (and the column to the left of it is not).
When macros are enabled hide the hyperlink column and show the one to the left of it that will cause the SelectionChange event to run.
(You need to beware how other hyperlinks are used as any cell with a hyperlink next to it will respond to the event. You may need to use intersection to check the cell clicked is in a namedrange that contains "all the cells that need to respond to a user clicking them in the manner".
All the above sounds a bit mad, but it would appear the event model does not facilitate stopping the screen flickering.
I hope there is a better way of doing what you want, but for what it's worth, the above might help.
Harvey

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