Highlight Active Cell in Excel Without Resetting Existing Filled Cells - vba

How can I automatically highlight the entire row where an active cell is highlighted without getting rid of other cells that are highlighted? (I want to highlight the entire row when there is an active cell and then un-highlight it when I move away.) t
I know the following VBA code does that but it eliminates all other cells filled with color.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target
' Highlight the entire row and column that contain the active cell
.EntireRow.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True End Sub

You could do that with Conditional Formatting and simple VBA Event. Follow these steps:
In Excel:
1. select range when you want to have highlighted rows... A1:J20 in this example
2. goto menu >> home >> conditional formatting >> new rule... >> use a formula to determine which cells to format
3. in formula textbox write this formula: =CELL("row") = ROW(A1)
4. set formatting stale by pressing 'format...' button
5. press ok
In VBA
in module of the sheet for which you made the above action use this event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Calculate
End Sub

Related

Have one worksheet mimic another worksheet- VBA, Excel

Below I have code that provides my Summary sheet with cell values from my Main sheet whenever there is a change in a cell on the main sheet.
This worked great, but I ended up needing to sort the main sheet whenever a new line is added so it is in alphabetical order.
When it is sorted nothing changes in the Summary tab because nothing in a cell was actually changed.
Is there a way to get the summary tab to mimic exactly what the main sheet is doing and not just cell changes.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Summary").Cells(Target.Row, Target.Column).Value = Target
Application.ScreenUpdating = True
End Sub

Change active sheetname based on a cell value of that sheet automatically

I found this code on google which can help me to change current tab name based on a cell value of this sheet. However, I have to run this macro code manually each time. How can I modify this code to make it change automatically after entering value in a cell or at least tab's name changes as typing. Here is the code:
Sub myTabName()
ActiveSheet.Name = ActiveSheet.Range("C3")
End Sub
place this in the sheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C3" Then ActiveSheet.name = ActiveSheet.Range("C3")
End Sub

How do I hide a sheet with a button in Excel?

I would like to create a button to hide the sheet. Ideally, it would hide the sheet where the button is located.
To be simple, sheet 1 has a form to fill out. The typical name, address, phone number, etc. Sheet 2 and 3 also have the same fields that is going to be referenced to the 'Data' tab as well. On a sheet named 'Data', I will add fields that will populate simply with the = option like (=Data!...)
However, I do not want the Data page in view once the information is added. We all know the simple right click and hide sheet. But sometimes that's too much for some people that will use this sheet and a pretty button would work better.
I was successful using:
Module 1
Sub SheetCommand()
If Worksheets("Sheet1").Range("C2").Value <> vbNullString Then
Worksheets("Sheet2").Visible = False
Else
Worksheets("Sheet2").Visible = True
End If
End Sub
Sheet1 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$2" Then Exit Sub
Run "SheetCommand"
End Sub
However, I am not very VB savvy and have hit a dead end. Could someone help show me how to apply this to a button? I don't want it to reference the $C$2 field as noted on the example, but just when someone presses the button, the sheet goes away. I'm not worried about getting it back as someone can be ready to get it the old fashioned way. This would help the data entry process for this manual form so much easier.
Edit: basically, I need help creating a vba code where I can hide the page. I'd like to create a button where once clicked, it hides that page. I showed an example of a code where I got it to work but it only works if that cell is populated. How do I make it work on button click?
I found this code that does what I need but I would like to tell it a specific Sheet Name instead of having to type it in B6 and B7.
Sub ShowHideWorksheets()
Dim Cell As Range
For Each Cell In Range("B6:B7")
ActiveWorkbook.Worksheets(Cell.Value).Visible = Not
ActiveWorkbook.Worksheets(Cell.Value).Visible
Next Cell
End Sub
It's actually not very clear to me what's your exact goal
for instance, assuming "Sheet 2" and "Sheet2" would point to the same sheet, in your question you seem to reference it as being both a "Form" sheet ("...sheet 1 has a form to fill out....Sheet 2 and 3 also have the same fields...") and "Data" sheet (Worksheets("Sheet2").Visible = False)
so here follow some possible solutions:
1) you want to hide "Data" sheet before closing the workbook it's contained in
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Data").Visible = False
End Sub
2) you want to hide "Data" sheet once some cells have been filled up in ANY sheet other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name <> "Data" Then
With Sh
'here follows code to check if "the information is added"
'for instance:
If WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count Then ThisWorkbook.Worksheets("Data").Visible = False 'check if all cells in range "A2" to "C2" has been filled with some data
End With
End If
End Sub
3) you want to hide "Data" sheet once some cells have been filled up in ALL sheets other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sht As Worksheet
Dim hideBool As Boolean: hideBool = True 'set initial value as true
If Sh.Name <> "Data" Then
For Each sht In ThisWorkbook.Worksheets
With Sh
'here follows code to check if "the information is added"
'for instance:
hideBool = hideBool And WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count ''check if all cells in range "A2" to "C2" of current sheet has been filled with some data
End With
If Not hideBool Then Exit For
Next sht
ThisWorkbook.Worksheets("Data").Visible = Not hideBool ' hide if ALL sheets met "information is added" condition
End If
End Sub
4) otherwise give more info about the desired behavior

Removing Formula from Excel Blank Cells automatically

How can i remove Formula from Excel Blank Cell, For Example i have these formula in one of the Blank Cells in Excel Dynamically.
=IF('filepath[filename.xls]Sheet1'!$A$1:A$65536="","",'filepath[filename.xls]Sheet1'!$A$1:A$65536).
Thanks
This will be some simple macro as:
Sub Macro1()
For Each c In Worksheets("your sheet name").Range("your range")
If c.Value = "" Then c.Select: Selection.ClearContents
Next c
End Sub
Where "your sheet name" can be for example "Sheet1"
and "your range" can be for example "a1:a10"
Private Sub Worksheet_Calculate()
Dim cell As Range
On Error GoTo finish
Application.EnableEvents = False
For Each cell In UsedRange
If cell.Text = "" Then cell.Clear
Next
finish:
Application.EnableEvents = True
End Sub
Unfortunately there is no way for Excel to provide a truly empty cell if it contains a formula, but I have a way to empty the cells:
Create an if statement that, if false, returns ERROR.TYPE(1).
From there, select the range of cells you want to delete the intended "blanks" from and use "Find and Select" >> "Go to Special".
Click on the "Formulas" radio button and leave "Errors" as the only box checked. Clicking OK will highlight all the cells that were assigned the value "#N/A".
Now just press the delete key.

Adding a formula dynamically to an excel cell

Is there a way (VB?) to add a formula dynamically to an excel cell?
I have a cell with conditions true and false, and they change based on a checkbox.
And I have another cell, which has a formula in it. This formula should be used if the checkbox is unchecked.
If the checkbox is checked, then user should be able insert value manually (without any formula prompting there).So the formula should not be there.
I was thinking of a solution where I would add the formula to the cell if checkbox is unchecked, and then if the checkbox is checked, then I would clear the cell.
How could this be done? I'm not very familiar with excel coding and VBA.
Ok you need a trigger on TRUE/FALSE cell to execut the next VBA code,
right click on sheet name and click "View Code" and enter this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A5:A5")) Is Nothing Then 'define adress of your True/Flase cell
If Target.Cells.Value = False then
Range("B5").formula = "=enter your formula" 'define adress for cell with formula aswell
else
Range("B5").value = ""
end if
end if
end sub
well you could use:
if userform1.checkbox.checked = false then
range("A1").formula = "=myformula"
else
range("A1").value = ""
end if
you need to insert the code into the userform checkbox click or change event both should have same effect, just double click on the checkbox in userform and it will take you to the click event or replace the click with "change", hope that's what you meant to achieve, cheers
PS. thanks for suggestions #99moorem
If you have a classical Excel CheckBox, you can add a Linked Cell which will be True or False.
In the following code, your Linked Cell is in A1, the cell with the formula to use in B1 and the cell that need to be empty or filled by formula is in C1.
You'll need to specify the Sheet_Name (can be differents) and place that code directly into the Sheet "module" in VBA (press Alt+F11 and double-click on the sheet (in the left panel) with the Linked Cell, then just paste and edit regarding your specifications)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LinkedCell As Range, _
FormulaCell As Range, _
ChangingCell As Range
Set LinkedCell = Sheets("Sheet_Name").Range("A1")
Set FormulaCell = Sheets("Sheet_Name").Range("B1")
Set ChangingCell = Sheets("Sheet_Name").Range("C1")
If Application.Intersect(Target, LinkedCell) Is Nothing Then
'not in linked cell
Else
'in linked cell
If LinkedCell.Value2 <> True Then
'Unchecked
ChangingCell.FormulaLocal = ChangingCell.FormulaLocal
Else
'Checked
ChangingCell.FormulaLocal = vbNullString
End If
End If
Set LinkedCell = Nothing
Set FormulaCell = Nothing
Set ChangingCell = Nothing
End Sub