VBA如何在“ Do While”功能中实现IF LEN?

时间:2018-08-08 15:32:01

标签: vba while-loop

我正在尝试将其他三个工作表(AMZN,TGT,WM)中的产品ID携带到一个连续的列(MASTER V2)中。但是其中一张纸有“脏”数据,其中句子/词组应包含ID。

第二步是我的问题所在。如果值的长度为6位数字,我希望在MASTER V2中粘贴该函数,否则请跳过AMZN工作表中的该行。

现在看来,无论其长度如何,它都将粘贴在所有数据上。

Sub RefMaster()

    Dim lastrow
    Dim row
    Dim TGTrow
    Dim AMZNrow
    Dim WMrow
    Dim row2

    TGTrow = Application.CountA(Worksheets("TGT").Range("a:A"))
    AMZNrow = Application.CountA(Worksheets("AMZN").Range("a:a")) - 2
    WMrow = Application.CountA(Worksheets("WM").Range("a:a"))
    row = 2
    row2 = 2



    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    Do While row <= TGTrow
    'TGTStyle
        Worksheets("Master V2").Range("a" & row) = Worksheets("TGT").Range("d" & row2) & Worksheets("TGT").Range("e" & row2)    'Style
        row = row + 1
        row2 = row2 + 1
    Loop

    row2 = 3

    Do While row > TGTrow And row <= (AMZNrow + TGTrow)
    'AMZNStyle
        If Len(Worksheets("AMZN").Range("q" & row2)) = 6 Then
            Worksheets("Master V2").Range("a" & row) = Worksheets("AMZN").Range("q" & row2)
            row = row + 1
            row2 = row2 + 1
        Else: row2 = row2 + 1
            AMZNrow = AMZNrow - 1
        End If
    Loop

    lastrow = TGTrow + AMZNrow + WMrow

    Do While row > (AMZNrow + TGTrow) And row <= lastrow
    'WMStyle
        row = row + 1
        row2 = row2 + 1
    Loop

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True

    ActiveWorkbook.RefreshAll

End Sub

0 个答案:

没有答案