一个CSV中的标题,其他CSV中的超链接,创建内置链接的新CSV

时间:2015-03-18 13:07:14

标签: excel excel-vba csv vba

我在一个.csv中有纯文本标题,在另一个.csv

中有这些标题的超链接

我目前在同一本工作簿中打开它们,将标题放在A中,将超链接放在H中,然后使用

=HYPERLINK(H1,A1)

获取内置超链接的标题的最终输出。

是否有一种简单的方法(Excel VBA或宏)绕过手动工作并使用原始的两个.csv文件中的“内置超链接的标题”创建一个新的输出文件?

编辑:我的两个.csv文件在A列下方都有相应的文本(超链接和标题)。

2 个答案:

答案 0 :(得分:0)

Sub buildlinks()
Dim i As Integer
Dim wb1, wb2 As Workbook

Set wb1 = Application.Workbooks.Open("C:/path/Links.csv")
Set wb2 = Application.Workbooks.Open("C:/path/Titles.csv")

i = 1
Do Until wb1.Sheets("Sheet1Name").Cells(i, 1).Value = ""
    ThisWorkbook.Sheets("Sheet1Name").Cells(i, 1).Formula = "=HYPERLINK(" & wb1.Sheets("Sheet1Name").Cells(i, 1).Value & "," & wb2.Sheets("Sheet1Name").Cells(i, 1).Value & ")"
    i = i + 1
Loop

End Sub

答案 1 :(得分:0)

假设您要在当前电子表格中创建超链接,而不是创建单独的文件。

由于您已经说输入实际上只是文本文件,每行一个项目,而不是以逗号分隔,因此使用VBA文件处理命令实现起来非常简单。

Sub BuildLinks(titlesFilePath as String, linksFilePath As String, ByVal rowStart As Long)
  Dim tf As Long, lf As Long, of As Long
  tf = FreeFile
  On Error Goto NO_TITLE_FILE
  Open titlesFilePath For Input As #tf
  lf = FreeFile
  On Error Goto NO_LINKS_FILE
  Open linksFilePath For Input As #lf
  On Error Goto 0

  While Not (EOF(tf) Or EOF(lf))
    Dim curTitle As String, curLink As String
    Line Input #tf, curTitle
    Line Input #lf, curLink
    Cells(rowStart, 1).Formula = "=HYPERLINK(""" & curLink & """,""" & curTitle & """)"
  Wend
  Close #tf
  Close #lf
  Exit Sub
NO_TITLE_FILE:
  MsgBox "Can't Open Title File" & titlesFilePath
  Exit Sub
NO_LINKS_FILE:
  MsgBox "Can't Open Links File" & linksFilePath
End Sub