I've got this project I'm working on where I have a "Data" sheet and am pulling the data in to "Sheet 2". I want a row on "Sheet 2" to automatically hide/unhide if there is no data within a specific cell in that row.
I am very new to macros and coding in general. I have tried looking this up but and following the tutorials on YouTube but none of them go too in-depth as to why you select certain things and I haven't had any luck getting them to work.
My question are.:
Would a macro that is effecting "Sheet 2" while I am on "Data", be placed within "Sheet 2" or within "ThisWorkbook"?
Before you write the macro, you need to select the drop downs "Workbook" and then I would assume you are detecting a change so would "SheetChange" be correct?
Will a macro detect a change within a cell that is hidden and then can that cell's row be unhidden?
Lastly, What the heck do I even write for the to detect a 0 and hide/unhide cells, I haven't gotten any of them to do anything so far?
Thank you for any help you guys can give.
To answer your questions:
Would a macro that is effecting "Sheet 2" while I am on "Data", be placed within "Sheet 2" or within "ThisWorkbook"?
It doesn't matter where you place the code unless the code is triggered by a built in event like Worksheet_Change(). In that case then it must reside within the worksheet that you are wanting to detect changes in. If it's not event driven then I would place the code in it's own module. To each their own though.
Before you write the macro, you need to select the drop downs "Workbook" and then I would assume you are detecting a change so would "SheetChange" be correct?
It would be appropriate if you wanted the code contained in that subroutine to be executed whenever there is a change to that worksheet. You could also trigger code through a user action like a button click or a double click or sheet activation, etc.
Will a macro detect a change within a cell that is hidden and then can that cell's row be unhidden?
Sure! Why not? As an example:
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if the change was in a cell we care about (it can be hidden, VBA don't care)
If Not Intersect(Target, Column(3)) Is Nothing Then
'Something changed in Column C! Unhide whatever row had the change
Target.EntireRow.Hidden = False
End If
End Sub
Lastly, What the heck do I even write for the to detect a 0 and hide/unhide cells, I haven't gotten any of them to do anything so far?
Using that last example to detect if a particular column experienced a change, detect if it was just a single cell, detect if that single cell is now a 0 and hide it's row:
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if the change was in a cell we care about (it can be hidden, VBA don't care)
If Not Intersect(Target, Column(3)) Is Nothing Then
'Something changed in Column C!
If Target.Cells.Count = 1 Then
If Target.Value = "0" Then
Target.EntireRow.Hidden = True
End If
End If
End If
End Sub
Stick that in whatever worksheet you wanting to detect the change. Change that first If so that you are only detecting changes in whatever range you anticipate that change to happen in (I chose Column C as an example).
Related
I have formatted a worksheet to need input in only specific cells. I'm wondering if it is possible to add VBA code such that when the user enters a value in cell A5 that the code automatically selects G5. When the user enters a value in G5, the code should automatically select A6.
You need to put a small bit of VBA code in the worksheet module of the worksheet you want to behave in this manner.
Open up the Visual Basic Editor and double-click the name of the worksheet you are interested in modifying under "Microsoft Excel Objects" As seen here:
Then paste in the following code so the module appears as in the picture. The "Option Explicit" directive at the top is optional.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$5" Then Range("G5").Select
End Sub
This code says: When ever a change is made to the worksheet, if the cell changed was "A5" then select G5
We all know that Excel has some counter-intuitive behaviours and it is, I believe, one of them:
When you select a range of few cells, starting your selection with the cell with data validation list and choose value from drop-down list: only one cell changes (the one containing drop-down list) instead of all selected.
Sometimes a few magic keyboard shortcuts such as CTRL+d, or combination of CTRL+' and CTRL+ENTER can fix this behaviour, but from my experience clients doesn't like to learn some new hacks, they just want to work everything in as simple way as possible.
I found even similar questions on SO e.g. here:
Adding same drop-down value to multiple cells simultaneously
I know that this is very simple code, but following few lines of code make my life easier, and I am sure this will help somebody too. Code in Worksheet module of course:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' MACRO FILLS THE WHOLE SELECTED RANGE
' WITH THE SAME VALUE USING DROP-DOWN LIST
' IN JUST ONE ACTIVE CELL
' change to false if all selected cells should be filled with value
Const FILL_VISIBLE_CELLS_ONLY As Boolean = True
' detecting if dropdown list was used
'
' I am using very clever solution by JvdV from SO
' ~~~~> stackoverflow.com/questions/56942551/
'
' If after edit we're in the same cell - drop-down list was used
' I know that may be also drag&drop or copy-paste
' but it seems no matters here.
' Warning! Should be add one more check if someone used
' 'accept OK character' next to formula bar, not implemented here.
'
If ActiveCell.Address <> Target.Address Then Exit Sub
' preventing error which sometimes occurs
If IsEmpty(ActiveCell.Value) Then Exit Sub
' fill a range or visible range with activeCell value
If FILL_VISIBLE_CELLS_ONLY Then
Selection.Cells.SpecialCells(xlCellTypeVisible) _
.Value = ActiveCell.Value
Else
Selection.Value = ActiveCell.Value
End If
End Sub
When you select a Range of more than one Cell, is is important to distinguish between the Active Cell (the single highlighted cell) and the Selection (the entire selected range, including the Active Cell).
Then:
Any Content (such as Values or Formulas) that you enter into the Formula bar is input into the Active Cell only, not the entire Selection.
Any Formatting changes you make are applied to the entire Selection, not just the Active Cell.
An exception is when entering an Array formula which applies to the Selection.
Since in this case you are making a change to content not formatting, it is applied to only the Active Cell.
When considering the above, it is not counterintuitive but entirely consistent with the design and operation of the software.
However, from a UX experience this may seem counterintuitive simply because it defies your expectation. This is kind of like a "customer is always right" type situation, which can be very frustrating for programmers, but is essential that it be understood. You can read more about the concept in a series of well-written articles on the topic at https://www.joelonsoftware.com/2000/04/10/controlling-your-environment-makes-you-happy/ (disclosure: the author of these articles happens to be integral to the development history of both Excel and SO, but it is linked here on merit). It was written more than 20 years ago and is still every bit as relevant today.
I am trying to create a table that tracks downtime for a production machine. the operator will be using a table with the columns down time start and down time stop. each time something happens that they have to leave the station I want them just to have to click the empty cell under downtime stat title and the time will appear/ log itself in the cell then the same for downtime stop.
I under stand how =now() works but then it shows the time when the file is opened, i want it to display only when the cell is selected.
Any help is much appreciated! I have never had to do anything like this in excel before.
You'll need some VBA for this. In your VBE (alt+f11) go to your worksheet and use something like the following:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check to see if the click/selected cell is in columns A or B
If Not Intersect(Target, Range("A:B")) Is Nothing Then
'Make sure just one cell is selected:
If Target.Cells.Count = 1 Then
'Update the value
Target.Value = Now()
End If
End If
End Sub
Here we are using the Worksheet_SelectionChange() event. This event will fire any time a selection change is detected on the worksheet in which this code is placed. When the change is detected it will test to see if the selection was in columns A or B. It will also test to insure that only one cell was clicked (otherwise highlighting those columns would cause every row in the column to update with the time, which would be bad). If that all passes, then it just sets the selected cell's (target) value to the current time.
I tried running the private sub code:
Private Sub Worksheet_Calculate()
Dim target As Range
Set target = Range("G3")
If Not Intersect(target, Range("G3")) Is Nothing Then
End If
End Sub
I am pulling a value from one sheet and putting it into another cell on a separate sheet. I want the VBA to run when the cell is automatically changed. When I tried the code above, nothing happened when I updated the cell.
From the sounds of things the issue seems to be that you're trying to put the code in a Module like a regular macro. To have it run based on a worksheet event, you need to have the code in that worksheet's code window (in the VBA window, there's the "Microsoft Excel Objects" folder, inside is the list of worksheets, double-click the worksheet to open it's code).
Once you've opened the worksheet's code, at the top of the window you should see two drop-downs. The left one should show "(General)". In that drop-down select "Worksheet" (should be the only option).
In the drop-down to the right of that, select "Change". Then you need to validate the Target is the right cell (If Target.Address = "$<Column letter>$<row #>"). Inside that If statement is where you'd nest your code to copy the value to the second worksheet
I have two ideas that could lead to more or less the same result.
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
I need this to work with a cell or a table. So, getting to the possible solutions...
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
You can try a worksheet change function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
To install:
1) Save your workbook as a macro-enabled file.
2) Close Excel, reopen file, and enable macro security
3) Type Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
5) In the code entry area on right (big window) paste the example code above
6) Return to your worksheet and try it.