我在窗体上画了42个textbox(index0-41),还有一个button   要求当我按下button的时候某个excel表(和工程在同一个目录下 abc.xls)中的"A3","B4","c8"...............等42个框格中的42个文本值(excel中的这些数值基本都是由公式得出的)被依次读到textbox(index0-41),且把textbox中读到的值设为不可更改。
   另外,最好再把button再设一下,也就是当把42个数值读到textbox中后button不可再点击!就是button从始至终可以点击一次!
谢谢

解决方案 »

  1.   


    '怎样控制textbox的只读,要使textbox中的数据不能改变(删除或修改),在属性里我没有找到'有相关的方法吗?'解答:Textbox.Enabled = False,直接修改控件属性都行。'又问:这样还不行,因为Textbox在显示上就灰显了,我想只让它不可改变值,在显示上还是原来的形式。'解答:那就用Label代替,设置BackColor和SpecialEffect属性。
    '先引用excel *.0 object library
    Private Sub Command1_Click() If Text1(0) = "" Or Text1(2) = "" Then
        Dim ex As Excel.Application
        Dim wb As Excel.Workbook
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open("c:\1.xls")  ' 改成你的文件路径
        Set sh = wb.Sheets(1)
           Text1(0) = Range("A3").Value
           Text1(1) = Range("B4").Value
           Text1(2) = Range("C8").Value
           Text1(3) = Range("D3").Value
           '还有自己写.......
        wb.Close
        ex.Quit
        'ex.Visible = True
        Set ex = Nothing
        Set wb = Nothing
        Set sh = Nothing
     Else
        MsgBox "你不能改变数据"
     End If
        
    End SubPrivate Sub Form_Load()
    Text1(0) = ""
    Text1(1) = ""
    Text1(2) = ""
    Text1(3) = ""
    Text1(0).Enabled = False
    Text1(1).Enabled = False
    Text1(2).Enabled = False
    Text1(3).Enabled = False
    End Sub
      

  2.   

    请大家再帮我修改一下,上面的程序是正确的,但是我现在希望button可以无限次被点击,每次点击以后都执行一次读取过程,但是读到的数据仍然为只读
      

  3.   

    上面的程序就是你每点一下button就重新读取一次阿
      

  4.   

    '先引用excel *.0 object library
    Private Sub Command1_Click()
        Dim ex As Excel.Application
        Dim wb As Excel.Workbook
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open("c:\1.xls")  ' 改成你的文件路径
        Set sh = wb.Sheets(1)
           Text1(0) = Range("A3").Value
           Text1(1) = Range("B4").Value
           Text1(2) = Range("C8").Value
           Text1(3) = Range("D3").Value
           '还有自己写.......
        wb.Close
        ex.Quit
        'ex.Visible = True
        Set ex = Nothing
        Set wb = Nothing
        Set sh = Nothing    
    End SubPrivate Sub Form_Load()
    Text1(0) = ""
    Text1(1) = ""
    Text1(2) = ""
    Text1(3) = ""
    Text1(0).Enabled = False
    Text1(1).Enabled = False
    Text1(2).Enabled = False
    Text1(3).Enabled = False
    End Sub
      

  5.   

    goosen 先生,谢谢你的支持:
        当我引用下面代码的时候,button仍然只可以点击一次,因为当我点击第二次的时候出现一个错误“实时错误"1004"  对象"Range"的方法"_Global"失败” 如果把Text1(0).Enabled = False去掉就可以了,但是我不希望
                                                  Text1(1).Enabled = False
                                                  Text1(2).Enabled = False
                                                  Text1(3).Enabled = False
    这样做,textbox的内容不可以更改,请您再想想办法!!
    Private Sub Command1_Click()
        Dim ex As Excel.Application
        Dim wb As Excel.Workbook
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open("c:\1.xls")  ' 改成你的文件路径
        Set sh = wb.Sheets(1)
           Text1(0) = Range("A3").Value
           Text1(1) = Range("B4").Value
           Text1(2) = Range("C8").Value
           Text1(3) = Range("D3").Value
           '还有自己写.......
        wb.Close
        ex.Quit
        'ex.Visible = True
        Set ex = Nothing
        Set wb = Nothing
        Set sh = Nothing    
    End SubPrivate Sub Form_Load()
    Text1(0) = ""
    Text1(1) = ""
    Text1(2) = ""
    Text1(3) = ""
    Text1(0).Enabled = False
    Text1(1).Enabled = False
    Text1(2).Enabled = False
    Text1(3).Enabled = False
    End Sub
        
      

  6.   

    我说错了把 Text1(0).Enabled = False去掉也不行     
              Text1(1).Enabled = False
              Text1(2).Enabled = False 
             Text1(3).Enabled = False 
      

  7.   

    '先引用excel *.0 object library
    Private Sub Command1_Click()
    Text1(0) = ""
    Text1(1) = ""
    Text1(2) = ""
    Text1(3) = ""
    Text1(0).Enabled = False
    Text1(1).Enabled = False
    Text1(2).Enabled = False
    Text1(3).Enabled = False
        Dim ex As Excel.Application
        Dim wb As Excel.Workbook
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open("c:\1.xls")  ' 改成你的文件路径
        Set sh = wb.Sheets(1)
           Text1(0) = Range("A3").Value
           Text1(1) = Range("B4").Value
           Text1(2) = Range("C8").Value
           Text1(3) = Range("D3").Value
           '还有自己写.......
        wb.Close
        ex.Quit
        'ex.Visible = True
        Set ex = Nothing
        Set wb = Nothing
        Set sh = NothingEnd Sub