Excel VBA- 从活动单元格中减去变量
Excel VBA- Subtracting Variable from Active Cell
代码如下:
Sub Minus()
Dim numsub As Integer
If (D3 <> "") Then
numsub = Worksheets("Inventario 31-12-2015 ").Range("D3").Value
Dim FindString As Integer
Dim Rng As Range
FindString = ActiveWorkbook.Worksheets("Inventario 31-12-2015 ").Range("C3").Value
With Sheets("Inventario 31-12-2015 ").Range("C25:C")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Rng Is Nothing Then
Application.Goto Rng.Offset(0, 4), True
ActiveCell.Value = ActiveCell.Value - numsub
End If
End If
End Sub
我想要做的是:找到正确的单元格(这个函数应该像我已经拥有的那样工作),select 它并减去 D3 的值。由于我是 VBA 的新手,我无法使代码正常工作。
欢迎和赞赏任何提示、反馈或意见。
谢谢
这个答案采纳了上面的评论,并做了一些整理。 (你的sheet名字真的有一个space结尾吗?)
按照 OP 的建议更改了 FindString 的声明。另外,我将您的 Integer 更改为 Long,这是一个很好的做法(Google 以获取详细信息)。
Sub Minus()
Dim numsub As Long
Dim FindString As String
Dim Rng As Range
With Worksheets("Inventario 31-12-2015 ")
If .Range("D3") <> vbNullString Then
numsub = .Range("D3").Value
FindString = .Range("C3").Value
With .Range("C25:C100") 'change 100 to suit
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Rng Is Nothing Then
Application.Goto Rng.Offset(0, 4), True
Rng.Offset(0, 4).Value = Rng.Offset(0, 4).Value - numsub
End If
End If
End With
End Sub
代码如下:
Sub Minus()
Dim numsub As Integer
If (D3 <> "") Then
numsub = Worksheets("Inventario 31-12-2015 ").Range("D3").Value
Dim FindString As Integer
Dim Rng As Range
FindString = ActiveWorkbook.Worksheets("Inventario 31-12-2015 ").Range("C3").Value
With Sheets("Inventario 31-12-2015 ").Range("C25:C")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Rng Is Nothing Then
Application.Goto Rng.Offset(0, 4), True
ActiveCell.Value = ActiveCell.Value - numsub
End If
End If
End Sub
我想要做的是:找到正确的单元格(这个函数应该像我已经拥有的那样工作),select 它并减去 D3 的值。由于我是 VBA 的新手,我无法使代码正常工作。 欢迎和赞赏任何提示、反馈或意见。 谢谢
这个答案采纳了上面的评论,并做了一些整理。 (你的sheet名字真的有一个space结尾吗?)
按照 OP 的建议更改了 FindString 的声明。另外,我将您的 Integer 更改为 Long,这是一个很好的做法(Google 以获取详细信息)。
Sub Minus()
Dim numsub As Long
Dim FindString As String
Dim Rng As Range
With Worksheets("Inventario 31-12-2015 ")
If .Range("D3") <> vbNullString Then
numsub = .Range("D3").Value
FindString = .Range("C3").Value
With .Range("C25:C100") 'change 100 to suit
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Rng Is Nothing Then
Application.Goto Rng.Offset(0, 4), True
Rng.Offset(0, 4).Value = Rng.Offset(0, 4).Value - numsub
End If
End If
End With
End Sub