Update the name of Excel Worksheet dynamically- VBA excel - vba

I am trying to write a macro where i can equate the name of a worksheet to a cell value.
So far i have only been able to extract name of the worksheet and put it to a cell value.
Is there a way i can achieve the above?
Thanks

There is already a solution to this,
Follow this link
Credit to folks at Extendoffice.com
Pasting code here for reference, just follow the link you'll find details
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("A1")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Exit Sub
End Sub

You can easily achieve this with the Worksheet_Change event of the worksheet.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" Then
Target.Parent.Name = Target.Text
End If
End Sub
Every time the value of A1 changes, the worksheet name changes accordingly.
Note this procedure has to be in a worksheet scope not within a module.
It can be useful to implement an error handling for not allowed or empty sheet names.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" And Target.Text <> vbNullString Then
On Error GoTo ERR_NO_RENAME
Target.Parent.Name = Target.Text
On Error GoTo 0
End If
Exit Sub
ERR_NO_RENAME:
If Err Then MsgBox Err.Description, vbCritical, Err.Number, Err.HelpFile, Err.HelpContext
End Sub

Related

Excel VBA: How to autocreate hyperlink from cell value?

I have a table called Table1
In Column B, I have the ticket number. e.g: 76537434
Requirement: when any change happens in any cell in column B, that cell (Target cell) to be changed into a hyperlink such that the hyperlink address would be example.com/id=76537434
Cell value i.e. 76537434 must remain the same
Add this event handler to your worksheet's code module:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Then Exit Sub
Target.Hyperlinks.Delete ' or Target.ClearHyperlinks to conserve the formatting
Me.Hyperlinks.Add Target, "http://example.com/id=" & Target.value
End Sub
The following Worksheet_Change event should be able to solve your problem:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim tmp As String
If Intersect(Range("B:B"), Target) Is Nothing Then Exit Sub
For Each cell In Target
If cell.Column = 2 Then
Application.EnableEvents = False
tmp = cell.Value2
cell.Parent.Hyperlinks.Add _
Anchor:=Cells(cell.Row, 2), _
Address:="http://example.com/id=" & tmp, _
TextToDisplay:=tmp
Application.EnableEvents = True
End If
Next cell
End Sub
Note, that you must copy it to the sheet and not into a separate module.
=HYPERLINK(E14&F14,"Name")
where cell E14 contains "http://www.example.com/id=" and cell F14 contains "76537434".
This soultions doesn't need VBA macros.

Sheet specific macro will not run when condition is met

I have a very simple workbook macro for testing and it is not executing when the condition is met. Do you know why? Macros are enabled and modules are working, however, sheet codes will not work. Any idea why?
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$J$3" Then
Call macro1
End If
End Sub
And then I tried
if A1 = "correct!"
msgbox "hey"
else <do nothing>
If I adjust anything in cell J3 or putting "correct!" in A1 in that sheet, neither code will execute. macro1's code is simply msgbox "Hey". Any idea what I can do?
Thanks!
I used Workbook_SheetChange and it worked for me:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Sheet1" And Target.Address = "$J$3" Then
Call macro1
End If
End Sub
Sub macro1()
MsgBox "hello world!"
End Sub
I hope I'm understanding your post correctly, you want that once you modify a value in your worksheet at Cell J3, and the value in Cell A1 (at the same worksheet) is "correct!", that a MsgBox with "hey" will pop-up.
The code in your Worksheet_Change event is good, just modify the code in macro1 (which can be placed at another code module).
Sub macro1()
If Range("A1").Value = "correct!" Then
MsgBox "hey"
End If
End Sub
Option 2: using the Like operator
If Range("A1").Value Like "correct!" Then
MsgBox "hey"
End If

VBA - Open a UserForm by clicking anywhere in a specific column

I would like to build a makro in VBA which opens a UserForm when I click in a cell in a specific column, for more details look here.
With this code (from Mr.Burns):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A1")) Is Nothing Then
'name of userform .Show
End If
End If
End Sub
I was able to open the UserForm by clicking in the cell A1, but not by clicking in any cell inside the column A.
I tried to solve this problem with this code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
Dim check As Boolean
check = True
If check Then
Dim i As Long
For i = 1 To 100000
If Not Intersect(Target, Range("A" & i)) Is Nothing Then
UserForm1.Show
check = False
End If
Next
End If
End If
End Sub
It actually works fine, but it is very slow, is there any better possibility to solve this?
To display the form when a cell is selected in column A:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' if target is one cell and in column A
If Target.Columns.count = 1 And Target.Rows.count = 1 And Target.Column = 1 Then
UserForm1.Show
End If
End Sub
You can use .count and .column property together with AND and it will become so much simple and fast. Following code triggers pop-up if u click in column A on active-sheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo errorhandler
If Target.Count = 1 And Target.Column = 1 Then '.count to check if only one cell is selected and .column to check if it is a first column
'UserForm1.Show
'Do whatever you want to do here like opening User form
MsgBox "You clicked in column A"
End If
errorhandler:
End Sub

excel vba on cell change error

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Range = Range("A1") Then
If Range("A1").Value <> Range("A2").Value Then
Range("C1").Value = Range("C1").Value + 1
Range("A2").Value = Range("A1").Value
End If
End If
End Sub
that's the code however when i copy paste a set off cell, let's say 2 columns and 3 rows
it produce runtime error 13 type mismatch on line
If Target.Range = Range("A1") Then
why?
i simply wants the vba to do something everytime cell A1 changes
the value of A1 itself is an excel sum formula
You get type-missmatch error, becase you're trying to compare range (containing many cells) with single cell. If you want to do something every time cell A1 changed, use this one instead:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error GoTo ErrHandler
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Range("A1").Value <> Range("A2").Value Then
Range("C1").Value = Range("C1").Value + 1
Range("A2").Value = Range("A1").Value
End If
End If
ExitHere:
Application.EnableEvents = True
Exit Sub
ErrHandler:
Resume ExitHere
End Sub
also note that I'm using Application.EnableEvents = False - it's a good habbit for Worksheet_Change event to use it. It prevents code from infinity firing itself each time you change any cell in event handler code.
UPD:
Btw, the value of A1 itself is an excel sum formula - you can't track changes of formula using above approach. I covered in details how you can do it in this question: Using Worksheet_Calculate event for tracking changes of formula
Simoco's answer should work for you. Another way (the one I usually use, though only out of habit) is to compare the addresses:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
If Range("A1").Value <> Range("A2").Value Then
Range("C1").Value = Range("C1").Value + 1
Range("A2").Value = Range("A1").Value
End If
End If
End Sub
You are getting an error because Target.Range is not defined. You should either just refer to Target (a Range Object) or Target.Address (the address of the Range Object). Secondly, depending on the context, Range("A1") refers to either the cell A1 itself (a Range Object) or the value in cell A1 (a literal value). You need to carefully think what you want to compare to what.
If, as you said, you want the comparison done whenever the value in Range("A1") changes then you should follow Simoco's suggestion.

An excel macro to switch worksheets based on the values placed anywhere in a column range

I'm trying to create a macro where it will switch from Sheet1 to Sheet2 when the word "Yes" is typed in any cell in column A. The closest I've come to getting this to work is with the code below that switches worksheets when "Yes" is typed in a specific cell.
Sub ifs()
If Worksheets("Sheet1").Range("A1").Value = "Yes" Then
Sheets("Sheet2").Select
Else
End If
End Sub
Like I mentioned that you can create a hyperlink in Cell A1 which will directly take you to Sheet2 but still if you want vba code then try this. This code will go into the sheet code area of Sheet1
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Target.CountLarge > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Columns(1)) Is Nothing And _
UCase(Target.Value) = "YES" Then _
ThisWorkbook.Sheets("Sheet2").Activate
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
NOTE: If you are interested in the Hyperlink approach then THIS is worth visiting. Checkout the section Create a hyperlink to a specific location in a workbook
Sub ifs()
If WorkSheetFunction.CountIf(Worksheets("Sheet1").Range("A:A"),"Yes") > 0 Then
Sheets("Sheet2").Select
End If
End Sub