首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

有关excel vba

2012-04-01 
求助有关excel vbaPrivate Sub CommandButton1_Click()Dim iFind As RangeDim a As Integer工作表的数目D

求助有关excel vba
Private Sub CommandButton1_Click()
Dim iFind As Range
Dim a As Integer '工作表的数目
Dim b As Double '暂存元件数量差值= 库存量 - 需求量
For a = 2 To Worksheets.Count Step 1
 i = 5
  Set BranchName = Worksheets(1).Range("A:A") '查询
  Do While Worksheets(a).Cells(i, 2).Text <> ""
  Set iFind = BranchName.Find(Worksheets(a).Cells(i, 2).Text)
  If a = 2 Then
   
  If Not iFind Is Nothing Then
  TargetRow = iFind.Row
  'MsgBox (Worksheets(1).Cells(TargetRow, 10).Value)
  Worksheets(a).Cells(i, 8).Value = Worksheets(1).Cells(TargetRow, 10).Value
  Else
  Worksheets(a).Cells(i, 8).Value = ""
  End If
  b = Worksheets(1).Cells(TargetRow, 10).Value - Worksheets(a).Cells(i, 9).Value '求差
  If b > 0 Then
  Worksheets(1).Cells(i, 12).Value = b
  Else
  Worksheets(1).Cells(i, 12).Value = ""
  End If
  Else
  If Not iFind Is Nothing Then
  TargetRow = iFind.Row
  'MsgBox (Worksheets(1).Cells(TargetRow, 10).Value)
  Worksheets(a).Cells(i, 8).Value = Worksheets(1).Cells(TargetRow, 12).Value
  Else
  Worksheets(a).Cells(i, 8).Value = ""
  End If
  b = Worksheets(1).Cells(TargetRow, 12).Value - Worksheets(a).Cells(i, 9).Value '求差
  If b > 0 Then
  Worksheets(1).Cells(i, 12).Value = b '不能执行????
  Else
  Worksheets(1).Cells(i, 12).Value = ""
  End If

  End If
  i = i + 1
  Loop
Next a
End Sub



其中b = Worksheets(1).Cells(TargetRow, 12).Value - Worksheets(a).Cells(i, 9).Value '求差
  If b > 0 Then
  Worksheets(1).Cells(i, 12).Value = b '不能执行????
  Else
  Worksheets(1).Cells(i, 12).Value = ""
  End If

他总是跳过Worksheets(1).Cells(i, 12).Value = b ,是不是他不能判断正负值啊?顺便在帮我看看整个程序还有啥问题?
还望多多批评指正

[解决办法]
单步调试啊,设置个断点,看看求差以后b的值。
程序写的不太规范,也不好说其他地方有什么问题。
[解决办法]

探讨
Private Sub CommandButton1_Click()
Dim iFind As Range
Dim a As Integer '工作表的数目
Dim b As Double '暂存元件数量差值= 库存量 - 需求量
For a = 2 To Worksheets.Count Step 1
i = 5
Set BranchName = Worksheets(1).R……

[解决办法]
如下加几个MSGBOX 看看具体值在析一下问题在那
VB code
msgbox  Worksheets(1).Cells(TargetRow, 12).Value msgbox Worksheets(a).Cells(i, 9).Value  b = Worksheets(1).Cells(TargetRow, 12).Value - Worksheets(a).Cells(i, 9).Value '求差msgbox b  If b > 0 Then 

热点排行