Reset Button Excel VBA - Value as Number not Text - vba

I have a spreadsheet and I want to reset some cells back to how they begin.
My Reset Button works using the following code.....
Sub Reset_Cells()
'Updateby Extendoffice 20161008
Range("D4", "D8").Value = "£0.00"
Range("D11").Value = "£0.00"
End Sub
It works great but it inserts the £0.00 as text which effects a dependant cell with an IF statement (see below)
=IF(D4=0,0,IF(D4<=300,35,IF(D4<=500,50,IF(D4<=1000,70,IF(D4<=1500,80,IF(D4<=3000,115,IF(D4<=5000,205,IF(D4<=10000,455,IF(D4<=200000,ROUND(D4/100,2)*5,IF(D4>200000,10000))))))))))
When clicked the vlaues 'look' how they should but the dependant cell shows £10,000 (the highest IF option) instead of £0.00.
The resetted (is that even a word?!) cells have the green triangle in the corner which tell me the cell has a Number Stored as Text.
How can I fix this?
Thanks

How about:
Sub Reset_Cells()
With Range("D11,D4,D8")
.Value = 0
.NumberFormat = "£#,##0.00"
End With
End Sub

Related

It is possible to trap AutoFilter as an Event?

I am trying to show the filter columns to users in the report. Excel gives a different icon but for large no. columns it will be good to color the columns in another color like blue.
I found code at Is there a way to see which filters are active in Excel, other than just the funnel icons?
It works for me, but how do start this code without any button
SheetChange and selection change do not work.
code
Sub test()
Call markFilter(ActiveSheet)
End Sub
Sub markFilter(wks As Worksheet)
Dim lFilCol As Long
With wks
If .AutoFilterMode Then
For lFilCol = 1 To .AutoFilter.Filters.Count
'/ If filter is applied then mark the header as bold and font color as red
If .AutoFilter.Filters(lFilCol).On Then
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbRed
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = True
Else
'/ No Filter. Column header font normal and black.
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbBlack
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = False
End If
Next
Else
'/ No Filter at all. Column header font normal and black.
.UsedRange.Rows(1).Font.Color = vbBlack
.UsedRange.Rows(1).Font.Bold = False
End If
End With
End Sub
I will use the same example I used in the answer that you mentioned in your post. I answered that. :)
There is no filter change event in excel. One work-around that I would use is trapping the calculate method of the worksheet or better the workbook.
So, in the worksheet with filter add a dummy formula like this: =SUBTOTAL(3,Sheet2!$A$1:$A$100) this counts only visible cells. But its up to you. Feel free to use any formula that responds to filter change.
After that, go to workbook's code and add this :
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call markFilter(Sh)
MsgBox "Filter changed"
End Sub
Boom. Now you are trapping the filter change events and it will update the filtered columns by firing the vba code.
Note markFilter is coming from the answer that you mentioned.
The key points from my article Trapping a change to a filtered list with VBA
A "dummy" WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet.
A Worksheet_Calculate() Event is added to the "dummy" WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed.
The next two steps are only needed if it is desired to run the Workbook Calculation as Manual
Add a Workbook_Open Event to set the EnableCalculation property of all sheets other than "Dummy" to False.
Run the Workbook in Calculation mode

Hiding the data in rows -Excel VBA

I have three rows.I want to hide the data in those rows and display those rows in different color.I tried searching but only found Entirerow.hidden,which hides the row number as well. Is it possible to only hide the data in the rows and display it using some other color?
The below code will change the background color to Yellow for rows 1 to 3.
Sub ChangeBackColorForSpecificRows()
Rows("1:3").Interior.Color = 65535
End Sub
Change the Rows and Interior Color to suit your requirement.
Edit:-
formupahidden set to true not working neither formatting it to locked
and hidden ,is hiding the content of formula bar – Sunaina
Copy the below code and do right click on sheet tab and select view code and paste it.
Close the VBA window (Alt+Q to close VBA window) and return to that sheet and check.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Rows("1:3")) Is Nothing Then
If Application.DisplayFormulaBar Then Application.DisplayFormulaBar = False
Else
If Not Application.DisplayFormulaBar Then Application.DisplayFormulaBar = True
End If
End Sub

Conditional formatting excel textbox

What is the best way to change the font color of a single textbox based on value of linked cell?
Textbox is located on sheet1 when recording macro it recognizes textbox as ActiveSheet.Shapes.Range(Array("TextBox 1")).Select
I have inserted an image on sheet1, then I inserted textbox's from the insert toolbar. All the textbox's are linked to data on the "stylist" sheet. This sheet gets updated with a macro when the workbook is opened. I'm trying to get the textbox fonts to be red or green based on comparing the value of the linked cell to another cell on the stylist sheet.
enter image description here
Please try this..
' replace Text with your text box name
ActiveSheet.Text.Object.ForeColor = RGB(0, 255, 0)
With the code you provided and described as having a linked cell, one would assume you are referring to a TextBox from the ActiveX toolbar.
The code for that textbox is located in the Worksheet module. Right click on the sheet tab and select View code to open that module.
If your Linked cell is A1 then we could use a Worksheet_Change event to trigger the code when you change A1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address <> "$A$1" Then Exit Sub
Dim x
x = IIf(Target <= 0, vbRed, vbGreen)
Me.TextBox1.ForeColor = x
End Sub
You can also use the TextBox1_Change event, this would trigger when the textbox changed.
Private Sub TextBox1_Change()
Dim x
x = IIf(TextBox1.Value <= 0, vbRed, vbGreen)
Me.TextBox1.ForeColor = x
End Sub
You did not indicate what the conditions were so I made up my own.This example uses,
if <=0 then red, else green
The results would be

VBA - Excel Showing form after double click on a Cell

I've encountered a strange Problem. I have an Input Form for the User in Excel.
This Form provides basic CRUD functions to edit a List in a Table. Now this Userform also has a ListBox which shows all the entrys with Id and some basic infos (so you know which entry is which ). Now what I want is, that if you select a row in the normal excel table, and then open the InputForm, the selected row in the table should also be selected in the listBox of the InputForm.
I do this like this:
If Selection.Row >= StartRow() Then
ListBoxAll.listIndex = Selection.Row - StartRow()
Else
ListBoxAll.listIndex = 0
End If
This works great if I open the Ui per Button click. But if I try to open the form with the Before Double Click Event. I got an Offset.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserFormInput.Show
End Sub
Now for the strange part. If I check what value the Selection.Row has, by showing it with a MessageBox, it all works fine! But as soon as I remove the MessageBox its broken all over again.
If Selection.Row >= StartRow() Then
ListBoxAll.listIndex = Selection.Row - StartRow()
MsgBox Selection.Row
Else
ListBoxAll.listIndex = 0
End If
It does not matter where in the if I put the Messagebox.
So the Question has anybody ever got the same Problem ? And does anybody know a solution or a workaround to it ? (I don't want to show a Messagebox!)
So I added some screenshots to make it easier to understand.
Update I think it has something to do with the Focus after a double click, which is (maybe) on the doubleclicked Cell. And gets changed if I put out a MsgBox
(I'll write this as an answer but this is only a part of the code... :)
Oh, german ^.^
I believe each time you call your userform you'll have to compare the selected data to the listbox data. As you have a multicolumn listbox you could do this by the following (I believe, but untested):
Dim ThisRow As Integer
ThisRow = ActiveCell.Row
Dim ThisValue As String
ThisValue = ActiveWorkbook.Sheet("Tabelle1").Cells(ThisRow, 1).Value
This "1" should be the column which contains the same kind of information as is displayed in the first column of your listbox.
Dim i As Integer
For i = 1 to UserFormInput.Listbox1.Listcount
If UserFormInput.Listbox1.List(i, 0).Value = ThisValue Then
UserFormInput.Listbox1.List(i).Select
End If
Next i

(Excel VBA) If Cell Value equals "" Then Show/Hide Images

I am working on a Excel Spreadsheet that when a dropdown box value is selected an image will pop up, and if another value is selected it will hide the current image and pop up the image related to the selection. I have found a few methods that are just too time consuming using just the sheet and positioning of the image using coordinates; that's not exactly the route I would like to go.I have done a quite a bit of research before using StackOverflow, and nothing seemed to work thus far. Below is what I am trying to achieve. I am trying to keep all the images within the spreadsheet which adds another level of challenge, but I believe there is a way to do this because excel assigns the image a number when inserted EX. Picture 9.
Sub Main()
If Range(G11).Value = "anything" Then
Picture1 show
Picture2 hide
End If
End Sub
Any Help is greatly appreciated. Thanks
Rather than hiding/moving/reducing the size of the unwanted pic, why not simply delete it?
Logic:
Save all your images in a temp sheet. When ever a relevant picture is supposed to be shown, get it from the temp sheet and delete the previous.
Here is an example.
Sub Sample()
Select Case Range("G11").Value
Case "Picture 1": ShowPicture ("Picture 1")
Case "Picture 2": ShowPicture ("Picture 2")
Case "Picture 3": ShowPicture ("Picture 3")
Case "Picture 4": ShowPicture ("Picture 4")
End Select
End Sub
Sub ShowPicture(picname As String)
'~~> The reason why I am using OERN is because it is much simpler
'~~> than looping all shapes and then deleting them. There could be
'~~> charts, command buttons and other shapes. I will have to write
'~~> extra validation code so that those shapes are not deleted.
On Error Resume Next
Sheets("Sheet1").Shapes("Picture 1").Delete
Sheets("Sheet1").Shapes("Picture 2").Delete
Sheets("Sheet1").Shapes("Picture 3").Delete
Sheets("Sheet1").Shapes("Picture 4").Delete
On Error GoTo 0
Sheets("Temp").Shapes(picname).Copy
'<~~ Alternative to the below line. You may re-position the image
'<~~ after you paste as per your requirement
Sheets("Sheet1").Range("G15").Select
Sheets("Sheet1").Paste
End Sub
Snapshot of temp sheet
Here is a solution using the Visible property of the object.
I used this to show a picture based on a value in a field.
The field had a formula that resulted in either "good" or "bad".
If its value was "good", I wanted to show one picture; for "bad", another picture should show; and they should never show at the same time.
The field needed to update its value whenever a user refreshed a pivot table, so I put the code in that method of the worksheet where the pivot table and picture were to appear.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'hide both pictures by loopng through all the shapes on the sheet
Dim s As Shape
For Each s In ActiveSheet.Shapes
'hide the shape if it is a picture, leave other shapes on the page visible.
If s.Type = msoPicture Then s.Visible = msoFalse
Next
Dim judgement As String
'The field whose value tells what picture to use is a one-cell named range called "judgement"
judgement = Range("judgement")
'you need to know which picture is which.
If judgement = "Good" Then ActiveSheet.Shapes("Picture 8").Visible = True
If judgement = "Bad" Then ActiveSheet.Shapes("Picture 1").Visible = True
End Sub
Sub hidePicture(myImage)
ActiveSheet.Shapes.Range(Array(myImage)).Select
Selection.ShapeRange.Height = 0
Selection.ShapeRange.Width = 0
End Sub
Sub showPicture(myImage)
ActiveSheet.Shapes.Range(Array(myImage)).Select
Selection.ShapeRange.Height = 200
Selection.ShapeRange.Width = 300
End Sub
Handy tip: record macro and look at the code it generates!
Might be better just to move your pictures "off screen", particularly if they're of different sizes.
Sub Tester()
ShowPicture "Picture 3"
End Sub
Sub ShowPicture(PicName As String)
Dim s As Shape
For Each s In ActiveSheet.Shapes
With s
.Top = IIf(.Name = PicName, 100, 100)
.Left = IIf(.Name = PicName, 100, 1000)
End With
Next s
End Sub