作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很欣赏这是一个业余问题,但我不习惯 VB 及其语法。
我正在尝试根据数量(QTY)列中是否存在值将信息从一个工作表(ProductList)拉到另一个(报价)。
这是我的方法:
Private Sub cmdProductListContinue_Click()
'Declare variabless
Dim i, duration, qty, outputX, outputY
'Set initial values
duration = 120 'Used to determine number of iterations in for loop (no. of QTY cells we are to check)
i = 3 'Used as cell co-ordinates to pull information from
outputX = 17 'Used as cell co-ordinates to output information
'Populate invoice with product info by iterating through all QTY cells and pulling across info if needed
For i = 3 To duration
'Reset quantity to zero each time
qty = 0
'Set quantity to the value in the QTY cell
Set qty = Worksheets("ProductList").Cells(i, 3)
'If there is a quantity value present
If qty > 0 Then
'Insert quantity value into appropriate cell in quote sheet
Worksheets("Quote").Cells(outputX, 2) = qty
'Insert description into quote sheet
Worksheets("Quote").Cells(outputX, 3) = Worksheets("ProductList").Cells(i, 2)
'Insert unit price into quote sheet
Worksheets("Quote").Cells(outputX, 4) = Worksheets("ProductList").Cells(i, 4)
'Increment the output co-ordinates to the next line
outputX = outputX + 1
End If
Next i
'Open quote sheet
Sheets("Quote").Select
End Sub
最佳答案
在这里,您将 qty 分配给 对象 (范围):
Set qty = Worksheets("ProductList").Cells(i, 3)
qty = Worksheets("ProductList").Cells(i, 3).Value
Private Sub cmdProductListContinue_Click()
Dim i, duration, qty, outputX
Dim wsQuote As Worksheet, wsProd As Worksheet
Set wsQuote = Worksheets("Quote")
Set wsProd = Worksheets("ProductList")
duration = 120
outputX = 17
For i = 3 To duration
qty = wsProd.Cells(i, 3).Value
If qty > 0 Then
With wsQuote.Rows(outputX)
.Cells(2).Value = qty
.Cells(3).Value = wsProd.Cells(i, 2).Value
.Cells(4).Value = wsProd.Cells(i, 4).Value
outputX = outputX + 1
End With
End If
Next i
wsQuote.Activate
End Sub
关于Excel VB For循环没有遍历if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6189545/
我是一名优秀的程序员,十分优秀!