Macros for this post
https://excelfox.com/forum/showthrea...ll=1#post13456
Add Workseets from names list, for example from :
_____ Workbook: DynamicWorksheetNamesLinkHideBasedOnCellValueC.xls m ( Using Excel 2007 32 bit )
Worksheet: Master Sheet
Row\Col B C D 3 4ANUJ 5RITA 6MUKESH 7RAM 8RAHIN 9Anshu 10
Code:' _1. I want to create tabs (Sheets) on the basis of names. https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13456&viewfull=1#post13456 https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13445#post13445 Sub AddWorksheetsfromListOfNamesC() ' https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13456&viewfull=1#post13456 https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13445#post13445 https://www.mrexcel.com/board/threads/vba-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-value.1135674/ Rem 0 On Error GoTo Bed ' If we have problems then we want to make sure that we still re enable Events coding before ending the macro Let Application.EnableEvents = False ' This will prevent anything we do in this macro from causing erratic working of any automatic event coding Rem 1 worksheets 1 info Dim Ws1 As Worksheet Set Ws1 = ThisWorkbook.Worksheets.Item(1) ' or Worksheets("Master Sheet") ' first worksheet counting tab from the left is worksheets item 1 Dim Lr1 As Long Let Lr1 = Ws1.Range("C" & Ws1.Rows.Count & "").End(xlUp).Row ' Range("A" & Ws1.Rows.Count & "").End(xlUp).Row ' http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11466&viewfull=1#post11466 Making Lr dynamic ( using rng.End(XlUp) for a single column. ) Dim arrNmes() As Variant Let arrNmes() = Ws1.Range("C4:C" & Lr1 & "").Value2 ' Range("A1:A" & Lr1 & "").Value2 Rem 2 Add and name worksheets from list Dim Cnt As Long For Cnt = 1 To UBound(arrNmes(), 1) ' From (2,1) To (2,Lr1) in names array list column 1 ( Column A ) Worksheets.Add After:=Worksheets.Item(Worksheets.Count) Let ActiveSheet.Name = arrNmes(Cnt, 1) Next Cnt Worksheets.Item(1).Select Bed: Let Application.EnableEvents = True End Sub '
Add hypelinks to Worksheets
Code:Sub AddHypolinkToWorksheet() Rem 0 On Error GoTo Bed ' If we have problems then we want to make sure that we still re enable Events coding before ending the macro Let Application.EnableEvents = False ' This will prevent anything we do in this macro from causing erratic working of any automatic event coding Rem 1 worksheets 1 info Dim Ws1 As Worksheet Set Ws1 = ThisWorkbook.Worksheets.Item(1) ' or Worksheets("Master Sheet") ' first worksheet counting tab from the left is worksheets item 1 Dim Lr1 As Long Let Lr1 = Ws1.Range("C" & Ws1.Rows.Count & "").End(xlUp).Row ' Range("A" & Ws1.Rows.Count & "").End(xlUp).Row ' http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11466&viewfull=1#post11466 Making Lr dynamic ( using rng.End(XlUp) for a single column. ) Dim arrNmes() As Variant Let arrNmes() = Ws1.Range("C4:C" & Lr1 & "").Value2 ' Range("A1:A" & Lr1 & "").Value2 Rem 2 Add hyperlinks Ws1.Hyperlinks.Delete Dim Cnt For Cnt = 4 To Lr1 ' ='F:\Excel0202015Jan2016\OffenFragensForums\AllenWyatt\[DynamicWorksheetNamesLinkHideBasedOnCellValue.xlsm]RAHIM'!$A$1 Dim Paf As String: Let Paf = "='" & ThisWorkbook.Path & "\[" & ThisWorkbook.Name & "]" & arrNmes(Cnt - 3, 1) & "'!$A$1" ' "='" & ThisWorkbook.Path & "\[" & ThisWorkbook.Name & "]" & arrNmes(Cnt, 1) & "'!$A$1" ' Ws1.Hyperlinks.Add Anchor:=Ws1.Range("A" & Cnt & ""), Address:="", SubAddress:="='" & arrNmes(Cnt, 1) & "'!A1", ScreenTip:=arrNmes(Cnt, 1), TextToDisplay:=arrNmes(Cnt, 1) Ws1.Hyperlinks.Add Anchor:=Ws1.Range("C" & Cnt & ""), Address:="", SubAddress:=Paf, ScreenTip:=arrNmes(Cnt - 3, 1), TextToDisplay:=arrNmes(Cnt - 3, 1) ' Ws1.Range("A" & Cnt & ""), Address:="", SubAddress:=Paf, ScreenTip:=arrNmes(Cnt, 1), TextToDisplay:=arrNmes(Cnt, 1) Next Cnt Bed: ' error handling code section. Let Application.EnableEvents = True End Sub '
Event macros
Code:' Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13456&viewfull=1#post13456 'If Target.Column = 1 And Not IsArray(Target.Value) Then ' we are in column A , And we selected one cell If Target.Column = 3 And Not IsArray(Target.Value) Then ' we are in column C , And we selected one cell Set LRng = Target Else End If End Sub Private Sub Worksheet_Change(ByVal Target As Range) If IsArray(Target.Value) Then Exit Sub ' If we have changed more than 1 cell, our code lines below will error, so best do nothing in such a case Dim Ws1 As Worksheet Set Ws1 = Me Dim Lr1 As Long Let Lr1 = Ws1.Range("C" & Ws1.Rows.Count & "").End(xlUp).Row ' Range("A" & Ws1.Rows.Count & "").End(xlUp).Row ' http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11466&viewfull=1#post11466 Making Lr dynamic ( using rng.End(XlUp) for a single column. ) If Not LRng Is Nothing And Target.Value = "" And LRng.Row = Lr1 + 1 Then Let Lr1 = Lr1 + 1 Dim Rng As Range Set Rng = Ws1.Range("C4:C" & Lr1 & "") ' Ws1.Range("A1:A" & Lr1 & "") If Not Intersect(Rng, Target) Is Nothing Then ' The Excel VBA Application.Intersect method returns the range where all the given ranges cross, or Nothing if there are no common cells. So, in this example, we would have Nothing if our selection ( which VBA supplies in Target ) , did not cross our names list ' https://docs.microsoft.com/en-us/office/vba/api/excel.application.intersect Dim Rw As Long Let Rw = Target.Row If Target.Value = "" Or Target.Value = "-" Then ' 5. If I delete the content of A1 (ANUJ), i.e, if cell A1 is blank, the corresponding sheet "ANUJ" should hide automatically. Let Application.EnableEvents = False Let Target.Value = "" Let Application.EnableEvents = True ' ThisWorkbook.Worksheets.Item(Rw + 1).Visible = False ThisWorkbook.Worksheets.Item(Rw + 1 - 3).Visible = False Exit Sub Else ' ThisWorkbook.Worksheets.Item(Rw + 1).Visible = True ' Let ThisWorkbook.Worksheets.Item(Rw + 1).Name = Target.Value ' In the list, each row number corresponds to one less than the item number of our worksheets made from that list ThisWorkbook.Worksheets.Item(Rw + 1 - 3).Visible = True Let ThisWorkbook.Worksheets.Item(Rw + 1 - 3).Name = Target.Value End If Else ' changed cell was not in Student name list End If ' Call AddHypolinkToWorksheet End Sub
Top 2 lines of code module
Code:Option Explicit Dim LRng As Range
File:
DynamicWorksheetNamesLinkHideBasedOnCellValueC.xls m : https://app.box.com/s/alo1fbzx8r41jd81rttghikytqzvm0w9




Reply With Quote
Bookmarks