Autopopulate cells in a row based on Active-x Dropdown - excel

I have an Active-x Dropdown and want to autopopulate other cells in row based on this Dropdown.
I wrote the code in worksheet-change Event but when i select from this drop-down it doesn't trigger The autopopulat code for other rows. Any help would be appreciated.
column 9 is my active-x drop-down list but when i select from the list the code for showing the next cell in my Resource(sheet) doesn't trigger.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsSource As Worksheet
Dim r As Long
Set wsSource = ThisWorkbook.Sheets("Source") 'Source sheet
Application.EnableEvents = False
If Target.Column = 9 Then
r = Application.Match(Target.Value, wsSource.Columns(8), 0)
Target.Offset(0, 1) = wsSource.Cells(r, 9)
End If
Application.EnableEvents = True
End Sub

Changing the value in a Active X ComboBox will not trigger a Worksheet_Change. Instead, use a ComboBox_Change event like so:
Private Sub ComboBox1_Change()
MsgBox "Please share your code next time you post. It will greatly help others help you :)"
End Sub
You may need to validate the selected value before running your code which can be done with a simple If ComboBox1.Value = "?" Then

Related

Call a hiding macro with a checkbox

I wanted to make a checkbox, calling a macro that hides and unhides columns on Excel worksheet with specific value in cell, but it is not working
I tried the following VBA script
Sub Hide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = True
End If
Next c
End Sub
Sub Unhide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = False
End If
Next c
End Sub
Sub CheckBox_For()
If CheckBox1.Value = True Then
Call Hide_Forecasts
Else
Call Unhide_Forecasts
End If
End Sub
Please help me out
You haven't said what type of checkbox you're using - Form Control or ActiveX Control.
For an ActiveX Control right-click the control and select View Code.
Use this code that sits behind the worksheet (CheckBox1 will be named after your checkbox).
Private Sub CheckBox1_Click()
Dim Cell As Range
For Each Cell In Me.Range("E12:CF12")
If Cell.Value = "Forecast" Then
'Checkbox returns TRUE/FALSE - Hidden takes TRUE/FALSE so just connect the two up.
Cell.EntireColumn.Hidden = Me.CheckBox1.Value
End If
Next Cell
End Sub
For a Form Control right-click the control and select Assign Macro.
Place this code in a normal module.
Sheet1 is the codename for the sheet (that's the name not in brackets in the Project Explorer).
'Form Control can have three values:
' 1 = Checked
' -4146 = Unchecked
' 2 = Mixed - ignoring that this value may occur.
Public Sub Checkbox_For()
Dim ChkValue As Boolean
'Is value different from -4146? Returns TRUE = Checked or Mixed / FALSE = Unchecked
ChkValue = Sheet1.Shapes("Check Box 1").OLEFormat.Object.Value <> -4146
Dim Cell As Range
For Each Cell In Sheet1.Range("E12:CF12")
If Cell.Value = "Forecast" Then
Cell.EntireColumn.Hidden = ChkValue
End If
Next Cell
End Sub

Userform and Selecting Cell values

Just playing around with userform. Pretty new to using them . Created a pretty simple userform that find me the percentage change between two values that I enter. (see image below). I'm just interested in going one step further, I want to know how to enter the values based on excel cells that I select. So in this example, if I select K6, the value 15 enters in current year and if I subsequently click on K8, the value 10 enters in base year). Hope that makes sense, let me know if that is possible. Thanks.
current code....
TextBox3_Change()
TextBox3.Value = Format((Val(TextBox1.Value) - Val(TextBox2.Value)) / Val(TextBox2.Value), "#0.00%")
Rather than clicking, why not load the values in those textboxes in the UserForm_Initialize() event? Something like
Private Sub UserForm_Initialize()
TextBox1.Value = Sheets("Sheet1").Range("K6").Value2
End Sub
If you really want to select a cell (after the userform is shown) and then populate the textboxes, then yes that is also possible. Here is an example
For demonstration purpose, I am going to populate 1 textbox. Let's say our userform looks like this. Note, I added a CommandButton1 next to the textbox. I changed the caption to .... We will use Application.InputBox with Type:=8 so that user can select a range.
Next paste this code in the userform code area
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range
On Error Resume Next
'~~> Prompt user to select range
Set rng = Application.InputBox(Prompt:="Select the range", Type:=8)
On Error GoTo 0
'~~> Check if the user selected a range
If Not rng Is Nothing Then
'~~> Check if the range is single cell or not
If rng.Columns.Count > 1 Or rng.Rows.Count > 1 Then
MsgBox "Select single cell"
Exit Sub
End If
TextBox1.Text = rng.Value2
End If
End Sub
Demonstration
This isn't a built-in functionality for userforms (unlock the create a graph dialogue box).
One option would be to have the user start by selecting two cells, and then your code runs and calculates the percentage change in those two cells selected and returns the answer in a pop-up. Another option would be to have the userform automatically populate from preselected cells in your workbook (example below).
Private Sub UserForm_Activate()
Dim ActiveR As Long
ActiveR = ActiveCell.Row
TextBox1.Value = Cells(ActiveR, 1).Value
TextBox2.Value = Cells(ActiveR, 2).Value
TextBox3.Value = Cells(ActiveR, 3).Value
TextBox4.Value = Cells(ActiveR, 4).Value
End Sub

circular reference with data validation list in excel

I'm trying to see if it is possible to choose an item from a data validation list in either sheet 2 or 3 or 4, cell E2 to change automatically. Meaning i want the cell E2 in each of these sheets linked with each other, so if i change any one of them the other has to show the new value. The list itself is in sheet 1.
I also would like to extend this macro from just cell E2 to a range of cells (E2 to E300).
The problem i have now is if i use the code, it also changes the value of cell E2 in sheet 1. I have the code copied to modules in sheets 2, 3 4 and not in module of sheet 1.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
If Target.Address = "$E$2" Then
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = Me.Name And Not ws.Name = "Sheet1" Then
If Not ws.Range(Target.Address) = Me.Range(Target.Address) Then
ws.Range(Target.Address) = Me.Range(Target.Address)
End If
End If
Next ws
End If
End Sub
Welcome to SO. You cna handle an unique sub from Worksheet_Change event
I made up this code in that event on each worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
UPDATE_E_VALUES ActiveSheet, Target
End Sub
And the code in a module of sub UPDATE_E_VALUES is:
Public Sub UPDATE_E_VALUES(ByVal vThisWK As Worksheet, ByVal vThisCell As Range)
Application.EnableEvents = False
Application.ScreenUpdating = False
If vThisCell.Row >= 2 And vThisCell.Row <= 300 And vThisCell.Column = 5 Then 'check if cell is in range E2:E300
Select Case vThisWK.Name
Case "Sheet1"
'we do nothing
Case "Sheet5"
'we do nothing
Case Else
'we update all E cells with same address
With ThisWorkbook
.Worksheets("Sheet2").Range(vThisCell.Address).Value = vThisCell.Value
.Worksheets("Sheet3").Range(vThisCell.Address).Value = vThisCell.Value
.Worksheets("Sheet4").Range(vThisCell.Address).Value = vThisCell.Value
End With
End Select
With ThisWorkbook
.Worksheets("Sheet2").Range(vThisCell.Address).Value = vThisCell.Value
End With
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Hope you can adapt this to your needs. You can add/remove extra lines where you want sheets to be affected or not

If A1 changes, put something into B1 | If A2 changes, put something into B2

I have rows from 1-100.
I know how to target specific cells and get data from them, but how would I do this when any row from 1 to 100 can be changed?
Say you put anything into Row A3. How would you write "Updated" into row B3 via VBA?
I want this to apply to rows A1-A100.
Thanks
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, Intersection As Range, Cell As Range
Set A = Range("A1:A100")
Set Intersection = Intersect(Target, A)
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each Cell In Intersection
Cell.Offset(0, 1).Value = "Updated"
Next Cell
Application.EnableEvents = True
End Sub
Open VBA Editor
Double click on the sheet you event take action (sheets appears in the left top box)
Select Worksheet on the left box above code box
Select change on the right box above code box
Paste the code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With ThisWorkbook.Worksheets("Sheet1")
If Not Intersect(Target, .Range("A1:A100")) Is Nothing Then
Application.EnableEvents = False
.Range("B" & Target.Row).Value = "Updated"
Application.EnableEvents = True
End If
End With
End Sub

Macro to autofill a cell based on a value in a different sheet

I'm having a hard time trying to find a macro for the following use:
Taking in consideration this example:
Consider that i have in the "sheet 1" the table with the columns Country and Food with its values.
In the sheet 2, i have two columns named Country#1 and Food#1. The macro i want, needs to autofill the Food#1 cell that is associated with the right text in Country#1 cell, via the drop down list.
Example: When i select "Madrid" in Country#1, it needs to autofill the Food#1 with the text "Tapas and tortillas".
I'm sorry if this is a re-post question, but i didn't saw anything close as this :|
Best regards,
Luís
You need a Sheet Change Event like below...
The following code assumes that you have a list of Countries and their food in column A and B respectively on Sheet1 and the country dropdown list is in column A on Sheet2.
Right click the Sheet2 Tab --> View code --> Past the following code into the opened code window.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim wsSource As Worksheet
Dim r As Long
Set wsSource = Sheets("Sheet1") 'Source sheet which contains a table of countries and their food
If Target.Column = 1 And Target.Row > 1 Then
If Application.CountIf(wsSource.Columns(1), Target.Value) > 0 Then
Application.EnableEvents = False
r = Application.Match(Target.Value, wsSource.Columns(1), 0)
Target.Offset(0, 1) = wsSource.Cells(r, 2)
Application.EnableEvents = True
End If
End If
End Sub

Resources