如何将宏推广到任何行


-1

下面的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

Answers:


1

插入以下作为第一行:

activeRow = ActiveCell.Row

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

"X" & activeRow

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


这对我行得通。
Stan Green
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.