How to direct jump on specific Cell in Excel - excel

Good Morning/Afternoon/Evening to all :)
My First time on here so.................... :)
Spreadsheet Example
I use this code in "Conditional Formatting"
=AND(ISNUMBER(SEARCH($B$3,A6)),$B$3<>””)
so i only able to highlight the cell as on Screenshot (Whenever type text on B3 it's automatically highlight the contain Cell as on Screenshot) but what I want is it's only highlight the cell but cannot jump (select) cell when i type ""text"" on Search B3 box.
For Example: On Screenshot I type DW353 and It's highlight in Redcolor on A18 Cell No. but if i type other text as DW364, 365 and on which located on A24, A25 and continue to down, it's only highlight the specific Cell and I have to Search (Scroll up and down) for look that RED Highlight. What i want is whenever i type a "Text" on B3 it's Highlight and direct Jump on Contain Cell as (Find and Replace).
Thank you in Advance and Sorry for Long Question :)

The easiest way to accomplish this kind of functionality is to use VBA to trigger the Advanced Filter functionality, so that all other rows are hidden.
Here's how:
Add a Named Range called "MyList" covering the entire range of data in the MACHINE NO: column that you want to filter. (Best if you turn this block of data into an Excel Table, and then just reference the MACHINE NO column, as this will mean the named range is dynamic i.e. it will automatically adjust should the underlying data grow).
Put the text "MACHINE NO:" in B2
Add a Named Range called "Criteria" covering B2:B3
Add a Named Range called "Input" covering B3
This should look something like the below:
Put this code in the Sheet Module that corresponds to the Per Machine Movement tab (and NOT in a standard Code Module) :
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Input")) Is Nothing Then
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
Range("MyList").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Criteria"), Unique:=False
End If
End Sub
End Sub
i.e. like so:
Now, any time someone enters a Machine Number, the sheet will be filtered to show just the row of interest:
You can find a working example at Daniel Ferry's excellent blog, at the following link:
http://www.excelhero.com/blog/2010/07/excel-partial-match-database-lookup.html
Look for the second sample file he posted under the heading --- UPDATE ---
This approach can be tweaked to search across multiple columns, as per your follow-up question. First, here's the setup of the Named Ranges (including a new one above the input cell called "Header"):
...and here's the amended code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vRanges As Variant
Dim vItem As Variant
If Not Intersect(Target, Range("Input")) Is Nothing Then
On Error GoTo errhandler
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlManual
End With
'Clear any existing filter
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
'Cycle through the search arrays one by one, and run the advanced filter until you find a match
vRanges = Array("Range1", "Range2", "Range3", "Range4") '<<< Change these to match your range names
For Each vItem In vRanges
Range("Header") = Range(vItem).Cells(1)
Range(vItem).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Criteria"), Unique:=False
If Range(vItem).SpecialCells(xlCellTypeVisible).Count > 1 Then Exit For
Next vItem
errhandler:
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlAutomatic
End With
End If
End Sub
And here's a couple of screenshots showing it matching on different columns:
You can format cell B2 so that it can't be seen if you want.
I would suggest turning the different input areas in your workbook into Excel Tables (also known as ListObjects) before you set up the "RangeX" named ranges. That's because under the hood, Tables are basically dynamic named ranges that automatically expand to accommodate new data. So if you then manually set up a Named Range that points at a Table column, you never need to remember to adjust your Named Ranges in order to handle new data, because the Table automatically does this for you. See the image below:
Note that you can change the formatting of Tables using the Table Styles option in the ribbon, or even turn off the formatting entirely:

Related

Excel VBA: changing visible cells based on names cell value. Works once then not again

I am trying to get a spreadsheet to show a set of names rows called authorisations when there is no project number in the project ref cell but if a project number is added to the cell the authorisations are not required and should disappear.
If the cell is blank and the row visible then when project reference is added the rows disappear but if I then delete that reference and change cell they do not reappear.
Private Sub Worksheet_Change(ByVal Target As Range)
If Project_Ref = "" Then
Range("authorisations").EntireRow.Hidden = False
Else
Range("authorisations").EntireRow.Hidden = True
End If
End Sub
I have tried a search online and found comments that adding Application.EnableEvents = True before the End sub should help but it didn't seem to make a difference. I am now wondering if Worksheet_change in fact operates as I thought it did where it would look for any changes in the sheet and then execute the code if required.

Excel - drop down menu changes with certain input in another cell

I have two cells for drop-down menu. Cell A1 is for brand name and B1 is for model name. I want that if in cell A1 Samsung is selected that in cell B1 only Samsung models to be enable. All the brand and models are listed in separated sheet.
Please let me know how to implement this as I am new to excel.
The solution is actually simpler than what you may think. I’ve made 2 suggestions below, the first doesn’t require VBA at all – the second one does. In both cases, you must start by correctly naming the Ranges where the model names are listed.
Name the Ranges (must be done for both options)
Using Samsung as the example, select the cells where all the Samsung model names are listed (must be contiguous) and name the range Samsung. Repeat for all the other model listings by brand name. You say that All the brand and models are listed on separated sheet so you know how to do this.
Option One – formula only
In cell B1, set the Data Validation as such: Allow: = List. Source: = =INDIRECT($A$1)
Now, when you change the selection in cell A1, the Source of the Data Validation changes to that brands' model list via the named range.
Pros: doesn’t require VBA. Simple to implement.
Cons: INDIRECT() is a Volatile function. When you change brands in A1, the last model used is left in cell B1 (until you change it) which may interfere with any formulas that reference the cell.
Option Two – VBA
Place the following code in the Worksheet Module area of the sheet where your Data Validation cells are.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo GetOut
Application.EnableEvents = False
Dim MyList As String
If Not Intersect(Range("A1"), Target) Is Nothing Then
With ActiveSheet.Range("B1")
.ClearContents
.Validation.Delete
MyList = Sheet1.Range("A1").Value
.Validation.Add Type:=xlValidateList, Formula1:="=" & MyList
End With
ActiveSheet.Range("B1").Value = ActiveSheet.Range(MyList).Cells(1, 1).Value
End If
Continue:
Application.EnableEvents = True
Exit Sub
GetOut:
MsgBox Err.Description
Resume Continue
End Sub
Pros: replaces the value in B1 with the first valid model number for that brand.
Cons: uses VBA so the file needs to be saved as a macro-enabled file type
Let me know how you go.

Data validation list is being ignored

I have the following situation: on one cell, I have a Data - validation - list which should restrict the user to selecting only items from that list.
On the same cell I have on change triggers in VBA, so when the cell value is changed it also records the change into a Changelog sheet (it uses undo to get the previous value, and undo again to redo the change to the new value)
The problem is, right now, the Data - validation - list is being totally ignored, so the users can put what ever they want in that specific cell, even though I have "Show error" checked.
Is there a way to enforce the list validation, so the users can only select items from the list and not enter whatever they want? Or to trigger the on cell change event after the validation?
Maybe someone can clarify the order in which these things happen.
I recently worked on range.Validation.type=xlValidateList and wanted to prevent paste and cut and disallow typing wrong characters into the cell containing the dropdown list(and making it possible to type characters that match an entry in the list and show that entry).
While working on the last thing that was not OK (cutting a dropdown and pasting it into another cell already containing a dropdown) I discoverd an EASY way of prohibiting paste and cut for Data Validation Lists that do allow further processing depending on what has been entered,
the following code has to be on the worksheet module.
It prevents the user from destroying Data Validation on cells and because of that using Application.Undo to revert to the state before pasting or cutting is no longer needed.
I don't know if it makes a difference but I protected the sheet and unlocked the cells the user may alter.
Private Function HasValidation(ByVal rng As Range) As Boolean
' See: https://superuser.com/questions/870926/restrict-paste-into-dropdown-cells-in-excel
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
Dim rngType: rngType = rng.Validation.Type
HasValidation = (Err.number = 0)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Switch DragAndDrop on/off on a range containing DataValidation
Dim modeCutCopy: modeCutCopy = Application.CutCopyMode
Dim usesValidation: usesValidation = HasValidation(Target)
If ((usesValidation = True) And (modeCutCopy <> 0)) Then Application.CutCopyMode = False ' MUST reset to avoid copying / cutting xlValidateList into another xlValidateList
' Turn DragAnddDrop on/off depending on if a cell uses Validation or not
If (usesValidation = Application.CellDragAndDrop) Then Application.CellDragAndDrop = (Not usesValidation)
GoTo SKIP
' Don't allow DragAndDrop if Target intersects with specified ranges
Dim myRange As Range
Set myRange = Worksheets("Evenementen").Range("Oordeel", "Waardering")
Dim rngIntersect As Range
Set rngIntersect = Application.Intersect(Target, myRange)
Application.CellDragAndDrop = (rngIntersect Is Nothing)
'' CheckInputValidationList(Target) ' Sophisticated testing and actions depending on the selection made
'' CheckInputValidationList(Target, myRange) ' Sophisticated testing and actions depending on the selection made for specific ranges
SKIP:
Debug.Print "SelectionChange " & Target.Address & " usesValidation=" & usesValidation & " cellDragAndDrop=" & Application.CellDragAndDrop
End Sub
In this case there are two named ranges containing (different) Data Validation Lists.
As soon as the user clicks on a cell a check will be performed if that cell uses data validation.
If so cut and paste will be turned off and CutCopyMode will be cleared until the user selects another cell.
Important is to retrieve the current CutCopyMode BEFORE any VBA-code will be executed that changes something as VBA will automatically change CutCopyMode from xlCut or xlCopy into 0 when something changes.
That initiall state is required to avoid pasting a dropdownlist over another.
The Worksheet_SelectionChange subroutine contains SKIPPED code that does something similar if a user accesses a cell in one of two ranges.
It also contains the (turned into comment) code to perform additional actions when the user selects a cell. That code might also be put in worksheet_Change

VBA code to not allow user to select a cell with certain text

I am working on a spreadsheet to copy a users data from the previous day, then delete any numerical values but keep cells with "NA". What I want is for users to not be able to change/delete the cells that still have "NA" in them. I found some code that used OFFSET to move down one cell if a certain cell was selected (based on the row and column) but I haven't been able to figure out how to use the OFFSET to move down one cell if the current cell contains "NA". (https://www.extendoffice.com/documents/excel/3820-excel-lock-cell-without-protecting-sheet.html) This worksheet is already locked with a Quality-set password, so I can't do anything to unlock the spreadsheet, then select the "NA" cells to be locked, then relock the spreadsheet, thus looking for a creative way to keep the cells from being selected or changed. Also, the code would need to run all the time, not just when a macro was selected to run. Any ideas?
If it's possible for the user to open the book without macros enabled, then I'm not sure what you're asking is possible.
If you can assume macros are enabled though, you could use events to either prevent the user selecting the cell (similar to the OFFSET you mention) or you could track changes manually onto a hidden tab in order to note changes and deal with them as you see fit. There are many ways you can achieve the latter, just search "VBA tracking changes to a sheet" etc.
This is how you'd use the OFFSET method:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells(1, 1).Text = "NA" Then
Beep
Cells(Target.Row, Target.Column).Offset(0, 1).Select
End If
End Sub
Keep in mind though, this is a very simplistic method. It won't prevent users selecting multiple cells (a range) and deleting the contents. Nor will it prevent values being pasted to range that includes the 'NA'.
UPDATE:
The following is an improved version that will at least prevent users from selecting multiple cells (for pasting into or deleting) if one of the cells contains "NA".
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim testarea As Range
Set testarea = Intersect(UsedRange, Target.Cells)
If Not (testarea Is Nothing) Then
Application.EnableEvents = False
For Each cell_to_check In Intersect(UsedRange, Target.Cells)
If cell_to_check.Text = "NA" Then
Beep
Cells(cell_to_check.Row, cell_to_check.Column).Offset(0, 1).Select
Do Until Selection.Text <> "NA"
Selection.Offset(0, 1).Select
Loop
Exit For
End If
Next
Application.EnableEvents = True
End If
End Sub
This is still slightly flawed however, as it is still possible to drag-fill cells from other areas over a cell containing "NA".

Moving Rows to another sheet in a workbook

I need Help!
I am not well versed in VBA or Macros but i cannot find any other way to accomplish what i need to do without using it.
I have a sheet which i will be using to track Purchase orders, and what i need to do is; when i have a row in sheet 1 (Purchase Orders) which has been recieved i.e. the date of receipt has been recorded in column H i need for the entire row to be cut and pasted into sheet 2 (Received orders).
The header takes up the first 7 rows the rows, so i need the macro to look at rows 8-54. Once the received items are removed from sheet 1, i need the row to also be deleted or preferably for the list to be sorted by column A moving the now empty row which has been cut from open for a future entry.
Any help would be greatly appreciated.
The "Record Macro" feature should be enough to do the task you describe.. In Excel 2007, go to the Developer tab in the Ribbon, and select "Record Macro", and perform exactly the steps you are describing. It will record the equivalent VBA code, which you can then execute - or tweak/modify.
I tested this out, here's one way to do it:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim receivedDate As Range, nextOpen As Range, isect As Range
Set receivedDate = Sheet1.Range("H8:H54")
Set isect = Application.Intersect(Target, receivedDate)
If Not (isect Is Nothing) And IsDate(Target) = True Then
Set nextOpen = Sheet2.Range("A1").End(xlDown).Offset(1, 0)
Target.EntireRow.Copy Destination:=nextOpen.EntireRow
Target.EntireRow.Delete
End If
Application.EnableEvents = True
End Sub
This would be pasted into the Sheet1 code. Any time a cell is changed on sheet1, the code checks to see if it's in the critical range that you specified. (H8:H54) If it is, it then checks to see if it's a date. If it is, it then copies the entire row, puts it in the next open row on Sheet2, and deletes the original row. The cells below it will get shifted up so there are no gaps.
Since the code functions on a cell changing event, it disables "Application.EnableEvents" in order to avoid a loop of changing a cell to call an event which changes a cell to call an event... etc.

Resources