Excel 2010: how to auto-refresh auto-filter based on checkbox cell link - vba

In Excel2010 I have created a table of X items on rows and features of these items on Y columns. One of the features is the Application Area and I want to filter the items based on the application area type. The difficulty is the items on the table have multiple application areas, so I cannot use the simple filter function of excel.
I came up with an idea to have n check-boxes (3 for this example) and assign the output of each check-box to a different cell, e.g. A10, B10, C10. I assign relevant values to OR functions for each item in an extra column, e.g. column F. So, for example, when I check the application area A the items 1 and 3 have TRUE in the column F and I can use this column to filter the table by selecting TRUE in the auto filter function. See the picture for better understanding.
The problem is that the auto filter function does not refresh automatically after I change the status of a check box. Every time I have to reapply the filter in data ribbon. Online I found this piece of VBA code to have an auto refresh feature in the active sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 8 Then
ActiveSheet.AutoFilter.ApplyFilter
End If
End Sub
Now, this code does work and the filter is reapplied, if I actively go to one of the cells in the F column and edit the cell to TRUE or FALSE manually. If I change a cell value in the F using the check box, the auto refresh does not work.
What is the difference between changing a cell value by typing the value and pressing ENTER and using a check box?
BTW, the check box is a FORM CONTROLS type, not ACTIVEX.
Any help would highly be appreciated.
Thanks!

the following will have Worksheet_Change react as if you manually changed any sheet cell
assuming:
your Form checkboxes names are "A", "B" and "C"
they are assigned macros named, respectively, "A_Click", "B_Click", "C_Click"
they are in the same sheet as the table one
the table name is "Features"
then place the following code in any module:
Option Explicit
Sub A_Click()
SetOrs
End Sub
Sub B_Click()
SetOrs
End Sub
Sub C_Click()
SetOrs
End Sub
Sub SetOrs()
Dim i As Long
Dim A As Boolean, B As Boolean, C As Boolean
Dim appAreaStrng As String
With ActiveSheet
A = .Shapes("A").ControlFormat.Value = xlOn '<~~ get CheckBox "A" value
B = .Shapes("B").ControlFormat.Value = xlOn '<~~ get CheckBox "B" value
C = .Shapes("C").ControlFormat.Value = xlOn '<~~ get CheckBox "C" value
With .ListObjects("Features")
For i = 1 To .ListRows.Count
appAreaStrng = .DataBodyRange.Cells(i, .ListColumns("App Area").Index) '<~~ get current row "App Area" value
.DataBodyRange.Cells(i, .ListColumns("OR").Index) = (A And InStr(appAreaStrng, "A") > 0) Or (B And InStr(appAreaStrng, "B") > 0) Or (C And InStr(appAreaStrng, "C") > 0)
Next i
End With
End With
End Sub

Related

find text in column in one sheet and copy row data to another sheet

I have a spreadsheet with customer information that I want to search on by last name. I want to enter the last name on a separate sheet (Sheet 1) and have the macro search the Last Name column in the customer data spreadsheet (Sheet 2). When it finds a match, I want it to copy the entire row in Sheet 2 and paste it to a specific row in Sheet 1. I've searched a number of sites and tried numerous versions of code but cannot get it to work.
Here's a link that shows you how to get data from another sheet or workbook. Basically you use Sheet_name!Cell_address or Sheet_name!First_cell:Last_cell.
Hope this helps :)
I think this sounds simple enough, loop until you find the value you want. How do you want the trigger to fire? the below into sheet 2 into the will trigger after double clicking on a selected cell in column 1, will prompt you for input, then copy the first match.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub 'or which ever column you enter for
Dim str_Act, str_Test As String
Dim i As Integer
'find value to search
str_Act = InputBox("Enter User Last Name")
If str_Act = "" Then Exit Sub
'loop to find search
Do While str_Act <> str_Test
str_Test = Sheets(1).Range("A1").Offset(i, 0) ' or whichever column has your value
i = i + 1
Loop
'Copy and paste
Sheets(1).Range("A1:ZZ1").Offset(i - 1, 0).Copy
Target.PasteSpecial
End Sub

Selecting every Nth row, Excel. Replace with nothing or original cell content

I've got an Excel sheet with my variables listed in column E and their values listed in column G
I would like to test if E contains the word "text" (my variable). If so then I want to replace the corresponding cell in column G with "This is my successful if statement text".
If not -- I want the cell to either be left alone (impossible in excel) or keep the value it originally had (I think the issue is its populated with text not numbers).
So far ive tried
=if(e2="text", "Replace with this", G2)
as well as
=if(e2="text", "replace with this", "")
The top returns a number while the bottom returns an empty cell which deletes the contents I had there.
Any suggestions? I think this can be done with VB but that's out of my league.
The proper way to solve this is as so.
In column H (or any that doesn't contain any information) place the formula
=IF(E2 = "text", "This is the true part", G2) and drag down.
This will test E2 for the word "text" and then replace with "this is true.." If the conditions are not met, the original text from G2 is pulled into the new column.
Once this is complete, the desired results should have taken effect. You can then copy the row and use "Paste Special" and select "Values" from the pop up menu to paste in your new data. Selecting Values allows the user to paste the actual field data, not the formula that generated it!
Try this.
Sub g()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1") 'change sheet name as applicable
lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
For i = 1 To lastRow
With ws
If .Cells(i, 5) = "text" Then
.Cells(i, 7) = "The text you want"
End If
End With
Next
End Sub
It seems like you are trying to get four values from column E that you want to parse (cut up) and place in Column G.
By creating four parses { =mid(e2,16,10), =mid(e3, 9, 15), =mid(e4,5,3), =mid(e5,10,22) } in cells G2, G3, G4, and G5, respectively, you can select the block of four G cells (G2:G5), select the block at the bottom right, and drag it down throughout the file.
Optionally, you can use modulo math and case statements to loop through the file and perform the required function at each point:
myCount = 0
myLoop = 0
endMyLoop = false
range("G2").activate
do
myLoop = myCount mod 4
select case myLoop
case 0
code for description_tag
case 1
code for title_tag
case 2
code for headline
case 3
code for text
end select
if activecell.value = "" then endMyLoop = true
loop until (endMyLoop = true)
You stated that every fourth row the value in E is text. So, it should just be a matter of copying the formula every fourth row or performing your function every fourth iteration (modulo returns the remainder) in the G column.
One other option would be to nest your if loops (=if(e2="text","Its text",if(e2="title_tag","Its a title",if(e2="headline","Its headline","Its description")))) to account for the four different options. Of course you would fill the text with the function that you actually want to perform.

How to have a macro search for a value in an offset cell on one sheet in another sheet

this is my first question so please bear with me if i'm doing anything wrong.
Quick back storey... on sheet "Purchase" in column C i want the item name of what has been purchased, in column D i want the quantity and in column G i want the location of where the item is going. For example i want 10 boxes of tiles delivered to the office. C2=Tiles, D2=10, G2=Office
When the location of an item is entered into column G(cell change) i want the macro to offset to the same row in column C and then search for the value in column A on sheet "Office", i currently have this for searching if a cell changes;
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
keycells = "g2:g9999"
If Application.Intersect(ActiveCell, Range(keycells)) = "Office" Then MsgBox "hello"
End Sub
This does work, but it also checks if all other cells change, which i dont want it to do this. In place of "msgbox "hello" " i want to put the script for it to offset to the same row in column C look at the value and then search for this value in column A on sheet "Office".
If it is found i need the macro to add the newly purchased quantity to the current quantity on sheet "Office" (this will be column B), if it is not found i need it add the information from columns C & D on sheet "Purchase" to columns A & B
Also i will need to add more locations to the script and will give these locations a sheet in the workbook over time
If this is possible i will be extremely gratefull for all help provided
The Worksheet_Change event fires on all cell changes.
To prevent your code running unless the cell is in column G, you could restrict it like this:
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
Dim iSect
If target.Column = 7 Then
keycells = "g2:g9999"
Set iSect = Application.Intersect(target, Range(keycells))
If Not iSect Is Nothing Then
If lcase(target) = "office" Then
MsgBox "hello"
' add code here to copy the data
End If
End If
End If
End Sub
However, a better solution might be to use formulas (such as an array formula or using INDEX and MATCH) in the target worksheets to extract the data - this will avoid using a macro entirely.

Excel Drop Down Box with Formula

Hi I have an excel with a drop down box whose list has 3 cells. One of these cells contain a formula. The problem is this formula is dependent on data in another cell and when this data changes the calculated value changes. The value is automatically update in the list where it was chosen from but I will manually have to go back to the drop down box and change it. How can I have the value be updated automatically. Willing to look at a VBA solution if need be
Put the following into the worksheet module. It assumes that the cell with validation applied is G9, and the second option of the list is the formula.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "G9" Then
If Target.HasFormula Then Exit Sub 'or else infinite loop
Dim ListRange As Range
Dim FoundIdx As Variant
Set ListRange = Me.Evaluate(Me.Range("G9").Validation.Formula1)
FoundIdx = Application.Match(Target.Value, ListRange, 0)
If Not IsError(FoundIdx) Then
If FoundIdx = 2 Then
Target.Formula = ListRange(2).Formula
End If
End If
End If
End Sub
Note that this will not work if the formula might have the same value as any of the other options!
I couldn't reproduce your issue. Here is what I did:
Populate 2 cells with a random value and a third cell with a formula (columns B3, B4, B5)
Define a Name Range with these 3 cells called it Options
Create a drop down using Insert/Form Controls/Combo Box
Set the input range of the drop down to Options
Change the cells value to get the formula give different results and the new values are reflected on the Options list and the Drop Down.
Is this what you are doing?

Excel - Lock Range of Cells Based on Values

Is it possible to lock a particular range of cells based on the input from a dropdown in a row of data?
For example, each row of my spreadsheet represents a patient, and the first cell asks a question, to which a "Yes" or "No" response is required (which is selected/entered via a dropdown).
EDIT
The "Yes/No" cell is, in fact, a merge of two cells (G13 & H13). I have updated my example to reflect this.
EDIT ENDS
If the user selects "No", then I wish to lock the remainder of the range of questions (G13-H13:AB13) as there is no need to enter data here. However, if the user selects, "Yes", then the remainder of the cells shall remain available to enter data into.
All cells within each range have data entered via dropdowns only.
Here is what I am hoping to achieve:
If "No"
Then lock range G13-H13:AB13
Else If "Yes"
Then do nothing
i.e.
G13-H13 I13-J13 K13-L13 .... .... AB13
| NO | ---- | ---- | ---- | ---- | ---- | (Locked Cells)
OR
G13-H13 I13-J13 K13-L13 .... .... AB13
| YES | | | | | | (Unlocked Cells)
Once again, I would like to emphasize that all data is entered via dropdown menus and that nothing is to be typed in manually; I would like it so that if G13-H13 = "No", then the remainder of the cells within the range which have dropdowns are blocked or locked from having further information selected from their respective dropdowns.
Please note that the value in G13-H13 can be either "Yes" or "No".
Can this be achieved using VBA and if so, how?
Many thanks.
EDIT:
You can do this without VBA. I based this on another answer here: https://stackoverflow.com/a/11954076/138938
Column G has the Yes or No drop-down.
In cell H13, set up data validation like this:
Data --> Data Validation
Select List in the Allow drop-down.
Enter this formula in the Source field: =IF($G13="Yes", MyList, FALSE)
Copy cell H13 and paste the validation (paste --> pastespecial --> validation) to cells I13:AB13.
Replace MyList in the formula with the list you want to allow the user to select from for each column. Note that you'll need to have the patient answer set to "Yes" in order to set up the validation. Once you set it up, you can delete it or set it to No.
Once you have the validation set up for row 13, copy and paste the validation to all rows that need it.
If you want to use VBA, use the following, and use the worksheet_change event or some other mechanism to trigger the LockOrUnlockPatientCells. I don't like using worksheet_change, but it may make sense in this case.
In order for the cells to be locked, you need to lock them and then protect the sheet. The code below does that. You need to pass it the row for the patient that is being worked on.
Sub LockOrUnlockPatientCells(PatientRow As Long)
Dim ws As Worksheet
Dim YesOrNo As String
Set ws = ActiveSheet
YesOrNo = ws.Range("g" & PatientRow).Value
' unprotect the sheet so that we can modify locked settings
ws.Unprotect
ws.Range("a:g").Cells.Locked = False
' lock row
Range("h" & PatientRow & ":AB" & PatientRow).Cells.Locked = True
' unlock the row depending on answer
If YesOrNo = "Yes" Then
Range("h" & PatientRow & ":AB" & PatientRow).Cells.Locked = False
End If
' protect the sheet again to activate the locked cells
ws.Protect
End Sub
You can test the functionality by using the following, manually adjusting the values for the patient row. Once you get it to work the way you want, get the row from the user's input.
Sub testLockedCells()
Dim AnswerRow As Long
AnswerRow = 9
LockOrUnlockPatientCells AnswerRow
End Sub
This code should get you started. You may need to tweak it to suit your specific needs, but I based my logic on the details in your original post.
Place the module in the respective Worksheet Object in the VBE.
Private Sub Worksheet_Change(ByVal Target As Range)
'assumes cell changed in column G and first row of data entry is 13
If Target.Column = 7 And Target.Row > 12 Then 'change to If Intersect(Target, Range("G13")) Then if all you care about is G13
Application.EnableEvents = False 'stop events from processing
Dim blnLock As Boolean
Dim r As Integer
Select Case Target.Value
Case Is = "No"
blnLock = True
r = 191
Case Is = "Yes"
blnLock = False
r = 255
End Select
Unprotect Password:="myPassword"
With Range("H" & Target.Row & ":AB" & Target.Row)
'just a suggestion, fill the cells grey before locking them, or turn them back to no fill if it's unlocked
.Interior.Color = RGB(r, r, r)
.Locked = blnLock
End With
.Protect Password:="myPassword"
Application.EnableEvents = True
End If
End Sub
Try this : -
Apply a Loop and then check
if "NO"
ws.get_Range(StartCell, EndCell).Locked = true;
// where ws is the worksheet object The only point to be seen is that if the Sheet is Locked you need to unlock it and then proceed
else if "YES"
continue;