如何调用输入的变量??

heruo 2004-08-29 04:45:32
1.我想在command1里想调用文本框里输入的变量,按输入的变量来执行程序。
2.限制文本框里输入的变量为数字。
...全文
139 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
heruo 2004-08-30
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim excel_app As Object
Dim excel_sheet As Object
Dim lh1, lh2, lh3 As String
Dim lh4, lh5, lh6 As Double
Dim lh7 As String
Dim i, s As Integer
Dim str
Screen.MousePointer = vbHourglass
DoEvents
Set excel_app = CreateObject("excel.application")
excel_app.Workbooks.Open FileName:=App.Path & "\book1"


If Val(excel_app.Application.Version) >= 8 Then
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If



i = Val(Text1.Text) //是这儿不对吗?
s = 4
a1:
Do While Not i = Val(Text2.Text)
excel_app.sheets(2).Select
Set excel_sheet = excel_app.ActiveSheet
lh1 = excel_sheet.cells(i, 3)
lh2 = excel_sheet.cells(i, 4)
lh3 = excel_sheet.cells(i, 5)
lh4 = excel_sheet.cells(i, 6)
lh5 = excel_sheet.cells(i, 7)
lh6 = excel_sheet.cells(i, 8)
lh7 = excel_sheet.cells(i, 14)

str = excel_sheet.cells(i, 22)
If excel_sheet.cells(i, 21) = "" Then

excel_app.sheets(3).Select
Set excel_sheet = excel_app.ActiveSheet
excel_sheet.cells(s, 2) = lh1
excel_sheet.cells(s, 3) = lh2
excel_sheet.cells(s, 4) = lh3
excel_sheet.cells(s, 5) = lh4
excel_sheet.cells(s, 6) = lh5
excel_sheet.cells(s, 7) = lh6
excel_sheet.cells(s, 8) = lh7
i = i + 1
s = s + 1
GoTo a1
Else
i = i + 1
GoTo a1
End If
Loop

excel_app.activeworkbook.save
MsgBox "已成功!"
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
Screen.MousePointer = vbDefault
End Sub


Private Sub Text1_Change()
Dim keyascii
If (keyascii > 47 And keyascii < 58) Or keyascii = 8 Or keyascii = 46 Then
keyascii = keyascii
Else
keyascii = 0
End If
End Sub


Private Sub Text2_Change()

End Sub


我想把text1传递给i, text2传递给S ,并限制text1和text2输入的变量为数字。

我上面的代码不知错在哪呢。
Andy__Huang 2004-08-29
  • 打赏
  • 举报
回复
這裡是可以輸入數字,也可以輸入小數點,但小數點只能輸入一次

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
If KeyAscii = 46 And InStr(Text1.Text, ".") > 0 Then
KeyAscii = 0
End If
End Sub
hnzhc 2004-08-29
  • 打赏
  • 举报
回复
第一个问题 请参考 BlueBeer(1win) ( ) 信誉:600000

第二个问题 请参考 SKer(海) ( ) 信誉:80000
lxxldd 2004-08-29
  • 打赏
  • 举报
回复
第一个问题 请参考 BlueBeer(1win) ( ) 信誉:132

第二个问题 请参考 SKer(海) ( ) 信誉:100
Mars.CN 2004-08-29
  • 打赏
  • 举报
回复
If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Or KeyAscii = 46 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If


比较不错.
BlueBeer 2004-08-29
  • 打赏
  • 举报
回复
sub command1_click()
if isnumeric(text1.text) then
select case val(text1.text)
case 123
'输入的是123,执行相应的代码写在这里
case 456
'输入的是456,执行相应的代码写在这里
case else
'输入的不是123也不是456,执行相应的代码写在这里
end select
else
msgbox "不是数字"
end if
end sub
laviewpbt 2004-08-29
  • 打赏
  • 举报
回复
private sub command1_click()
msgbox text1.text
end sub
SKer 2004-08-29
  • 打赏
  • 举报
回复
1 问题没有看懂,请详细说明

2
利用keyascii在keypress事件中写代码

If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Or KeyAscii = 46 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If
helanshan 2004-08-29
  • 打赏
  • 举报
回复
val(text1.text) '数字字符串转换为数值
heruo 2004-08-29
  • 打赏
  • 举报
回复
那位高手说一下呀,解决马上结帖。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧