如何将宏推广到任意行

如何将宏推广到任意行

下面的 Excel 宏被编写为在 Excel 的特定行(第 2 行)上运行。我希望能够将其应用于我选择的任何行,而不仅仅是第 2 行。有人知道如何改编它吗?这是我自己编写的,但我不懂任何 VBA 编码。

Sub test()

Set ie = CreateObject("internetexplorer.application")

ie.Navigate "http://www.fedex.com/us/"

While ie.Busy Or ie.ReadyState <> 4: DoEvents: Wend

ie.Visible = 1

Set doc = ie.document

On Error Resume Next

With doc

    .getElementsByname("username").Item(0).Value = "Username"
    .getElementsByname("password").Item(0).Focus
    .getElementsByname("password").Item(0).Value = "Password"
    .getElementsByname("startpage").Item(0).selectedindex = 1
    .getElementsByname("login").Item(0).Click

    While ie.Busy Or ie.ReadyState <> 4: DoEvents: Wend

    Application.Wait (Now + TimeValue("00:00:04"))

    Set doc = ie.document

    doc.getElementsByname("toData.addressData.contactName").Item(0).Value
= Range("L2")

    doc.getElementsByname("toData.addressData.addressLine1").Item(0).Value
= Range("M2")

    doc.getElementsByname("toData.addressData.city").Item(0).Value = Range("N2")

    doc.getElementsByname("toData.addressData.zipPostalCode").Item(0).Value
= Range("P2")

    doc.getElementsByname("toData.addressData.phoneNumber").Item(0).Value
= Range("S2")

    doc.getElementsByname("psdData.mpsRowDataList[0].weight").Item(0).Value
= Range("F2")

    doc.all("psdData.packageType").Value = "Your Packaging"

    Call ie.document.parentWindow.execScript("psdHandler_onShipPackageTypeChange(""IS3_GSV="")", "JavaScript")

    ie.document.getElementsByname("psdData.mpsRowDataList[0].mpsDimensionSelect").Item(0).Item(1).Selected
= True

    Call ie.document.parentWindow.execScript("psdHandler_onChangeDimensions()", "JavaScript")

    ie.document.getElementsByname("psdData.mpsRowDataList[0].mpsLength").Item(0).Value
= Range("g2")
    ie.document.getElementsByname("psdData.mpsRowDataList[0].mpsWidth").Item(0).Value
= Range("h2")
    ie.document.getElementsByname("psdData.mpsRowDataList[0].mpsHeight").Item(0).Value
= Range("i2")

    .getElementbyid("module.rating._headerButtons").Click

    ie.document.getElementbyid("rating.calculateRate").Click
     End With

Set doc = Nothing Set ie = Nothing

End Sub

答案1

插入以下内容作为第一行:

activeRow = ActiveCell.Row

这将返回当前选定的行。然后将与第 2 行相关的每个单元格替换为

"X" & activeRow

其中 X 是代码中已存在的列名。

相关内容