在VBA中递减一个值

在VBA中递减一个值,vba,excel,Vba,Excel,我是VBA的新手,加入VBA是因为一位朋友告诉我这个社区很有帮助。 我目前在VBA中遇到如下问题: -用户从工作表“Book Rent”中的列表中选择代码; -该代码保存在单元格D10中,由宏保存在另一张“Renting”表中,该表保存了表单中的多个数据; -然后在另一个称为“图书”的工作表中,B栏有图书代码,C栏有书名,D栏有可用图书数量; -我想要的是,当用户租用这本书时,数量会减少1(如果数字现在为零或不是零,我不需要验证,所以只要减少就足够了) 我试过做床单(“书本租金”)。单元格(10

我是VBA的新手,加入VBA是因为一位朋友告诉我这个社区很有帮助。 我目前在VBA中遇到如下问题:

-用户从工作表“Book Rent”中的列表中选择代码; -该代码保存在单元格D10中,由宏保存在另一张“Renting”表中,该表保存了表单中的多个数据; -然后在另一个称为“图书”的工作表中,B栏有图书代码,C栏有书名,D栏有可用图书数量; -我想要的是,当用户租用这本书时,数量会减少1(如果数字现在为零或不是零,我不需要验证,所以只要减少就足够了)

我试过做床单(“书本租金”)。单元格(10,“D”)=床单(“书本”)。范围(“D5”)-1,但它给了我一个错误。 我现在正在尝试使用for循环,但它也给了我一个错误。 如果有人能给我一些帮助,或者如果你需要更多的信息,我很乐意帮助你

下面是我的for循环:

Dim Looper As Integer
    For Looper = 5 To Sheets("Livros").Cells(Looper, 2) = Sheets("Form Aluguer").Cells(10, "D")
        Sheets("Livros").Cells(Looper, "D").Select
        Cells(Looper, "D") = Cells(Looper, "D") - 1
    Next Looper
顺便说一下,谢谢。

编辑:

这是我认为你在寻找的,我在这篇文章的底部留下了我最初的答案。但这段代码更多的是我认为您需要的工作版本。根据工作簿的布局,可能需要稍微调整一下

由于您是VBA新手,我添加了比平时更多的评论

希望这有帮助

Sub Decrement()
'Declare a BookCount variable to hold the value of the cell containing the number of books
Dim BookCount As Integer
'Declare a range to hold the range of the Books Quantity column
Dim QuantityRng As Range
'Dim two ranges to use during the loops
Dim RentCell As Range, BookCell As Range
'Declare and set a variable named RentRng to the Rental list and BookRng to the Book list's Code column
Dim RentRng As Range, BookRng As Range
Set RentRng = Worksheets("Renting").Range("A:A")
Set BookRng = Worksheets("Books").Range("B:B")

'For every cell in the Renting list
For Each RentCell In RentRng
'Stop the subroutine when the loop encounters a blank cell
    If RentCell.Value = "" Then
        Exit Sub
    End If
'Check every cell in the Book code list
    For Each BookCell In BookRng
'Exit the loop when encounters a blank cell so can look for the next book in the outer loop
        If BookCell.Value = "" Then
            Exit For
        End If
'Check if the Rental worksheet Code Matches the Books worksheet code, and if so then decrements the field by one
        If RentCell.Value = BookCell.Value Then
            Set QuantityRng = BookCell.Offset(0, 2)
            BookCount = QuantityRng.Value
            BookCount = BookCount - 1
            QuantityRng.Value = BookCount
        End If
    Next BookCell
Next RentCell

End Sub
我不确定我是否理解正确,但我将首先声明一个名为BookCount的变量来保存相关单元格的值。然后可以使用BookCount=BookCount-1

这里的问题是,你试图从一个范围中去掉1,而实际上它是你想要修正的范围的值

然后可以将范围的值设置为BookCount

这里有一个小例子

Sub Decrement()
Dim BookCount As Integer
BookCount = Range("A1").Value
BookCount = BookCount - 1
Range("A1").Value = BookCount
End Sub

如果要从X循环到Y(其中X>Y),请使用以下命令:

For X = 5 to 0 Step - 1

Next
Step命令告诉它如何递增X,0是递增X到的时间。请记住,For循环需要一些数字作为第二个参数,因此可以执行以下操作:

For X = 5 to Sheets("Form Aluguer").Cells(10, "D")
但是,只有当
表格(“Form Aluguer”)。单元格(10,“D”)
包含一个数字时,才能执行此操作

如果您想在
Sheets(“Livros”).Cells(Looper,2)=Sheets(“Form Aluguer”).Cells(10,“D”)
时循环,您需要执行以下操作:

Dim looper as Long
Do While Sheets("Livros").Cells(Looper, 2) = Sheets("Form Aluguer").Cells(10, "D")
    looper = looper -1
Loop

使用
单元格(活套,“D”)。值
。如果不这样做,您将使用
范围
对象。同样,你也不需要在上面调用
Select
。你的循环是用
Sheets(“Livros”)从5到真或假说
。单元格(Looper,2)=Sheets(“Form Aluguer”)。单元格(10,“D”)
`如果你在循环中需要帮助,我只需要知道你试图循环的具体内容。保存这些值的是“租赁”工作表吗?