如何用Excel Vba编写个计算器 要求是:编写个和电脑上一样的计算器。

如何用Excel Vba编写个计算器 要求是:编写个和电脑上一样的计算器。,第1张

1、首先要做需求分析

2、设计窗体 像窗体添加元素(按钮、标签、文本框等等)

3、为每个按钮赋值 像 加减乘除 阿拉伯数值等等

4、为每个按钮添加单击事件(就是计算公式,点击加号时做加法)

5、将结果在文本框显示

6、其它按钮像根号 平方也一样 把符号复制到按钮上,然后添加事件就好

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If [f6] <>"=" Then Exit Sub

Static temp

Static w, j

r1 = Target.Row

c1 = Target.Column

If r1 >2 And r1 <7 And c1 >3 And c1 <7 Then

a = ""

Select Case r1 &c1

Case 34

a = 1

Case 35

a = 2

Case 36

a = 3

Case 44

a = 4

Case 45

a = 5

Case 46

a = 6

Case 54

a = 7

Case 55

a = 8

Case 56

a = 9

Case 64

a = 0

Case 65

temp = [d1]

[d1] = 0

j = 1

Case 66

If j = 1 Then

[d1] = [d1] + temp

w = 0

j = 0

Else

[d1] = 0

End If

End Select

If a <>"" Then

If w = 1 Then

[d1] = [d1] &a

Else

[d1] = a

w = 1

End If

End If

[a1].Select

End If

End Sub

Sub 初始化计算器外观()

Cells.Clear

Cells.Select

Selection.RowHeight = 44.25

Range("D3").Select

ActiveCell.FormulaR1C1 = "1"

Range("E3").Select

ActiveCell.FormulaR1C1 = "2"

Range("F3").Select

ActiveCell.FormulaR1C1 = "3"

Range("D4").Select

ActiveCell.FormulaR1C1 = "4"

Range("E4").Select

ActiveCell.FormulaR1C1 = "5"

Range("F4").Select

ActiveCell.FormulaR1C1 = "6"

Range("D5").Select

ActiveCell.FormulaR1C1 = "7"

Range("E5").Select

ActiveCell.FormulaR1C1 = "8"

Range("F5").Select

ActiveCell.FormulaR1C1 = "9"

Range("D6").Select

ActiveCell.FormulaR1C1 = "0"

Range("E6").Select

ActiveCell.FormulaR1C1 = "+"

Range("F6").Select

ActiveCell.FormulaR1C1 = "="

Range("D1:F2").Select

With Selection

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

.WrapText = False

.Orientation = 0

.AddIndent = False

.IndentLevel = 0

.ShrinkToFit = False

.ReadingOrder = xlContext

.MergeCells = False

End With

Selection.Merge

Range("D1:F6").Select

With Selection.Font

.Name = "宋体"

.Size = 36

.Strikethrough = False

.Superscript = False

.Subscript = False

.OutlineFont = False

.Shadow = False

.Underline = xlUnderlineStyleNone

.ThemeColor = xlThemeColorLight1

.TintAndShade = 0

.ThemeFont = xlThemeFontMinor

End With

With Selection

.HorizontalAlignment = xlGeneral

.VerticalAlignment = xlCenter

.WrapText = False

.Orientation = 0

.AddIndent = False

.IndentLevel = 0

.ShrinkToFit = False

.ReadingOrder = xlContext

End With

With Selection

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

.WrapText = False

.Orientation = 0

.AddIndent = False

.IndentLevel = 0

.ShrinkToFit = False

.ReadingOrder = xlContext

End With

Selection.Font.Bold = True

With Selection.Font

.Color = -16776961

.TintAndShade = 0

End With

With Selection.Interior

.Pattern = xlNone

.TintAndShade = 0

.PatternTintAndShade = 0

End With

With Selection.Interior

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.ThemeColor = xlThemeColorAccent6

.TintAndShade = 0.399975585192419

.PatternTintAndShade = 0

End With

Range("D1:F6").Select

Selection.Borders(xlDiagonalDown).LineStyle = xlNone

Selection.Borders(xlDiagonalUp).LineStyle = xlNone

With Selection.Borders(xlEdgeLeft)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

With Selection.Borders(xlEdgeTop)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

With Selection.Borders(xlEdgeBottom)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

With Selection.Borders(xlEdgeRight)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

With Selection.Borders(xlInsideVertical)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

With Selection.Borders(xlInsideHorizontal)

.LineStyle = xlContinuous

.ColorIndex = 0

.TintAndShade = 0

.Weight = xlThin

End With

Range("D1:F2").Select

End Sub

Private Sub Worksheet_Activate()

初始化计算器外观

End Sub

把上面代码贴到一个表的模块上,返回就可以使用了

Private Sub CommandButton1_Click()

Select Case ComboBox1.Text

Case "+"

CommandButton1.Caption = Val(TextBox1.Text) + Val(TextBox2.Text)

Case "-"

CommandButton1.Caption = Val(TextBox1.Text) - Val(TextBox2.Text)

Case "*"

CommandButton1.Caption = Val(TextBox1.Text) * Val(TextBox2.Text)

Case "/"

CommandButton1.Caption = Val(TextBox1.Text) / Val(TextBox2.Text)

End Select

End Sub

Private Sub UserForm_Initialize()

ComboBox1.AddItem "+"

ComboBox1.AddItem "-"

ComboBox1.AddItem "*"

ComboBox1.AddItem "/"

End Sub

是不是这效果


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10866802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存