excel-VBA按钮另一个代码

时间:2014-08-07 09:08:55

标签: excel excel-vba vba

我正在处理这个excel项目,我想问你关于VBA按钮程序的这一部分。我是VBA的初学者,我刚刚学习了变量。 有人可以找到这个按钮的任何其他程序,如循环过程。 Button正在很好地完成它的工作,但我不知道这个程序是否已经建立,或者还有其他类似的循环。

这是我的VBA按钮代码和我的Excel文件

http://i57.tinypic.com/16a7h9i.png

好的,这是我的代码

Sub Button2_Click()
Dim emri As String, nje As Integer, dy As Integer, tre As Integer
Dim kater As Integer, pese As Integer, gjashte As Integer
Dim shtate As Integer, tete As Integer, nente As Integer


  emri = Range("B5").Value
  nje = Range("B6").Value
  dy = Range("F6").Value
  tre = Range("F7").Value
  kater = Range("F8").Value
  pese = Range("F9").Value

  gjashte = nje + dy
  shtate = nje + tre
  tete = nje + kater
  nente = nje + pese

If UCase(Range("B5").Value) = UCase(Range("D6").Value) Then

  Range("F6").Value = gjashte

ElseIf UCase(Range("B5").Value) = UCase(Range("D7").Value) Then

  Range("F7").Value = shtate

ElseIf UCase(Range("B5").Value) = UCase(Range("D8").Value) Then

  Range("F8").Value = tete

ElseIf UCase(Range("B5").Value) = UCase(Range("D9").Value) Then

  Range("F9").Value = nente

End If

Range("A12").Value = emri
Range("A13").Value = nje

Range("B5", "B6").ClearContents


End Sub

1 个答案:

答案 0 :(得分:1)

好的,让我们试试这段代码吧。它从第6行到第16行循环遍历列D.如果输入的值(更改为大写)与列表中的值匹配则对已售出的汽车产生加号,从而减去剩余数量(您可能在那里有一个功能,因此它只是一个可选项)。其他一切都保持不变。

Sub Button2_Click()

Dim emri As String, nje As Integer


  emri = UCase(Range("B5").Value)
  nje = Range("B6").Value


For i = 6 To 16 'you can also set variable for checking lastrow
    If emri = Cells(i, 4).Value Then

        Cells(i, 6).Value = Cells(i, 6).Value + nje 'plus in sold cars column
        Cells(i, 7).Value = Cells(i, 7).Value - nje 'minus in quantity left
        Exit For

    End If

Next i


Range("A12").Value = emri
Range("A13").Value = nje

Range("B5", "B6").ClearContents


End Sub