Copy specific cell contents into clipboard in Excel - excel

I am a frequent user of Excel. Now I was looking to copy some of specific cells into clipboard for pasting them in another application.
I could see the copy single cell data into clipboard. Can someone suggest steps to make a configurable way to copy specific cell data into clipboard by the click of a button?

This will work for any sheet in the workbook and clear the clipboard before copying. Right click the Excel logo just to the left of File on the menu bar, select View Code and paste in
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.CutCopyMode = False
Target.Copy
End Sub
then press ALT + Q to return to your sheet.

Related

Copying the activecell to a cleared textbox and keeping the hyperlink

Goals
I am trying to make a macro that will copy the active cell and paste the contents of that cell to a text box.
I also need it to clear the text box before adding the new value, as well as keeping the original hyperlink source.
Context
I have a large list of items that have hyperlinks to various web pages. What I ultimately want is for a user to search a specific item, using the Ctrl + F function, and the item be copied with its hyperlink into a textbox at the top of the sheet.
Then once a new Item is searched it will clear the textbox and copy the new item into it.
Code
All it does is copy the active cell into the text box. I will not clear the box beforehand or copy the hyperlink.
Option Explicit
Private Sub TextBox1_Change()
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Copy
Sheet1.TextBox1.Paste
End Sub
If you're after the hyperlink address, in the text box then just use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet1.TextBox1 = Target.Hyperlinks(1).Address
End Sub
If you're after the text, but want the hyperlink to fire when the button (or text) is pressed - I'm not sure if either are possible. I guess perhaps you could use the OnAction to cause a hyperlink to fire.. but we'd need more info. Is this an ActiveX Textbox on a sheet?

Is there a Macro to hide all sheets in a workbook when a certain cell is equal to 100%?

I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub

Is it possible to change the Enter key function for just one cell?

I have a cell (=$C$4) on a worksheet that is referenced in multiple formulas. I am using it as an end-user input cell.
I am trying to avoid selecting next cell after Enter is pressed so users can input values, hit Enter, see the results, then if needed, replace the contents with new values in the cell and hit Enter again to see different results without having to arrow-up or click on the cell again.
Is there a way to change the function of Enter for just this cell or is anyone aware of another work-around for this?
You can use sheet protection to do that:
Unlock cell C4 in the Format Cell dialog (in the Protection tab)
Make sure all other cells are locked (which they are be default)
Right click the worksheet name, click Protect sheet, uncheck Select locked cells and press OK
Done!
If for some reason this is not applicable, you can use this macro:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C4")) Is Nothing Then
Me.Range("C4").Activate
End If
End Sub
You need to place this code in the worksheet module of your worksheet. Press Alt-F11 to activate the Visual Basic Editor, double click on the worksheet in the tree view on the top left and paste your code in the main window!
I did something similar once by implementing a VBA subroutine. I think that the signature of the sub is:
Private Sub Worksheet_Change(ByVal Target As Range)
All you need to do in the body is check if the current cell is the cell below C4 then select C4, otherwise do nothing.

How to Protect the sheet name not the entire sheet?

I need to protect the sheet name by preventing
any change to the sheet name, or
the sheet being deleted.
This must be done without protecting the entire sheet using the Protect Sheet or Protect Workbook options.
Is there any way to do this with VBA?
Right click the sheet tab that you wish to protect
View Code
Copy and paste in the code below
This code disables the delete control on the sheet (but not right click on cell) menu when the sheet is activated. The control is enabled when the sheet is de-activated
The code will also name the sheet "NameOfSheet" when the sheet is de-activated. This is a workaround to prevent the sheet being renamed
Private Sub Worksheet_Activate()
Application.CommandBars.FindControl(ID:=847).Enabled = False
End Sub
Private Sub Worksheet_Deactivate()
Application.CommandBars.FindControl(ID:=847).Enabled = True
Me.Name = "NameOfSheet"
End Sub
I don't think you can. What you can do, you can make a worksheet a very hidden one (accessible only from VBA) and in case of the deleted sheet, you can copy it and make a copy visible.
Would this approach work?
Select all cells in the sheet, then UN-lock all cells with "Lock
Cells" (yellow background of padlock turns white).
Write the name of the sheet in a (fixed or named) cell, then lock
this cell ONLY ("Lock Cells", padlock background turns yellow).
Then Protect workbook, but allow every action, except the first one
"Select Locked Cells".
The user can do everything except selecting the cell with the sheetname (and delete rows/columns).
Now write a VBA to compare the actual sheetname with the data in the protected named cell (or fixed reference e.g. A1).
Run this script either on every change (probably too much) or at least on close of
the workbook.
As long as the sheetname is always in the same cell (e.g. A1), you can then loop through all sheets, compare their name with the data in cell A1 and correct the sheet name if required.

Excel - Highlight active row

Wondering if there is some preference I can set in excel so that when I am in a specific cell the entire row is slightly highlighted (not overly so but enough to easily tell it out)
Similar to the fashion that Notepad++ works in tab delimeted files, and highlights the entire row.
Thanks in advance
I do not know of any built-in preferences in Excel to do this but it can be done using VBA. Place the following in "ThisWorkbook" of the VBAProject:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Tgt As Range)
If Tgt.Cells.Count = 1 Then
ActiveSheet.Rows(Tgt.Row).Select
Tgt.Activate
End If
End Sub
The code only highlights the active row if a single cell is selected so that it does not interfere when multiple cells are selected.

Resources