I have a formula that's part of a conditional formatting rule that highlights in green any cell that contains whatever text I type. It works great but I have to scroll to find the row. Is there a way to have the formula goto that cell so I don't have to scroll down.
This is the formula: =SEARCH(input,$B2&$C2&$D2&$E2)
In MS Excel, there's possibility find and select cells that meet specific conditions, here's the manual on office support.
If you know how to use Excel VBA, you may create the script, which will do the job, and to bind it to some shortcut or button:
ActiveWorkbook.Worksheets("Your sheet").Columns(1).Find("Text to search").Select
And the third possibility to "go to specific cell" which I've found is described here.
Possibly you may adapt for your needs CHOOSE function, which does the selection of up to 254 values based on the index number.
Not a formula per se, but if it is the only conditional fomatting, you could hit F5, click "special", select "conditional formats" and click "ok"
Here is the solution I got from the Microsoft Office forums.
Public Sub FindRecords()
Dim sFalse As String
Dim iDay As String
Dim i As Long, j As Long
sFalse = "False"
iDay = InputBox("Enter the value you want to scroll to:")
' Test if input box is empty or cancel is pressed
If iDay = vbNullString Or iDay = sFalse Then GoTo ExitOut
i = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo Notfound
j = Range("A3:A" & i).Find(what:=iDay, after:=Range("A3")).Row
ActiveWindow.ScrollRow = j
Exit Sub
Notfound:
MsgBox "Your value was not found."
Exit Sub
ExitOut:
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Related
I have two worksheets ("Worksheet A" and "Worksheet B").
Worksheet A has a list of tests. Each row represent a test performed.
Column F has the value "Passed" or "Not Passed".
If "Not Passed", the entire row needs to be added automatically to "Worksheet B" which represent an action list.
Any suggestion on how to do this dynamically, i.e. without having to run a macro manually?
Use worksheet events. Alt + F11 to open VBA interface:
Double click "Sheet1" on the left hand side of the interface and paste this in:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 0 Then
update_sheet
End If
End Sub
Below where it says "Sheet1", right click on "Modules". Hover on insert and click "Add Module". Paste the following code into the module:
Sub update_sheet()
s1_rows = ThisWorkbook.Worksheets(1).Cells(ThisWorkbook.Worksheets(1).Rows.Count, "F").End(xlUp).Row
Set s1_rng = ThisWorkbook.Worksheets(1).Range("F1:F" & s1_rows)
s2_rows = 1
For Each cell In s1_rng
If cell.Value = "Not Passed" Then
cell.EntireRow.Copy
ThisWorkbook.Worksheets(2).Range("A" & s2_rows).PasteSpecial xlPasteValues
s2_rows = s2_rows + 1
End If
Next cell
Application.EnableEvents = True
End Sub
This can be improved, but should start you off.
I'm currently working on a line of code that puts checkboxes in every row on Column A (Every row but row A1, so it starts at A2) in Excel. These checkboxes are connected to their neighbour Column B where it puts its true or false data.
Now my goal is:
If column B (decided by checkbox) is true, then copy the checkbox row location cell C value to the checkbox row location cell D. When this is not true it has to put a 0 in the target cell (D).
This line of vba is activated as soon as a checkbox will be clicked.
My current code is:
Sub TrueFalse()
If Range("B" & ActiveRow).Value = True Then
MsgBox "True"
ElseIf Range("B" & ActiveRow).Value = False Then
MsgBox "False"
End If
End Sub
I know "ActiveRow" doesn't work but it is to show where the rowcheck should be.
Hope someone can help, thanks in advance!
Its not clear to me the reason you are using check boxes in this way?
You could achieve something similar using data input validation instead and avoid the hassle of using forms for the checkboxes.
E.g. allow only two values in the range of cell A2:A99 (e.g. "Y" or "N"), or possibly only allow one value and an empty cell (e.g. "X" or blank). Then use VBA to detect data entry into column "A" and use an offset from the active cell to do the work e.g.
if Activecell.value="X" Then
Activecell.offset(0,1) = "whatever you want to write to the cell right of the checked cell"
Is this the sort of thing you are try to do?
If the Checkbox is connected via the Cell Link, i.e Format Control | Control Tab | Cell Link then you can get the row of the relevant checkbox using
Sub TrueFalse()
Debug.Print Range(ActiveSheet.CheckBoxes(Application.Caller).LinkedCell).Row
End Sub
So your code becomes
Sub TrueFalse()
Dim ActiveRow As Long
ActiveRow = Range(ActiveSheet.CheckBoxes(Application.Caller).LinkedCell).Row
If Range("B" & ActiveRow).Value = True Then
MsgBox "True"
ElseIf Range("B" & ActiveRow).Value = False Then
MsgBox "False"
End If
End Sub
Edit
You can also change the above to work like
Sub TrueFalse()
Dim CellAddress As String
CellAddress = ActiveSheet.CheckBoxes(Application.Caller).LinkedCell
If Range(CellAddress).Value = True Then
MsgBox "True"
ElseIf Range(CellAddress).Value = False Then
MsgBox "False"
End If
End Sub
The following VBA script for an Excel sheet allows an individual to type text into a cell and then automatically jump to the next cell in the array once they hit enter.
However, this requires someone to type into every cell for the script to advance to the next cell in the array below and does not recognize tab, only enter.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tabArray As Variant
Dim i As Long
tabArray = Array("B5", "C6", "D7", "E8")
Application.ScreenUpdating = False
For i = LBound(tabArray) To UBound(tabArray)
If tabArray(i) = Target.Address(0, 0) Then
If i = UBound(tabArray) Then
Me.Range(tabArray(LBound(tabArray))).Select
Else
Me.Range(tabArray(i + 1)).Select
End If
End If
Next i
Application.ScreenUpdating = True
End Sub
I'd like to let the person hit tab or enter if they want to skip the given field and advance without typing anything in the cell.
You can also just protect the sheet and have the input cells unlocked.
Right Click on the Cell, choose "Format cells".
Then, go to Protection Tab, and uncheck "Locked".
Once the input cells are unlocked, go to the "Review" Ribbon, and click on Protect Sheet. Uncheck "Select locked cells" and it should only allow you to click on the cells that are unlocked, and can press Tab repeatedly to go to the next available cell.
Protecting the sheet and unlocking the data-entry cells as suggested by #Basher is probably a better solution IMO, but this sort-of works. Couple of notes:
To "break into" the tab sequence you need to select one of the data-entry cells
To "break out" of the tab sequence (so you can select some other cell maybe) you can make a multi-cell selection.
Code:
Dim lastCellAddress As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tabArray As Variant
Dim i As Long, addr, mCurr, indx, mPrev
If Target.Cells.CountLarge = 1 Then
tabArray = Array("B5", "C6", "D7", "E8")
addr = Target.Address(False, False)
mCurr = Application.Match(addr, tabArray, 0)
If IsError(mCurr) Then 'current selection isn't a data entry cell
If lastCellAddress <> "" Then
'was user previously in a data entry cell?
mPrev = Application.Match(lastCellAddress, tabArray, 0)
If Not IsError(mPrev) Then
'mPrev is 1-based but tabArray is 0-based...
indx = IIf((mPrev - 1) < UBound(tabArray), mPrev, 0)
On Error GoTo haveError
Application.EnableEvents = False
Me.Range(tabArray(indx)).Select '<< select the next entry cell
Application.EnableEvents = True
End If
End If
End If
lastCellAddress = Selection.Address(False, False)
Else
lastCellAddress = "" 'breaks out of the sequence
End If
Exit Sub
haveError:
Debug.Print Err.Description
Application.EnableEvents = True
End Sub
You don't need VBA to achieve this task - it's a built-in Excel functionality.
Simply select your data entry range and after inputting the desired value into the first cell hit TAB, it will take you to the next highlighted cell - either to the right of the current one or to the next row if you were at the end of the row.
I am trying to finish this macro that will open another workbook, copy, and paste the selected cells into the other workbook. I want to do this only when the selection is in column A and only when the selection contains data.
The if the selection contains data part is easy, however how can I make a statement that says if selection is not within column A, then MsgBox("Please select data")?
Here is the if statement so far, it still needs the part mentioned directly above.
'Warns if no QN#s are selected
If Selection = "" Then
MsgBox ("Please Select Your QN#s Before Running This Macro")
Exit Sub
End If
This seems to work for recognizing if the cells are within a certain column.
If Not Intersect(Selection, Range("A:A")) Is Nothing Then
'Proceed
Else
MsgBox ("Please Select Your QN#s Before Running This Macro")
Exit Sub
End If
Why not use a Worksheet_SelectionChange?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column <> 1 Then
MsgBox "Please Select Your QN#s Before Running This Macro", vbOKOnly, "Data Selection"
Else
RunMacro
End If
End Sub
Use this pattern of codes. This is just dealing with the selection and not detecting changes.
Dim a As Integer
Dim b As Integer
Dim c As String
a = Selection.Row
b = Selection.Rows.Count - 1
c = "A" & a & ":" & "A" & a + b
If ((Selection.Column = 1) And (WorksheetFunction.CountA(Range(c)) > 0)) Then
MsgBox "ready to go"
End If
In the above code the hidden rows with data will be considered.
Merged cells are considered as one cell with value and others are blanks( example: When you have merged A2:A5, then you have 3 blank cells)
Is there a simple way to do this, via macro or otherwise? By calculated field I mean a field that is computed from other fields, versus raw entered values. By highlight I mean colored differently. I need this to better understand a large spreadsheet from a client.
To do it manually, press the F5 key to bring up the GoTo dialog. Click the Special Cells button. On the next screen, select Formulas (it's an option on the right).
Excel will select all of the cells that match. Now it's just a matter of applying formatting.
I'm going to assume you're only talking about cell formulas rather than VBA calculations here, since you could set the cell colour in your VBA procedure if you're doing it that way.
The way to do this is to check the cell for a formula after you're done with it, and change it's colour at that point. The relevant event here is Change, and the cell's HasFormula property will tell you whether the cell is a literal value, or calculated from a formula:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.HasFormula Then
Target.Interior.Color = vbRed
Else
' remove background colour entirely (i.e. No Fill)
Target.Interior.ColorIndex = xlColorIndexNone
End If
End Sub
TLDR;
Use Conditional Formatting with a Formula to highlight all cells that contain a formula.
Details
In MS Office 365 Version: 5.0.4667.1002, the following works
Select a range of cells.
Case1: Use Ctrl + A to select all cells.
Case2: Select a specific range.
Go to the Home tab, Styles section, and choose Conditional Formatting > New Rule.
The "New Formatting Rule" dialog will open.
Choose "Use a formula to determine which cells to format"
In the textbox, add the following rule: =IsFormula(A1)
Case1: If you selected all cells, use A1 because it is the first cell.
Case2: If you selected a specific range, replace A1 with the first cell in your range.
Click Format...
The "Format Cells" dialog will open.
Choose the format you would like to apply. E.g. a yellow background.
Click OK.
All cells that have formulas will now have, for instance, a yellow background.
Screenshot
Excel has a built in feature of "Trace Dependents" (which shows arrows to show you the calculated cells)
Select the range containing your data.
Excel 2007 -> Formulas -> Trace Dependents
The code below should cycle through each sheet, highlighting every cells that starts with an '=' and colors it the desired color (currently colour 36 which is Light Yellow).
Sub HighLightFormulas()
Dim objSheet As Worksheet
Dim strOriginalSheet As String
Dim intMaxBlankCells As Integer
Dim intBlankColumns As Integer
Dim intBlankRows As Integer
Dim intCurrentColumn As Integer
Dim intCurrentRow As Long
intMaxBlankCells = 40
strOriginalSheet = ActiveSheet.Name
For Each objSheet In Worksheets
intBlankRows = 0
intCurrentRow = 1
intCurrentColumn = 1
Do While intCurrentRow <= 65536 And intBlankRows <= intMaxBlankCells
intBlankColumns = 0
intCurrentColumn = 1
Do While intCurrentColumn <= 256 And intBlankColumns <= intMaxBlankCells
If Left(objSheet.Cells(intCurrentRow, intCurrentColumn).Formula, 1) = '=' Then
objSheet.Cells(intCurrentRow, intCurrentColumn).Interior.ColorIndex = 36
End If
intCurrentColumn = intCurrentColumn + 1
Loop
If intCurrentColumn = intBlankColumns Then
intBlankRows = intBlankRows + 1
Else
intBlankRows = 0
End If
intCurrentRow = intCurrentRow + 1
Loop
Next objSheet
Worksheets(strOriginalSheet).Activate
Call MsgBox("The Highlighting process has completed", vbOKOnly, "Process Complete")
End Sub
It will also stop after 40 consecutive blank cells (to avoid processing all of a mostly blank sheet).
Hope this helps.
Simple solution:
Ctrl - ` (the key just above Tab)
You can use the Interior.ColorIndex property to change the active cell's background color:
ActiveCell.Interior.ColorIndex = 36
You may also apply it to a range:
Range("A1:A5").Interior.Color = RGB(200,160,35)
This applies to Excel 2003, I haven't used the latest version but I doubt this has changed.
You can usually record a macro and then look at the generated code to see how something is done.
I liked Craig's code here, because it keeps the layout of the existing worksheet and yet shows what is calculated and what is not 'at a glance', but I have reworked it a bit so it does a better job of working out the active area of sheets, and I added an 'UnhighlightFormulas' subroutine so one can easily undo the formatting (e.g. before printing). It has been tested in Excel 2007. Note that you will lose any other cell background colouring upon running this.
Option Explicit
Public Sub HighlightFormulas()
ColorFormulas (36) '36 is yellow
End Sub
Public Sub UnhighlightFormulas()
ColorFormulas (-4142) '-4142 is default
End Sub
Private Sub ColorFormulas(intColor As Integer)
Dim wshSheet As Worksheet
Dim rngRange As Range
Dim rngCell As Range
For Each wshSheet In Worksheets
Set rngRange = RangeInUse(wshSheet)
If Not rngRange Is Nothing Then
For Each rngCell In rngRange
If Left(rngCell.Formula, 1) = "=" Then
If rngCell.Interior.ColorIndex <> intColor Then rngCell.Interior.ColorIndex = intColor
Else
If rngCell.Interior.ColorIndex <> -4142 Then rngCell.Interior.ColorIndex = -4142 '-4142 is default
End If
Next
End If
Next
End Sub
Private Function RangeInUse(ws As Worksheet) As Range
Dim LastRow&, LastCol%
' adapted from http://www.beyondtechnology.com/geeks012.shtml
' Error-handling in case there is no data in worksheet
On Error Resume Next
With ws
LastRow& = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
LastCol% = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
End With
Set RangeInUse = ws.Range("A1", Cells(LastRow&, LastCol%))
End Function