I found this code on google which can help me to change current tab name based on a cell value of this sheet. However, I have to run this macro code manually each time. How can I modify this code to make it change automatically after entering value in a cell or at least tab's name changes as typing. Here is the code:
Sub myTabName()
ActiveSheet.Name = ActiveSheet.Range("C3")
End Sub
place this in the sheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C3" Then ActiveSheet.name = ActiveSheet.Range("C3")
End Sub
Related
I have the following piece of code on my workbook on my sheet, which is intended to test intersection using ws change and then go to the combo box and retrieve whatever value is entered in the box. However, what is happening is that after the value is entered in the combo box the first time the cell isn't updating with it's value. I have to click it again, and then it will populate. I know I have to likely use another event procedure, but I have no clue about combo box events. Can someone point me in the right direction?
Thx Mike.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aRng As Range
Dim tRng As Range
Set aRng = Range("C19:C36")
Set tRng = Sheet2.Range("I2")
Application.EnableEvents = False 'to prevent re-iteration of event
On Error GoTo cleanup:
If Not Intersect(aRng, Target) Is Nothing Then
Call Sheet2.ComboBox1_Change
Target.Value = Sheet2.ComboBox1.Value
End If
cleanup: 'enable events once again
Application.EnableEvents = True
End Sub
and on Sheet 2 where the box is.
Public Sub ComboBox1_Change()
With ComboBox1
.Activate
.SelText = Empty
.DropDown
.MatchRequired = True
End With
End Sub
To get to the basics of your question, the Combobox has a LinkedCell property. If you enter the cell address on Sheet1 there, the selected item in the ComboBox will be displayed on Sheet1. (If a value is entered in that Cell on Sheet1, it will also be displayed in the ComboBox.)
ComboBox LinkedCell
You can force Sheet1 to be displayed after a selection is made in the ComboBox with this code (in the module of Sheet2):
Private Sub ComboBox1_Change()
Sheet1.Select
End Sub
Using Worksheet_Change will only have effect AFTER something has been entered into the cell (and RETURN has been hit).
From a previous question, to dynamically rename a sheet based on a cell reference changing, you use this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C3" Then ActiveSheet.name =
ActiveSheet.Range("C3")
End Sub
But this does not work if the cell ("C3") is in sheet 1 but is itself referencing a cell on another sheet - let's say C3 is referencing "A1" in "sheet 2".
How can you change the code so when you edit A1 in sheet 2, the name of sheet 1 automatically updates?
Thanks!
Add the code below in the Worksheet_Change event in "Sheet2" worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
' only run the code if the cell being modified is in cell "A1"
If Not Intersect(Range("A1"), Target) Is Nothing Then
Worksheets("Sheet1").Name = Target.Value
End If
End Sub
Or place this code on Sheet1 Module...
Private Sub Worksheet_Calculate()
On Error Resume Next
Sheet1.Name = Range("C3").Value
End Sub
Note: Sheet1 here is the Sheet Code Name.
So each time the value of A1 changes on Sheet2, the Sheet1 will be renamed.
Below I have code that provides my Summary sheet with cell values from my Main sheet whenever there is a change in a cell on the main sheet.
This worked great, but I ended up needing to sort the main sheet whenever a new line is added so it is in alphabetical order.
When it is sorted nothing changes in the Summary tab because nothing in a cell was actually changed.
Is there a way to get the summary tab to mimic exactly what the main sheet is doing and not just cell changes.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Summary").Cells(Target.Row, Target.Column).Value = Target
Application.ScreenUpdating = True
End Sub
I would like to create a button to hide the sheet. Ideally, it would hide the sheet where the button is located.
To be simple, sheet 1 has a form to fill out. The typical name, address, phone number, etc. Sheet 2 and 3 also have the same fields that is going to be referenced to the 'Data' tab as well. On a sheet named 'Data', I will add fields that will populate simply with the = option like (=Data!...)
However, I do not want the Data page in view once the information is added. We all know the simple right click and hide sheet. But sometimes that's too much for some people that will use this sheet and a pretty button would work better.
I was successful using:
Module 1
Sub SheetCommand()
If Worksheets("Sheet1").Range("C2").Value <> vbNullString Then
Worksheets("Sheet2").Visible = False
Else
Worksheets("Sheet2").Visible = True
End If
End Sub
Sheet1 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$2" Then Exit Sub
Run "SheetCommand"
End Sub
However, I am not very VB savvy and have hit a dead end. Could someone help show me how to apply this to a button? I don't want it to reference the $C$2 field as noted on the example, but just when someone presses the button, the sheet goes away. I'm not worried about getting it back as someone can be ready to get it the old fashioned way. This would help the data entry process for this manual form so much easier.
Edit: basically, I need help creating a vba code where I can hide the page. I'd like to create a button where once clicked, it hides that page. I showed an example of a code where I got it to work but it only works if that cell is populated. How do I make it work on button click?
I found this code that does what I need but I would like to tell it a specific Sheet Name instead of having to type it in B6 and B7.
Sub ShowHideWorksheets()
Dim Cell As Range
For Each Cell In Range("B6:B7")
ActiveWorkbook.Worksheets(Cell.Value).Visible = Not
ActiveWorkbook.Worksheets(Cell.Value).Visible
Next Cell
End Sub
It's actually not very clear to me what's your exact goal
for instance, assuming "Sheet 2" and "Sheet2" would point to the same sheet, in your question you seem to reference it as being both a "Form" sheet ("...sheet 1 has a form to fill out....Sheet 2 and 3 also have the same fields...") and "Data" sheet (Worksheets("Sheet2").Visible = False)
so here follow some possible solutions:
1) you want to hide "Data" sheet before closing the workbook it's contained in
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Data").Visible = False
End Sub
2) you want to hide "Data" sheet once some cells have been filled up in ANY sheet other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name <> "Data" Then
With Sh
'here follows code to check if "the information is added"
'for instance:
If WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count Then ThisWorkbook.Worksheets("Data").Visible = False 'check if all cells in range "A2" to "C2" has been filled with some data
End With
End If
End Sub
3) you want to hide "Data" sheet once some cells have been filled up in ALL sheets other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sht As Worksheet
Dim hideBool As Boolean: hideBool = True 'set initial value as true
If Sh.Name <> "Data" Then
For Each sht In ThisWorkbook.Worksheets
With Sh
'here follows code to check if "the information is added"
'for instance:
hideBool = hideBool And WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count ''check if all cells in range "A2" to "C2" of current sheet has been filled with some data
End With
If Not hideBool Then Exit For
Next sht
ThisWorkbook.Worksheets("Data").Visible = Not hideBool ' hide if ALL sheets met "information is added" condition
End If
End Sub
4) otherwise give more info about the desired behavior
Hi there i have come across of last save options for a workbook, whereby the code allows to track the date and time of it being last modified and displayed in a cell .I was wondering if vba could allow to track the data and time of it being last modified on a specific worksheet, lets say Sheet1. So everytime changes has been made and saved in sheet 1, it would reflect the time and date saved of that sheet only. Here is the code i have for the workbook so far, tried adding .Sheets("Sheet 1") to the code but it tracks the time i visited the page and not that of i edited. These are the codes in my workbook.
Private Sub Workbook_Open()
Call starttheClock
End Sub
Sub Workbooky()
ActiveWindow.ScrollRow = 1
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Index" Then Exit Sub
Application.EnableEvents = 0
i = ActiveSheet.Index
With Sheets("Index")
.Cells(i, 1) = ActiveSheet.Name
.Cells(i, 2) = Now
End With
Application.EnableEvents = 1
End Sub
Just to add a bit more confusion to the above answer, if you do have a sheet named "Index" and want to have a date that is added to a sheet every time there is a change to it, possibly a code that will go into the workbook module. Then you will have only one code to check whenever any of the worksheets have a change.
This is where the workbook module is located and the code belongs there.
This assumes you have a sheet named "Index", name it whatever you want once you get it going properly. The actual sheet name and the sheet name in the code have to match exactly though.
Here is the code that will go in the workbook module.Copy and paste it into the Workbook Module
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Index" Then Exit Sub
Application.EnableEvents = 0
i = ActiveSheet.Index
With Sheets("Index")
.Cells(i, 1) = ActiveSheet.Name
.Cells(i, 2) = Now
End With
Application.EnableEvents = 1
End Sub
You can't save a worksheet. Always you have to save the whole workbook. That is the reason why it comes up with errors.
If you want to know the exact time and date you edited each sheet, you can find as per below.
http://www.ozgrid.com/forum/showthread.php?t=46624
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Index").Range("B2") = Now
End Sub