VBA循环虽然看起来应该是没有进步

时间:2018-01-30 17:24:07

标签: vba loops

我的程序出现在VBA屏幕上,但是在Excel文档上它正在操作,而不是那么多。

应该循环一个根列,将其填充到其他工作表,直到根列到达一个空白单元格。当我运行循环时,它会循环,并且加1显示它对RW变量生效,当我将鼠标悬停在VBA stepo through过程中时。然而它只是一遍又一遍地从同一行继续输入值,好像当我可以看到RW时它没有前进,并且它一直复制到同一行,再次,我可以看到RW变量前进1在VBA编辑器中。

这是代码。

Sub ExportData()
Dim RW As Integer
    RW = 2
Dim CL As Integer
CL = 3

With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With

Dim SKUUS As Range
Set SKUUS = Sheets("US").Cells(RW - 1, CL - 2)
Dim SKUCA As Range
Set SKUCA = Sheets("CA").Cells(RW - 1, CL - 2)
Dim RootSKU As Variant
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL)

AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")


'HeadersUS
SKUUS.Value = "Item #"
SKUUS.Offset(0, 1).Value = "MAX QTY"
SKUUS.Offset(0, 2).Value = "PROMO Price"
SKUUS.Offset(0, 3).Value = "AvailableDate"
SKUUS.Offset(0, 4).Value = "VendorNumOverride"
SKUUS.Offset(0, 5).Value = "PromoListPrice"
SKUUS.Offset(0, 6).Value = "BOGO ITEM #"
SKUUS.Offset(0, 7).Value = "BOGO QTY"
SKUUS.Offset(0, 8).Value = "ProgramCode"
SKUUS.Offset(0, 9).Value = "PromoCode"

'HeadersCA
SKUCA.Value = "Item #"
SKUCA.Offset(0, 1).Value = "MAX QTY"
SKUCA.Offset(0, 2).Value = "PROMO Price"
SKUCA.Offset(0, 3).Value = "AvailableDate"
SKUCA.Offset(0, 4).Value = "VendorNumOverride"
SKUCA.Offset(0, 5).Value = "PromoListPrice"
SKUCA.Offset(0, 6).Value = "BOGO ITEM #"
SKUCA.Offset(0, 7).Value = "BOGO QTY"
SKUCA.Offset(0, 8).Value = "ProgramCode"
SKUCA.Offset(0, 9).Value = "PromoCode"

Do

'Populate First Row in US
SKUUS.Offset(1, 0) = RootSKU.Value
SKUUS.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.8
    SKUUS.Offset(1, 1) = Math.Round(SKUUS.Offset(1, 1).Value, 0)
SKUUS.Offset(1, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(1, 3) = AvailDate
SKUUS.Offset(1, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(1, 9) = PromoCode

'Populate First Row in US
SKUCA.Offset(1, 0) = RootSKU.Value
SKUCA.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.2
    SKUCA.Offset(1, 1) = Math.Round(SKUCA.Offset(1, 1).Value, 0)
SKUCA.Offset(1, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(1, 3) = AvailDate
SKUCA.Offset(1, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(1, 9) = PromoCode

RW = RW + 1
Loop Until RootSKU = ""
End Sub

1 个答案:

答案 0 :(得分:0)

我将变量移动到循环内部,但仍然得到相同的结果。在这个前提下玩了一下后,我能够让它像这样工作。谢谢你的帮助。

Sub ExportData()
Dim RW As Long
Dim CL As Long
Dim SKUUS As Range
Dim SKUCA As Range
Dim RootSKU As Variant

RW = 2
CL = 1

With ThisWorkbook
    .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
    .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With

AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")


'HeadersUS
Sheets("US").Range("A1") = "Item #"
Sheets("US").Range("B1") = "MAX QTY"
Sheets("US").Range("C1") = "PROMO Price"
Sheets("US").Range("D1") = "AvailableDate"
Sheets("US").Range("E1") = "VendorNumOverride"
Sheets("US").Range("F1") = "PromoListPrice"
Sheets("US").Range("G1") = "BOGO ITEM #"
Sheets("US").Range("H1") = "BOGO QTY"
Sheets("US").Range("I1") = "ProgramCode"
Sheets("US").Range("J1") = "PromoCode"

'HeadersCA
Sheets("CA").Range("A1") = "Item #"
Sheets("CA").Range("B1") = "MAX QTY"
Sheets("CA").Range("C1") = "PROMO Price"
Sheets("CA").Range("D1") = "AvailableDate"
Sheets("CA").Range("E1") = "VendorNumOverride"
Sheets("CA").Range("F1") = "PromoListPrice"
Sheets("CA").Range("G1") = "BOGO ITEM #"
Sheets("CA").Range("H1") = "BOGO QTY"
Sheets("CA").Range("I1") = "ProgramCode"
Sheets("CA").Range("J1") = "PromoCode"

Do

Set SKUUS = Sheets("US").Cells(RW, CL)
Set SKUCA = Sheets("CA").Cells(RW, CL)
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL + 2)

'Populate First Row in US
SKUUS = RootSKU.Value
SKUUS.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.8
    SKUUS.Offset(0, 1) = Math.Round(SKUUS.Offset(0, 1).Value, 0)
SKUUS.Offset(0, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(0, 3) = AvailDate
SKUUS.Offset(0, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(0, 9) = PromoCode

'Populate First Row in US
SKUCA = RootSKU.Value
SKUCA.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.2
    SKUCA.Offset(0, 1) = Math.Round(SKUCA.Offset(0, 1).Value, 0)
SKUCA.Offset(0, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(0, 3) = AvailDate
SKUCA.Offset(0, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(0, 9) = PromoCode

RW = RW + 1
Loop Until RootSKU.Offset(1, 0) = ""
End Sub