用“分隔符”在VBA中集中更多的单元格

我正尝试使用VBA代码从GCmap.com生成地图。 我在一列中有一长串机场对。 将生成地图的超链接的格式将是:

“ http://www.gcmap.com/map?P=”&(calue in .cell(1.51))&“%0d%0a”&(calue in .cell(2.51)&…

'%0d%0a – 是需要在值之间的分隔符

问题是,这个列表是一个很长的时间,只是简单的循环会导致超链接的巨大长度(有时过长的时间来生成地图),所以它必须是如果valueOfFirstCell = ValueOfCellBellow然后'跳到下一个

我已经试过这个(可能是最坏的书面代码,但请注意,我只是在VBA的初学者:)

Sub hyperlinkgenerator() Dim separator As String ' declared the 'separator' value separator = "%0d%0a" Dim datarow, irow As Integer Dim myval as Range With Sheets("Direct flights") Set myval = .Cells(datarow, 51) Do While myval <> "" ' do until last empty row Dim Value1 As String Value1 = Sheets("Direct flights").Cells(datarow, 51) ' declare value of the first cell Dim Value2 As String Value2 = Sheets("Direct flights").Cells(datarow + 1, 51) ' declare value of cell bellow If Value1 = Value2 Then datarow = datarow + 1 Else: 'enter Value1 in hyperlink that is followed by & separator 'also the hyperlink must start with: http://www.gcmap.com/map?P= ' and end with %0d%0a&MS=wls&MR=540&MX=720x360&PM=* End If datarow = datarow + 1 Loop End With End Sub 

最终的链接将如下所示:

http://www.gcmap.com/map?P=LGW-AMS%0d%0aAMS-LHR%0d%0aLCY-AMS%0d%0aLUX-AMS%0d%0aBRE-AMS%0d%0aCWL-AMS%0d% 0aNUE-AMS%0d%0aAMS-JFK%0d%0a&MS = wls&MR = 540&MX = 720×360&PM = *

我不知道如何保持循环添加新的和新的文本到超链接。 也应该生成超链接,并打开一旦用户点击button(这很容易做到)。

提前谢谢了!

试试这个:

 Sub hyperlinkgenerator() Dim thisVal As String Dim nextVal As String Dim currentRow As Long Dim hyperlink As String currentRow = 1 ' or whatever the first row is hyperlink = "" With Sheets("Direct flights") Do While True thisVal = .Cells(currentRow, 1).Value nextVal = .Cells(currentRow + 1, 1).Value If thisVal = "" Then Exit Do ElseIf thisVal <> nextVal Then hyperlink = hyperlink & thisVal & "%0d%0a" End If currentRow = currentRow + 1 Loop End With hyperlink = "http://www.gcmap.com/map?P=" & hyperlink & "&MS=wls&MR=540&MX=720x360&PM=" MsgBox hyperlink End Sub