Removing Formula from Excel Blank Cells automatically - vba

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.

Related

Move ActiveCell back to the cell whose value got changed

I have a macro that needs to run when a cell value gets changed.
This is what I have. This doesn't work because when I change the value of cell and then hit Enter or click on some other cell, the macro takes values from the cell that I have moved to and not from the cell whose value I changed
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("B3:V34")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox (Target.Address)
'prints the cell address whose value I changed before I clicked on some other cell
MsgBox (ActiveCell.Address)
'prints the cell address of the new cell where I have moved to
ActiveCell.Range(Target.Address).Activate
'This is me trying to move the active cell to the cell whose value I changed
'But this doesn't work. The activeCell moves to some random cell
'I wanna run a macro here which takes activeCell address and value as input
Finalize:
Application.EnableEvents = True
End Sub

unlock specific area in a protected excel sheet with vba

I have to unlock a specific range (D6:BC116) in a excel sheet. It should be able for other people editing this specific area. So it should be unlocked for them.
At first I manually protect the whole sheet without any code. And after that I want to unprotect the specific area for editing. But something always goes wrong. I have these two codes. The first code has the hidden property it only hides empty cells. The other code I am trying to unprotect specific area I want to edit after protecting the whole sheet.
I am not really sure if the problem is in the first code because of the hidden property? And I am not sure if they are in a relation?
Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
End Sub
Sub UnlockCells()
Worksheets("Sheet1").Range("D6:BC116").Locked = False
Worksheets("Sheet1").Protect
End Sub
And when I execute this I always get "Index out of range"
Thanks!
I think you need to unprotect before unlocking.
Like this:
With Worksheets("Sheet1")
.Unprotect "MyLongAndSecurePassword"
.Range("D6:BC116").Locked = False
.Protect
End with
Concerning the first part of the code - make sure that you use a variable, which is not named cell, because cell is used by the VBEditor. Name your variable rngCell, myCell or anything else but cell. And declare it like this: Dim rngCell as Range.
Last point - lock your worksheet and try to hide and unhide manually the rows. Is it possible? If not, you know the reason for the error.
Edit:
To check whether the sheet is protected, try this in the Worksheet_Change:
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("Sheet1").ProtectContents Then Exit Sub
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
End Sub

Highlight Active Cell in Excel Without Resetting Existing Filled Cells

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

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

Hide Rows when a specific string is in a cell

I have a problem with VBA in Excel 2007.
I need a macro to hide rows when a specific string is in a cell. (e.g. "System PPP cancelled")
My macro:
Sub HideRows()
Dim Cell As Range
If InStr(Cell, "cancelled") And Rows(Cell.Row).Hidden = False _
Then Rows(Cell.Row).Hidden = True
Next Cell
End Sub
Unfortunately, I get the runtime-error '13'...
Can you help me?
This works in Excel 2010:
Sub hide_cancelled()
For i = 1 To Rows.Count
If InStr(Cells(i, 2).value, "cancelled") And Rows(i).Hidden = False Then
Rows(i).Hidden = True
End If
Next i
End Sub
Note that this will iterate over ALL rows in the spreadsheet, no matter if they contain any data or completely empty. This may take a while! So instead of Rows.Count you should enter a more sane value.
Also this will expect the search value in column 2. Change this in Cells(i,xxx)