问题描述
Private Sub UpdatePrices()
On Error GoTo UpdatePrices_Error
Dim i As Integer
Dim curPrice As Currency
Dim r As Range
'动态范围选择的另一种方法
'这有一个已知的起始单元格,并得到在同一列中使用的所有行都是$
',在这种情况下是合适的。
'Set r = Range(" D2")。End(xlDown)
设置r =范围("D2")
r。选择
设置r =范围("D2",ActiveCell.End(xlDown) )
r.Select
i = 2 to r.Count + 1
'在下一行中,我们使用对单元格的相对引用
'第一个参数是Row(我们将使用我们的计数器)
'第二个是专栏(4 = D)
curPrice = Cells(i,4)
如果curPrice< 50然后
'10%价格回滚更便宜的商品
curPrice = curPrice * 0.9
'现在用更新后的价格更新单元格。
单元格(i,4).Value = curPrice
否则
'与下一个标准不符,
结束如果
下一页
'如果它到目前为止,它已全部完成
MsgBox"全部选中价格已经回滚!"$
UpdatePrices_Exit:
退出Sub¥
UpdatePrices_Error:
$
MsgBox"修复其中一个价格时出错!错误号:" &安培; Err.Number& vbCrLf& _
$
Err.Description& vbCrLf& "请检查:" &安培; curPrice
恢复UpdatePrices_Exit
结束Sub
有人能告诉我代码是如何工作的吗?请尽可能解释。
Private Sub UpdatePrices()
On Error GoTo UpdatePrices_Error
Dim i As Integer
Dim curPrice As Currency
Dim r As Range
'another approach to dynamic range selection
'this has a known start cell, and gets all rows used
'in the same column, which is appropriate in this case
'Set r = Range("D2").End(xlDown)
Set r = Range("D2")
r.Select
Set r = Range("D2", ActiveCell.End(xlDown))
r.Select
For i = 2 To r.Count + 1
'in this next line, we use a relative reference to the cell
'the first parameter is the Row(we will use our counter for this)
'the second is the Column(4 = D)
curPrice = Cells(i, 4)
If curPrice < 50 Then
'10% price rollback on cheaper items
curPrice = curPrice * 0.9
'now update the cell with the updated price
Cells(i, 4).Value = curPrice
Else
'doesn't match criteria, to to next one
End If
Next
'if it made it this far, it is all done
MsgBox "All selected prices have been rolled back!"
UpdatePrices_Exit:
Exit Sub
UpdatePrices_Error:
MsgBox "There was an error fixing one of the prices! Error No: " & Err.Number & vbCrLf & _
Err.Description & vbCrLf & "Please check: " & curPrice
Resume UpdatePrices_Exit
End Sub
Could somebody tell me how the code work? Please explain if possible.
第二个问题是为什么我们需要两次这样做
The second question would be why we need to do this twice
'动态范围选择的另一种方法
'这有一个已知的起始单元格,并在同一列中获取所有使用的行(
'),在这种情况下适用于
设置r =范围("D2")
r.Select
设置r =范围("D2",ActiveCell.End(xlDown))
r.Select
'another approach to dynamic range selection
'this has a known start cell, and gets all rows used
'in the same column, which is appropriate in this case
Set r = Range("D2")
r.Select
Set r = Range("D2", ActiveCell.End(xlDown))
r.Select
推荐答案
$
http://social.msdn.microsoft.com/Forums/en-US/exceldev / threads
以上i nfo被Paul P Clement IV在另一个帖子中的帖子中删除。
The above info was filched from a post by Paul P Clement IV in another thread.
谢谢,保罗: - )
这篇关于Visual Excel代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!