Hyperlink Text in a Excel Row Automatically

Anonymous
2011-10-09T03:53:11+00:00

I have one excel sheet called product.xls and an Excel template with product detail fields.

I open this excel sheet. Whatever I type in the first column, should get converted to a hyperlink. So let's say I typed 'Roger' in A6. Roger should be hyperlinked. On clicking 'Roger' a new file with the name of Roger should be created  in a folder, using the template, and this new file should open for the user to edit. If the same filename as Roger already exists, open existing file.

Is this possible in Excel? My friend told you need something called VBA but I am so clueless that you guys are my only step-by-step help!

Thanks for reading this.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2011-10-10T06:01:13+00:00

Try this code.  Just delete the old and use this instead.  You'll need to make the same changes in it that you made in the first.  This time, if you type a name into the sheet and a file of that name already exists, then the hyperlink is still created and the file is opened.  No second new file is made.  This could result in multiple "Coffee" entries on the sheet.

Private Sub Worksheet_Change(ByVal Target As Range)

   'after typing into a single cell in column A below row 1,

   'either a workbook will be created using a template file

   'in the same folder with this file and saved using the

   'name typed into the cell, also in the same folder.

   'Once the new workbook is created and saved, then

   'the cell will be set up with a hyperlink to the created file.

   '

Const nameOfTemplateFile = "Book1.xltm" ' change as required   Const invalidFNCharacters = "<>:|/?*"""

   Const maxNameLength = 260

   Const FirstPossibleRowToUse = 2

   Dim myPath As String

   Dim newName As String

   Dim typeFile As String

   Dim hyperLink As String

   Dim LC As Integer ' loop counter

  'test if change in column A, and only 1 cell changed, and

   'the cell is not in a "protected" row (row 1 for now)

   'the result of the change is not an empty cell ( [Del] used)

   If Target.Column <> 1 Or _

    Target.Row < FirstPossibleRowToUse Or _

    Target.Cells.Count > 1 Or _

    IsEmpty(Target) Then

     Exit Sub

   End If

   'ask if the user really wants to create this new workbook

   If MsgBox("Do you wish to create a workbook named:" & _

    vbCrLf & Target.Value & " ?", vbYesNo + vbQuestion, "Continue Creating File?") _

    <> vbYes Then

     Exit Sub ' user chose not to create the file

   End If

   newName = Trim(Target.Value)

   'make sure there are no illegal characters in the entered name

   'if any illegal characters found, change to _ character

   If newName = "" Then

     MsgBox "Invalid filename - cannot continue.", _

      vbOKOnly + vbCritical, "Empty Filename"

     Exit Sub ' cannot name a book with empty string!

   End If

   For LC = 1 To Len(invalidFNCharacters)

     newName = Replace(newName, Mid(invalidFNCharacters, LC, 1), "_")

   Next

   'get path to this workbook

   myPath = ThisWorkbook.Path & Application.PathSeparator

   Application.ScreenUpdating = False

   'new workbook is now active

   newName = myPath & newName

   If Len(newName) > (maxNameLength - 5) Then

     MsgBox "The name you have chosen, combined with " & _

      "the path to the file is longer than Windows permits." & _

      vbCrLf & "Cannot Continue...", _

      vbOKOnly + vbCritical, "Complete Name Too Long"

     Exit Sub

   End If

   'prepare for last check - add file type identifier

   'based on the .FileFormat property

   Select Case ActiveWorkbook.FileFormat

     Case Is = xlWorkbookNormal, xlExcel8, xlExcel9795

       'Excel 97-2003 compatible file

     newName = newName & ".xls"

    Case Is = xlOpenXMLWorkbook

       'Excel 2007/2010 file without macros

     newName = newName & ".xlsx"

    Case Is = xlOpenXMLWorkbookMacroEnabled

       'Excel 2007/2010 file with macros

     newName = newName & ".xlsm"

  End Select

  'last check: does a file of this name already exist?

  'if it does, then go ahead and make the link and

  'open the existing file.

  'this might result in duplicate entries in this workbook

  If Dir$(newName) <> "" Then

    'make the hyperlink

    Application.EnableEvents = False

    Target.Hyperlinks.Add Anchor:=Target, Address:=newName, _

     TextToDisplay:=Target.Value

    Application.EnableEvents = True

    'just open the workbook and abort this operation

    'open the workbook, update any links, do NOT open as Read Only.

    Workbooks.Open newfilename, True, False

    Set basecell = Nothing

    Exit Sub

  End If

  'create new file from template in this same folder

  Workbooks.Add Template:=myPath & nameOfTemplateFile

  Application.DisplayAlerts = False

   'save the file based on the typeFile

   Select Case ActiveWorkbook.FileFormat

     Case Is = xlWorkbookNormal, xlExcel8, xlExcel9795

       'Excel 97-2003 compatible file

     ActiveWorkbook.SaveAs Filename:=newName, _

      FileFormat:=xlExcel8, Password:="", _

      WriteResPassword:="", ReadOnlyRecommended:=False, _

      CreateBackup:=False

    Case Is = xlOpenXMLWorkbook

       'Excel 2007/2010 file without macros

     ActiveWorkbook.SaveAs Filename:=newName, _

      FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

    Case Is = xlOpenXMLWorkbookMacroEnabled

       'Excel 2007/2010 file with macros

     ActiveWorkbook.SaveAs Filename:=newName, _

      FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

  End Select

   ActiveWorkbook.Close

   'back into this workbook

   'add the hyperlink to the cell

   Application.EnableEvents = False

   Target.Hyperlinks.Add Anchor:=Target, Address:=newName, _

    TextToDisplay:=Target.Value

   Application.EnableEvents = True

   Set basecell = Nothing

 'assume that the presence of a hyperlink to a file

 'is confirmation enough that all went well.

End Sub

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-10-10T10:13:12+00:00

    never mind looks like macros were getting disabled automatically when the excel file was opened each time.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-10-10T09:16:37+00:00

    Strangely the code runs well when the excel file is opened for the first time. When I close the excel file and open it again, nothing happens on typing in the first column. Right click > view code shows the code is there!

    Any idea what could be happening.

    My excel sheet is Products.xls and template is ProductDet.xlt

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-10-10T02:59:37+00:00

    God bless you JLLatham. This was spot on and thanks for writing this for me. I also followed your instructions of how to write vba code and it worked..yuhoooo!

    I just need one small change. If a file already exists, I want to open the file for editing and not overwrite it.

    Eg: If I typed 'Coffee' in the first column and if a file called 'Coffee.xls' already exist, then just open it. Overwriting it looses changes done to the file previously. Is that simple enough to do.

    Was this answer helpful?

    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more