从sheet1循环每个单元格,并始终在sheet2的1个单元格中替换

时间:2015-07-14 09:48:17

标签: excel excel-vba url vba

我遇到问题,当我想从sheet1使用C1到C21中的每个名称并在nameuser中插入url地址时。它会突然从Sheet1中删除C1到C21中的每个名称,但它不会替换每个名称的sheet2上的目标B2。

Sub URL_Get_Query()

Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet

Set wb = ThisWorkbook
Set src = wb.Sheets("Sheet1")
Set tgt = wb.Sheets("Sheet2")

Dim url As String
Dim symbol As String

tgt.Columns("B:C").Select
Selection.ClearContents
src.Select

Dim c As Range
Dim Rng As Range
Set Rng = Range("c3:c21")
For Each c In Rng
MsgBox (c.Address)

Rng = namerange

nameuser = namerange
'nameuser = src.Range("C3")
url1 = "URL;http://my001vs0253:81/user.aspx?username="
url = url1 & nameuser & "&userdomain=TP"

'"URL;http://my001vs0253:81/user.aspx?username=afiqg&userdomain=TP",

nameuser1 = "user.aspx?username="
nameurl = nameuser1 & nameuser & "&userdomain=TP_7"
'nameurl = nameuser1 & rRng & "&userdomain=TP_7"

With tgt.QueryTables.Add(Connection:= _
    url, Destination:=tgt.Range("B2"))


    '.CommandType = 0
    .Name = nameurl
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "22"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Next
End Sub

希望你能得到我的意见。谢谢你的提前。

1 个答案:

答案 0 :(得分:0)

也许您可以先将src工作表中的数据读入数组,然后使用该数组在tgt工作表中构建数据。这将删除两个工作表之间的直接链接,也可能是问题。问题是,为什么你需要删除这些列?删除列可以提供类似于您现在所拥有的问题,因此我通常会尽量避免这样做。