Conditional formatting excel textbox - vba

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

Related

Double click event in the excel sheet column to fill all textboxes of a Userform

I am trying to use double-click event of worksheet to populate a all textboxes in a userform with value when click on a specific column in the double-clicked row.
Eg: I have 6 columns
ID Project Approval tool status feedback
1 A1 yes 123 ongoing good
2 B2 no 456 stop bad
When I double-click on any cell in the column ID, userform with a textbox will pop up with the textbox filled with the value from the all columns (Project, Aprroval, tool, Status, Feedback) the row which was double-clicked. SO, when I double-click on "1" in column ID a userform should pop up with textbox value "A1", "yes", "123", "ongoing", "good".
this is my code so far i am trying for the double click Event.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, CANCEL As Boolean)
Dim UserForm2 As frm_Update
If Target.Column = 1 Then
Set UserForm2 = New frm_Update
CANCEL = True
With UserForm2
Dim wks As Worksheet
Set wks = ThisWorkbook.Worksheets("Sheet1")
wks.Activate
.txtProject = Cells(Target.Row, 2)
.txtapproval = Cells(Target.Row, 3)
.txttool = Cells(Target.Row, 4)
.txtstatus = Cells(Target.Row, 5)
.txtfeedback = Cells(Target.Row, 6)
Me.Show
End With
End If
End Sub
i used this Double click event as a reference to my Problem but I am not able to solve the Problem. My userform is not opening when i run the Code.
Me in the event routine of a sheet refers to the worksheet, not to the form. As a worksheet has not Show-method, you should get a runtime error.
Simple change the line to .Show should display the form.
To avoid such errors, always put Option explicit at the top of all modules. If you do, you would get a compiler error Method or data member not found

Reset Button Excel VBA - Value as Number not Text

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

Automatically updating a userform label

I'm new to VBA and programming in general, so bear with me. I have so far 6 labels, and a simple useform where one enters a value corresponding to each label. However, the label name can change (say if new labels are added) and I wanted a way to automatically change the label name. This is what I have so far:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Update_invest.Invest1.Caption = Sheet1.Range("A12").Value
Update_invest.Invest2.Caption = Sheet1.Range("A13").Value
Update_invest.Invest3.Caption = Sheet1.Range("A14").Value
Update_invest.Invest4.Caption = Sheet1.Range("A15").Value
Update_invest.Invest5.Caption = Sheet1.Range("A16").Value
Update_invest.Invest6.Caption = Sheet1.Range("A17").Value
End Sub
Now this works: if I change a label it will update the userform. However, if I just open the new worksheet and don't click on say cell A12, it will not update the userform and will leave the label name as it's default name. How do I make it actually save the label name, so that if a user opens the workbook the userform will already have the current cell value (without first having to click on the cell for the userform to update)?
I have this code currently on sheet1.
Solution 1
You can put your code in Worksheet_Activate which is run when the user select the sheet or in Workbook_Open which is run when the user open the workbook. Currently, your are using Worksheet_SelectionChange which is only run when the user change the selection, hence the need to click on another cell.
NOTE : if you chose to use Workbook_Open, you will need to put the sub in the ThisWorkbook file and not in the worksheet itself.
Solution 2
If you instead want to make the changes permanant and not have code to always update it, you will need to go through the designer
Here is an exemple
Sub exa1()
Dim i As Long, n As Long
n = 12
With ThisWorkbook.VBProject.VBComponents("Update_invest").Designer
For i = 1 To .Controls.Count
If TypeName(.Controls(i - 1)) = "Label" Then
.Controls(i - 1).Caption = Sheet1.Cells(n, 1).Value
n = n + 1
End If
Next i
End With
End Sub
source

Is there a way to have a scrollable list that is editable?

Following this video I have a spreadsheet that has a "Edit History" box on it that scrolls up and down. It uses a forms control scroll box and a list on a secondary sheet to create a scrollable list. The problem with this is that you cannot then edit the information in the scroll box (you must edit it on the secondary sheet).
I have VBA that automatically enters the person's name into the scrollbox when they edit any part of the sheet, and then enters "Note:" below that. I want the person to be able to edit the "Note:" box so that they can enter the reason they are editing the sheet:
John Smith and James Appleseed are previous users of this sheet. When Wayne Smith comes in to edit the sheet, as soon as he makes a change, it adds "Wayne Smith" and "Note:" to the sheet. (I already have this part working using VBA).
Because of the way that the scroll able list is implemented, it is actually all just formulas within this edit history box. If I double click "Note:" to try and edit it, this is what appears:
But what I want to happen is the ability to edit the "Note:" box (without having to switch to the secondary sheet where the list is actually stored). Essentially I want to make a scroll able text box that is also directly editable, and works with VBA.
Is there any way to do this?
I put together a working example of how to do this.
You can download the workbook here.
This method uses two sheets in a workbook... Sheet1 for the listbox and listboxdata for the data. Sheet1 can be called anything you like.
It would probably be wise to hide the listboxdata sheet.
On Sheet1 you need a Forms Control scrollbar. Use the Name Box to rename it: ScrollBar1. Assign to it the Scroll() procedure.
All of the code for this app should be placed in the Sheet1 code module:
Option Explicit
Private Const LISTBOX_SCROLLBAR = "scrollbar1"
Private Const LISTBOX_DATASHEET = "listboxdata"
Private Const LISTBOX_DATAHEADR = "a1"
Private Const LISTBOX_SCROLLMAX = 50
Private Const LISTBOX_SCROLLMIN = 1
Private Sub Scroll()
Dim ListBoxRows&, n&, ndx&, v
On Error Resume Next
With Shapes(LISTBOX_SCROLLBAR)
SetProps ndx
ListBoxRows = .BottomRightCell.Row - .TopLeftCell.Row
v = ThisWorkbook.Sheets(LISTBOX_DATASHEET).Range(LISTBOX_DATAHEADR).Resize(ListBoxRows).Offset(ndx)
Application.EnableEvents = False
.TopLeftCell(, 0).Resize(ListBoxRows) = v
End With
Application.EnableEvents = True
End Sub
Private Sub Update(Target As Range)
With Shapes(LISTBOX_SCROLLBAR)
If Target.Column = .TopLeftCell(, 0).Column Then
If Target.Row >= .TopLeftCell.Row And Target.Row <= .BottomRightCell.Row Then
If Target.Count = 1 Then
ThisWorkbook.Sheets(LISTBOX_DATASHEET).Range(LISTBOX_DATAHEADR).Offset(.ControlFormat.Value + Target.Row - .TopLeftCell.Row) = Target
Else
Scroll
End If
End If
End If
End With
End Sub
Private Sub SetProps(Optional ndx&)
With Shapes(LISTBOX_SCROLLBAR).ControlFormat
.Min = LISTBOX_SCROLLMIN
.Max = LISTBOX_SCROLLMAX
ndx = .Value
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Update Target
End Sub
Private Sub Worksheet_Activate()
SetProps
End Sub
That's it.
No formulas required in the listbox and any changes made to the cells in the listbox will be written to the source data and kept.

(VBA) Putting ComboBox + macroevent on cell change

I'm trying to put a combobox inside active worksheet (but not activeX combobox), choose a list to fill and linked cell. It is an easy task, for example:
Sub make_combobox()
ActiveSheet.DropDowns.Add(69.75, 1.5, 79.5, 40.5).Select
Selection.Name = "combo"
ActiveSheet.Shapes("combo").Select
With Selection
.ListFillRange = "$A$1:$A$3"
.LinkedCell = "$D$1"
.DropDownLines = 8
.Display3DShading = False
End With
End Sub
I tried to put macro in worksheet containing this combobox, which will show msgbox whenever chosen linked cell is changed according to the chosen option in combobox. I wrote this in Worksheet section:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D1")) Is Nothing Then
MsgBox "It works!"
End If
End Sub
Unfortunately, it doesn't work (Actually, it works when I change a value in D1 manually, but not work as a result of change in combobox).
Just assign a macro to the control using the OnAction property. It will run after every change made to the Combobox's value.