有excel表      A         B      C      D    E    F 
1  账号      姓名    金额 2  3024578    fhj    650 3  3058971    kkj    750 4  308922    ghj    900 
     要求实现金额这一列数字全部减少10,下面是几位高手给出的代码:
   
  Dim ex  As excel.Application
  Dim wb  As excel.Workbook
  Dim sh  As excel.WorksheetPrivate Sub Command1_Click()
 Dim i As Long, A As Long
    Count As Long
  
        Set ex = New excel.Application
    Set wb = ex.Workbooks.Open("c:\test.xls")  ' 你的A文件
    Set sh = wb.Sheets(1)  '第一个工作表
    
    Count = ex.Application.WorksheetFunction.CountA("C:C")
      For i = 1 To Count
        If sh.Cells(i + 1, 3) <> "" Then
        sh.Cells(i, 3) = Val(sh.Cells(i + 1, 3)) - 10                                                           难道这有问题吗?
        End If
      Next i
        wb.Close SaveChanges:=True    '直接关闭
    ex.Quit
  
        Set ex = Nothing
    Set wb = Nothing
    Set sh = NothingEnd Sub    可是我运行后,只把第一行减少了10,300变为290,其它行的金额数仍不变,请高手给指点,错在哪?好象没执行循环

解决方案 »

  1.   

    单步调试If sh.Cells(i + 1, 3) <> "" Then 
    msgbox Val(sh.Cells(i + 1, 3)) 
            sh.Cells(i, 3) = Val(sh.Cells(i + 1, 3)) - 10 
    msgbox sh.Cells(i, 3)
            End If 
      

  2.   

    Count =?
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  3.   


    '------------------------
        Count = ex.Application.WorksheetFunction.CountA("C:C") 
          For i = 1 To Count 
            If sh.Cells(i + 1, 3) <> "" Then 
            sh.Cells(i, 3) = Val(sh.Cells(i + 1, 3)) - 10                                                           难道这有问题吗? 
            End If 
          Next i 
    '---------------------------
    '上面一段改为这样式式
    '-------------------------
     i = 1
     While Len(sh.Cells(i, 3)) <> 0
       sh.Cells(i, 3) = val(sh.Cells(i, 3)) - 10
       i = i + 1
     Wend'--------------------------