一道excel的vb题,求助

来自:经验分享    更新日期:早些时候
一个简单的VB操作EXCEL的问题~

Sheets(sheet1).Activate
改为
xlbook.Sheets(sheet1).Activate
如果还不行,请粘出其他相关代码才能分析

Dim appexcel As Object'定义应用程序(Excel)
Dim wbmybook As Object'定义工作簿
Dim wsmysheet As Object'定义工作表
Set appexcel = CreateObject("excel.application") '创建Excel应用程序对象
Set wbmybook = appexcel.workbooks.Open(app.path & "\1.xls") '添加工作簿,假设你的Excel文件放在程序所在的文件夹下,文件名为1.xls,当把文件拷到别的电脑上时,只要保证1.xls在同意文件夹下就行了
Set wsmysheet = appexcel.worksheets("sheet1")'假设你的工作表名称为sheet1
dim sum as double'定义存放结果的变量
sum=0
for i=1 to 10
sum=sum+wbmysheet.cells(i,1)
next i
'更多关于vb调用Excel的参数可以发一个电子书给你

e:随机生成1~4的随机数,决定加减乘除哪类运算

a、b产生20以内数

c、d产生10以内数

代码如下:

Dim a, b, c, d, e

Private Sub CommandButton1_Click()

e = Int((4 * Rnd) + 1)

a = Int((20 * Rnd) + 1)

b = Int((20 * Rnd) + 1)

c = Int((10 * Rnd) + 1)

d = Int((10 * Rnd) + 1)

Cells(4, 12).Value = ""

Cells(4, 13).Value = ""

Select Case e

Case 1

Cells(4, 11).Value = a & "+" & b & "="

Case 2

If a < b Then

Cells(4, 11).Value = b & "-" & a & "="

Else

Cells(4, 11).Value = a & "-" & b & "="

End If

Case 3

Cells(4, 11).Value = c & "×" & d & "="

Case 4

If c < d Then

Cells(4, 11).Value = d - (d Mod c) & "÷" & c & "="

Else

Cells(4, 11).Value = c - (c Mod d) & "÷" & d & "="

End If

End Select

End Sub

Private Sub CommandButton2_Click()

If Cells(4, 12) = "" Then

MsgBox ("请输入答案")

Else

Select Case e

Case 1

If Cells(4, 12).Value = a + b Then

Cells(4, 13).Value = "正确"

Else

Cells(4, 13).Value = "错误!"

End If

Case 2

If Cells(4, 12).Value = Abs(a - b) Then

Cells(4, 13).Value = "正确"

Else

Cells(4, 13).Value = "错误!"

End If

Case 3

If Cells(4, 12).Value = c * d Then

Cells(4, 13).Value = "正确"

Else

Cells(4, 13).Value = "错误!"

End If

Case 4

If c < d Then

If Cells(4, 12).Value = (d - (d Mod c)) / c Then

Cells(4, 13).Value = "正确"

Else

Cells(4, 13).Value = "错误!"

End If

Else

If Cells(4, 12).Value = (c - (c Mod d)) / d Then

Cells(4, 13).Value = "正确"

Else

Cells(4, 13).Value = "错误!"

End If

End If

End Select

End If

End Sub



这么简单的程序求助就没意思了。
随生,要用到随机数函数,生成一个随机数,如加法,那用20减这个随机数,比得到的数小的数均可(可在这区间再随机任选一个)。显示两个数相加。
如乘法,用10除以这个数并取整,然后1至这个整数的数都可取(如随机数是2,10/2=5,1至5的数与2乘都不大于10)
思路就这样了,自己去实现吧。
当然还有其他思路……


一道excel的vb题,求助视频

相关评论:
  • 17554671529一道excel的vb题,求助
    单相饰e:随机生成1~4的随机数,决定加减乘除哪类运算 a、b产生20以内数 c、d产生10以内数 代码如下:Dim a, b, c, d, e Private Sub CommandButton1_Click()e = Int((4 * Rnd) + 1)a = Int((20 * Rnd) + 1)b = Int((20 * Rnd) + 1)c = Int((10 * Rnd) + 1)d = Int...

  • 17554671529求助:利用excel的vb编程功能 对单元格A1到A10简单求和
    单相饰打开你的Excel文件,按“Alt+F11”打开VBA编辑窗口,然后在左侧对应的Sheet上双击,右侧空白处粘贴下面的代码。关闭VBA窗口。然后按“Alt+F8”打开宏窗口,选择刚插入的宏,点击“执行”。Sub QH()Dim rg As RangeDim he As DoubleFor Each rg In Range("A1:A10") he = he + IIf(IsNumeric...

  • 17554671529求助:用VB在EXCEL中找出相同的数据
    单相饰假设数据在A1:D1000列,在E1输入:=IF(COUNTIF($A$1:$D$1000,A1)>=2,A1,"")然后右拉公式到H1,然后下拉到1000行.

  • 17554671529excel vb 一个消息窗口问题,比如检测了4个单元格abcd 假如a不是空的其...
    单相饰【更新了一下答案】刚才思考了一下下,感觉不需要用变量k。iStr初始值是空值,如果检测到空单元格的话就会给iStr赋值。所以检测完毕后,只要判断iStr是不是空值,就知道有没有检测到空单元格了。Sub 再次试一试()Dim iStr If Range("A1") = "" Then iStr = iStr & "公司名称、"If Range("C4"...

  • 17554671529在VB中操作Excel的问题
    单相饰'先在工程中引用Microsoft Excel 11.0 Object Library Dim R As String, i As Integer Dim sSource, sDestination As String Set mxlApp = New Excel.Application Set mxlApp = CreateObject("Excel.Application")R = "c:\\1.xls"H = 1 mxlApp.Visible = False Set mxlBook = mxlApp....

  • 17554671529求助!vb高手请解答。有关用excel输出vb计算出来的值得...
    单相饰FileName = App.Path & "\\计算"Set xlApp = New Excel.Application Set xlbook = xlApp.Workbooks.Add Set xlsheet = xlbook.Worksheets(1) '打开EXCEL工作表 xlApp.Visible = True xlsheet.Activate '激活工作表 t = 0 For i = 250 To 291.67 Step 1 For j = 500 To 583.33...

  • 17554671529VB判断EXCEL的问题
    单相饰1、循环建议使用 For each xlsheet in xlbook.worksheets 2、判断一个表是否存在可以这样写 on error resume Set wks = xlbook.Worksheets(xlsheet.name)If Not wks Is Nothing Then ‘表存在,拷贝 else '表不存在,建表,拷贝 End If Private Sub Button1_Click(ByVal sender As System.Object...

  • 17554671529关于excel自动删除空白行的VB的问题。
    单相饰If .Cells(R, 1) = "" Then 中.cells(R,1)改成.cells(R,3)就行了!

  • 17554671529excel的VB求解释
    单相饰Private Sub CommandButton1_Click()Dim i As Long '声明变量i为长整形Dim j As Integer '声明变量j为整数 j = 14 '赋值j=14 i = 1 '赋值i=1 Cells(j + 1, 5) = Cells(j + 1, 11) '赋值让行号为j+1,列号为5的单元格的值=行号为j+1,列号为11单元格...

  • 17554671529excel里面VB语言问题
    单相饰MyPath是一个自定义变量与ThisWorkbook.Path语句是不同的,需要你对它时行定义 如下 MyPath = "G:\\D文件夹\\C文件夹"MyPath = MyPath & "\\一览表.xlsm"如果MyPath使用的地方不是很多,还不如直接定义的好 MyPath = "G:\\D文件夹\\C文件夹\\一览表.xlsm"...

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网