简体   繁体   English

VBA从当前单元格值打开工作簿

[英]VBA Open workbook from current cells value

Hi I have a spreadsheet which I want to use the current cells value to open the url specified there and enter into the column next to it. 嗨,我有一个电子表格,我想使用当前单元格值打开指定的URL并进入旁边的列。 The URL only contains one set of characters. URL仅包含一组字符。 I tried recording with relative references turned on and got the following: 我尝试使用相对引用打开录制并获得以下内容:

Sub GETASINV2()
'
' GETASINV2 Macro
'

'
    Selection.Copy
    Application.CutCopyMode = False
    Workbooks.OpenText Filename:="http://upctoasin.com/027616716927", Origin:= _
        xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
        , ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:= _
        False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True
    Selection.Copy
    ActiveWindow.ActivateNext
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, -1).Range("A1").Select
End Sub

As you can see it all seems to be relative apart from the filename where it has picked up the value I entered and not the fact that I "copy pasted the value". 正如你所看到的那样,它似乎与文件名相对,它已经获取了我输入的值,而不是我“复制粘贴值”的事实。

Once this is done I want to repeat for the remaining list of urls (around 3000). 一旦完成,我想重复剩下的网址列表(大约3000)。 I can probably find someway of repeating till no more URLS exist but if you know a way would be glad to get help on this part also! 我可能会发现有些重复,直到不再存在URL,但如果你知道一种方式也很乐意在这部分得到帮助!

Thank you 谢谢

I put the link you gave us in rows A1:A10 and ran the following code to get loop through each cell and place the value from the link in the cell to it's right. 我把你给我们的链接放在A1:A10行中,然后运行以下代码来遍历每个单元格,并将单元格中链接的值放到右边。

I also tested it with 1000 links and it completed in 279 seconds -- so roughly 4 per second. 我还测试了1000个链接,它在279秒内完成 - 大约每秒4个。 I'd be interested to see if anyone has a quicker method. 我有兴趣看看是否有人有更快的方法。

Sub GETASINV2()
    Application.ScreenUpdating = False

    Dim sht As Worksheet
    Set sht = ThisWorkbook.Worksheets("Sheet1") 'update for your worksheet name

    Dim inputRange As Range, r As Range
    Set inputRange = sht.Range("A1:A10") 'update for your range

    For Each r In inputRange
        With sht.QueryTables.Add(Connection:= _
            "URL;" & r.Value, Destination:=r.Offset(0, 1))
            .Refresh BackgroundQuery:=False
            .Delete
        End With
    Next r

    Application.ScreenUpdating = True
End Sub


Update 更新

Is there a way of it working out the size of the range itself based on the number of records without a gap? 有没有办法根据没有间隙的记录数量计算出范围本身的大小?

Yes, you can replace this: 是的,你可以替换这个:

 Set inputRange = sht.Range("A1:A10") 

With the following: 具有以下内容:

 Set inputRange = Range(sht.Range("A1"), sht.Range("A1").End(xlDown)) 

Where A1 is the location of your first link A1是您的第一个链接的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM