VBA:从另一列获取数据列表并使用循环将它们传输到另一列

时间:2015-05-13 02:02:00

标签: excel vba excel-vba

假设我想获取一个数据列表,并且只想在每个列表中获取某个字符串,并且我想将它们传输到不同的列。

例如,我想在A列的每个数据上获得某些字符串,并且我想将这些特定字符串传输到另一列,即列E.我到目前为止所得到的是这个,但它不起作用。我是VBA的新手,我希望你们能帮助我。感谢。

PS:我对某个字符串的意思是在该列的每个字符串上使用mid函数。

Sub test()

With Sheets("Sheet1")
    LastRowE = .Range("E" & .Rows.Count).End(xlUp).Row
End With

For Each MyCell In Range("E2:E" & LastRowE)

    MyCell.Value = Mid(Range("A2:A6"), 7, 2)

Next MyCell

End Sub

1 个答案:

答案 0 :(得分:0)

请继续尝试下面的代码。您可以根据自己的情况调整代码。

之前的数据

on email(emails, names, origin, destination)
    set recipientName to names
    set recipientAddress to emails
    set theSubject to "c" & " --> " & "d" & "Shipment"
    set theContent to names & ", if you would like assistance with your shipment moving from " & "c" & " to " & "d" & " , or any other shipment you may have, please let me know. We have drivers available in your Area of Operations ready for pick up."

    tell application "Mail"

        ##Create the message
        set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

        ##Set a recipient
        tell theMessage
        make new to recipient with properties {name:recipientName, address:recipientAddress}

            ##Send the Message
            send

        end tell
    end tell
end email

on recipientNames()
    set y to 1 as string
    tell application "Numbers"
        activate
        open alias "Macintosh HD:Users:TYPKRFT:Desktop:Form.numbers"
        tell table 1 of sheet 1 of document 1
            set myNames to {}
            repeat with i from 1 to the count of rows
                set x to "B" & y
                set myNames to myNames & the value of the cell x
                set y to y as integer
                set y to y + 1
                set y to y as string
            end repeat
        end tell
    end tell

    set itemsToDelete to {"missing value", "Name", missing value, "stop"}

    set cleanNames to {}

    repeat with i from 1 to count myNames
        if {myNames's item i} is not in itemsToDelete then set cleanNames's end to myNames's item i
    end repeat
    return cleanNames
end recipientNames

on emailAddress()
    set y to 1 as string
    tell application "Numbers"
        activate
        open alias "Macintosh HD:Users:TYPKRFT:Desktop:Form.numbers"
        tell table 1 of sheet 1 of document 1
            set myEmails to {}
                repeat with i from 1 to the count of rows
                set x to "C" & y
                set myEmails to myEmails & the value of the cell x
                set y to y as integer
                set y to y + 1
                set y to y as string
            end repeat
        end tell
    end tell

    set itemsToDelete to {"missing value", "Email", missing value, "stop"}

    set cleanEmails to {}

    repeat with i from 1 to count myEmails
        if {myEmails's item i} is not in itemsToDelete then set cleanEmails's end to myEmails's item i
    end repeat
    return cleanEmails
end emailAddress

set a to emailAddress()
set b to my recipientNames()


set reps to count of a
set x to 1
repeat reps times
    set emails to item x of a
    set names to item x of b
    my email(emails, names)
    set x to x + 1
end repeat  

<强>代码

   A           B    C    D    E
1  hello 19
2  hello 20
3  hello 40
4  hello rr

运行代码后

Sub test()

    ' find the last row assuming data starts from A1
    Dim LastRow As Integer
    LastRow = Range("A1").End(xlDown).Row

    ' loop through E1..E<n>
    ' take value of corresponding row of column A and
    ' do MID on it. Put the result in A's row
    Dim MyCell As Range
    For Each MyCell In Range("E1:E" & LastRow)
        MyCell.Value = Mid(Range("A" & MyCell.Row), 7, 2)
    Next

End Sub

希望这些信息有所帮助。