Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2356|回复: 5

每次运行到这一句就报错,提示信息:“运行时错误1004,应用...

[复制链接]
发表于 2014-3-18 13:08 | 显示全部楼层 |阅读模式
每次运行到这一句就报错,提示信息:“运行时错误1004,应用程序定义或对象定义错误...Cells(c, 62).Value = ling
Private Sub CommandButton1_Click()
  Dim ling As Integer, yi As Integer, er As Integer, san As Integer, si As Integer, wu As Integer, liu As Integer, qi As Integer, ba As Integer, jiu As Integer
  Dim i As Integer, Hanghao As Integer, Bai As Integer, Shi As Integer, Ge As Integer, no1 As Integer
  Dim weihao As Integer
    Worksheets("一").Activate
      Hanghao = TextBox1.Value
        Bai = Sheets("一").Range("c1").Value
        Shi = Sheets("一").Range("d1").Value
        Ge = Sheets("一").Range("e1").Value
        For i = 3 To 209
          Cells(Hanghao + 1, i).Value = Cells(3, i).Value
          weihao = Worksheets("一").Cells(Hanghao, i).Value
          If weihao = Bai Then
              Cells(Hanghao, i).Interior.ColorIndex = 38
              no1 = Cells(Hanghao + 1, i).Value
              NO (no1)
            ElseIf weihao = Shi Then
              Cells(Hanghao, i).Interior.ColorIndex = 38
              no1 = Cells(Hanghao + 1, i).Value
              NO (no1)
            ElseIf weihao = Ge Then
              Cells(Hanghao, i).Interior.ColorIndex = 38
              no1 = Cells(Hanghao + 1, i).Value
              NO (no1)
            Else
          End If
        Next i
    UserForm1.Hide
  Cells(c, 62).Value = ling
  Cells(d, 62).Value = yi
  
End Sub
Private Sub CommandButton2_Click()
  UserForm1.Hide
End Sub
Public Function NO(no1 As Integer) As Integer
      Select Case no1
          Case 0
            ling = ling + 1
          Case 1
            yi = yi + 1
          Case 2
            er = er + 1
          Case 3
            san = san + 1
          Case 4
            si = si + 1
          Case 5
            wu = wu + 1
          Case 6
            liu = liu + 1
          Case 7
            qi = qi + 1
          Case 8
            ba = ba + 1
          Case 9
            jiu = jiu + 1
      End Select
  End Function

Book1.rar

24.05 KB, 下载次数: 2

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-3-18 13:13 | 显示全部楼层
回复

使用道具 举报

发表于 2014-3-18 13:19 | 显示全部楼层
本帖最后由 yuan1987 于 2014-3-18 13:24 编辑

改成这个" ling"  "yi",下边所有的都加英文状态下的引号。或者直接改成阿拉伯数字。
回复

使用道具 举报

 楼主| 发表于 2014-3-19 22:33 | 显示全部楼层

RE: 每次运行到这一句就报错,提示信息:“运行时错误1004,应用...

各位大师帮帮我啊
回复

使用道具 举报

发表于 2014-3-19 23:23 | 显示全部楼层
jmgyb 发表于 2014-3-19 22:33
各位大师帮帮我啊

报错的原因是c,d两个变量未赋值,此时为0,所以Cells(0,62)单元格对象不存在,从而导致错误。
你的代码,没有领会变量的作用域和生存期,基本是在胡乱使用。
先大致上给你改下,你自己再慢慢琢磨吧!
  1.   Dim ling As Integer, yi As Integer, er As Integer, san As Integer, si As Integer
  2.   Dim wu As Integer, liu As Integer, qi As Integer, ba As Integer, jiu As Integer


  3. Private Sub CommandButton1_Click()
  4.   Dim i As Integer, Hanghao As Integer, Bai As Integer, Shi As Integer, Ge As Integer, no1 As Integer
  5.   Dim weihao As Integer
  6.     Worksheets("一").Activate
  7.       Hanghao = TextBox1.Value
  8.         Bai = Sheets("一").Range("c1").Value
  9.         Shi = Sheets("一").Range("d1").Value
  10.         Ge = Sheets("一").Range("e1").Value
  11.         For i = 3 To 209
  12.           Cells(Hanghao + 1, i).Value = Cells(3, i).Value
  13.           weihao = Worksheets("一").Cells(Hanghao, i).Value
  14.           If weihao = Bai Then
  15.               Cells(Hanghao, i).Interior.ColorIndex = 38
  16.               no1 = Cells(Hanghao + 1, i).Value
  17.               NO (no1)
  18.             ElseIf weihao = Shi Then
  19.               Cells(Hanghao, i).Interior.ColorIndex = 38
  20.               no1 = Cells(Hanghao + 1, i).Value
  21.               NO (no1)
  22.             ElseIf weihao = Ge Then
  23.               Cells(Hanghao, i).Interior.ColorIndex = 38
  24.               no1 = Cells(Hanghao + 1, i).Value
  25.               NO (no1)
  26.             Else
  27.           End If
  28.         Next i
  29.     UserForm1.Hide
  30.   
  31.   
  32.   
  33.   Cells(62, "C").Value = ling   'Cells(c, 62).Value = ling
  34.   Cells(62, "D").Value = yi     'Cells(d, 62).Value = yi
  35. End Sub

  36. Private Sub CommandButton2_Click()
  37.   UserForm1.Hide
  38. End Sub

  39. Public Function NO(no1 As Integer) As Integer
  40.       Select Case no1
  41.           Case 0
  42.             ling = ling + 1
  43.           Case 1
  44.             yi = yi + 1
  45.           Case 2
  46.             er = er + 1
  47.           Case 3
  48.             san = san + 1
  49.           Case 4
  50.             si = si + 1
  51.           Case 5
  52.             wu = wu + 1
  53.           Case 6
  54.             liu = liu + 1
  55.           Case 7
  56.             qi = qi + 1
  57.           Case 8
  58.             ba = ba + 1
  59.           Case 9
  60.             jiu = jiu + 1
  61.       End Select
  62.   End Function
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-3-25 11:29 | 显示全部楼层
为什么运行时ling、yi两个变量没有数值呢,各位帮帮我啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-6 08:27 , Processed in 0.519584 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表