vba listbox控件大小变_VBA入门之---与代码亲密接触开场舞

本文介绍了如何使用Excel VBA创建交互式对话框。通过添加和编程命令按钮,展示了如何显示自定义消息以及获取单元格信息。通过双击按钮进入VBA编辑器,学习如何编写和理解基本的VBA代码,例如MsgBox函数和变量的使用。此外,还演示了如何读取单元格内容并在对话框中显示,以及如何操作其他工作表的单元格。这是一个适合Excel VBA初学者的实践教程。
摘要由CSDN通过智能技术生成
229e08b89f3924018ef6dbf0f03b9e31.png

去除粗枝繁叶,留一根主干,作攀天之梯,目标是那金碧辉煌的VBA殿堂。

一路上,两旁都是漂亮的姑娘,不要三心二意,请跟我走。

闲话少说,言归正传。

打开Excel,新建一工作簿。

如果你的工具栏中找不到“控件工具箱”,请打开它。控件工具箱可悬浮在工作表中,也可停靠在窗口的四周。

d2394381c34bc5feebec6a8e38c7d7a7.png

1点击命令按钮,在工作表中拖动鼠标至适合大小。

2按钮出来了。通过右键菜单,可以编辑它的文字。

3c982799ac2621357b322b6aa734f1e6.png

4设置属性虽然有趣,但总不过瘾,来点刺激的。双击按钮,眼前是另一片天地,它叫做VB编辑器(或称VBE),VBA的一切神奇将从这里诞生。

7cdf704eb0023a3799a4f1dc34decbd7.png

5依样画葫芦。在光标处输入下面一行代码:

QUOTE:

MsgBox "哈哈,我会 VBA 啦……"

msgbox 不拘大小写,后面有一个空格,引号是半角引号。

输入完毕后点击工具按钮返回Excel窗口。

2cf2654a62c7993db743aeee811e9683.png

6点击控件工具箱中的“退出设计模式”按钮。“芝麻开门”按钮周围的小圈圈没了,转换成运行模式。

1f14e7728a62dd58a53dbbd37bbd9f4d.png

7点击按钮“Excel 教案”,这就是代码运行的效果图,一个系统对话框。

ace4082c40caf915fb2a36a9b90b5dc4.png

8现在,由工作表界面切换到VBE界面,下列方式可任选一种:

F按Alt+TAB选择VBE图标,松开复合键。

工作表签右键菜单-查看代码。

F菜单:工具-宏-VB编辑器。

FAlt+F11。

F双击控件。

9将代码修改为:

QUOTE:

Private Sub CommandButton1_Click()

ctxt = "哈哈,我会 VBA 啦……"

MsgBox ctxt, 64, "宣告"

End Sub

实际修改的是中间的黑色字体部分。

代码中,蓝色处是一个空格,粉红色处是一个逗号。

命令后面、命令与命令之间,间隔一个以上的空格。

参数与参数之间,用逗号分开。

运行代码的办法,除了在工作表上点击按钮,还可以在VBE窗口中直接运行,把光标定位到代码首尾之间(从“Private Sub CommandButton1_Click()”到“End Sub”),如下图所示执行菜单命令或按F8,逐行执行代码。

如果执行“运行-运行子过程”命令(按F5),将连续执行过程首尾之间的全部代码。

10代码的解释:

ctxt叫做自定义变量,就像我们代数中常用的x、y、z一样。

ctxt = "……" 叫做赋值,跟代数中的赋值是一样的。

11MsgBox是什么东西?我们总是想知道。

请选择MsgBox(实际上,只需把光标定位到命令中的任意位置均可),按F1键,系统将弹出帮助窗口并显示该命令的帮助。

9bd0057ceeb5170bed9cff84b63a29e0.png

12在B2:D4单元格输入一些数据备用。为工作表添加第二个按钮。

这个按钮的功能是:用鼠标点击这些单元格,然后点击按钮,像上例一样弹出一个对话框,显示的内容为这个单元格的名称和内容。

13双击按钮。在光标闪烁处,输入代码:

msgbox "你点击的单元格是:" &

(蓝色位置表示空格),接下来要输入的代码是“ActiveCell.Address”,输入这些代码可以使用键盘输入,也可以从属性列表中选择。按Ctrl+J,在代码窗口中弹出一个下拉列表,就是属性/方法列表,将光标移动到ActiveCell,然后点击键盘上的小数点“.”,结果ActiveCell连同小数点被输入到代码窗口中。

口中继续显示属性列表中,选择属性“Address”,按空格键输入该属性。

接着,输入“& Chr(13) _”,回车换行,继续输入:

& "该单元格的值等于:" & ActiveCell.Value, 64, "神算子"

完整的代码是:

QUOTE:

Private Sub CommandButton2_Click()

MsgBox "你点击的单元格是:" & ActiveCell.Address & Chr(13) _

& "该单元格的值等于:" & ActiveCell.Value, 64, "神算子"

End Sub

辛苦了。返回Excel,退出设计模式,你可以自己试试看。

14.如何读写其它工作表的单元格呢?

把工作表sheet2重命名为VBA2,添加第四个按钮“暗渡陈仓”,事件代码为:

QUOTE:

Sheets("VBA2").Range("D5").Value = "明修栈道,暗渡陈仓。"

以上就是今天要和大家分享的技巧,希望对大家有所帮助,祝各位一天好心情!

唯有不断学习,才能不被淘汰!

Excel中每一个方法都有特定的用途,不是他们没有用处,只是你不了解或者暂时用不着,建议你收藏起来,万一哪天用着呢?

1f22b426aeadbf02b424a2e95fb663ee.png
VB窗体控件大小随窗体大小化自動調整 有时窗体化后,如改分辨率后控件大小却不能随之改。手工代码调整实在麻烦,下面的模块实现自动查找窗体上控件并使其改大小以适应窗体化。 在Form的Resize事件中调用函数Resize_All就能实现控件自动调整大小,如: Private Sub Form_Resize() Dim H, i As Integer On Error Resume Next Resize_ALL Me 'Me是窗体名,Form1,Form2等等都可以 End Sub 在模块中添加以下代码: Public Type ctrObj Name As String Index As Long Parrent As String Top As Long Left As Long Height As Long Width As Long ScaleHeight As Long ScaleWidth As Long End Type Private FormRecord() As ctrObj Private ControlRecord() As ctrObj Private bRunning As Boolean Private MaxForm As Long Private MaxControl As Long Private Const WM_NCLBUTTONDOWN = &HA1 Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function ReleaseCapture Lib "USER32" () As Long Function ActualPos(plLeft As Long) As Long If plLeft < 0 Then ActualPos = plLeft + 75000 Else ActualPos = plLeft End If End Function Function FindForm(pfrmIn As Form) As Long Dim i As Long FindForm = -1 If MaxForm > 0 Then For i = 0 To (MaxForm - 1) If FormRecord(i).Name = pfrmIn.Name Then FindForm = i Exit Function End If Next i End If End Function Function AddForm(pfrmIn As Form) As Long Dim FormControl As Control Dim i As Long ReDim Preserve FormRecord(MaxForm + 1) FormRecord(MaxForm).Name = pfrmIn.Name FormRecord(MaxForm).Top = pfrmIn.Top FormRecord(MaxForm).Left = pfrmIn.Left FormRecord(MaxForm).Height = pfrmIn.Height FormRecord(MaxForm).Width = pfrmIn.Width FormRecord(MaxForm).ScaleHeight = pfrmIn.ScaleHeight FormRecord(MaxForm).ScaleWidth = pfrmIn.ScaleWidth AddForm = MaxForm MaxForm = MaxForm + 1 For Each FormControl In pfrmIn i = FindControl(FormControl, pfrmIn.Name) If i < 0 Then i = AddControl(FormControl, pfrmIn.Name) End If Next FormControl End Function Function FindControl(inControl As Control, inName As String) As Long Dim i As Long FindControl = -1 For i = 0 To (MaxControl - 1) If ControlRecord(i).Parrent = inName Then If ControlRecord(i).Name = inControl.Name Then On Error Resume Next If ControlRecord(i).Index = inControl.Index Then FindControl = i Exit Function End If On Error GoTo 0 End If End If Next i End Function Function AddControl(inControl As Control, inName As String) As Long ReDim Preserve ControlRecord(MaxControl + 1) On Error Resume Next ControlRecord(MaxControl).Name = inControl.Name ControlRecord(MaxControl).Index = inControl.Index ControlRecord(MaxControl).Parrent = inName If TypeOf inControl Is Line Then ControlRecord(MaxControl).Top = inControl.Y1 ControlRecord(MaxControl).Left = ActualPos(inControl.X1) ControlRecord(MaxControl).Height = inControl.Y2 ControlRecord(MaxControl).Width = ActualPos(inControl.X2) Else ControlRecord(MaxControl).Top = inControl.Top ControlRecord(MaxControl).Left = ActualPos(inControl.Left) ControlRecord(MaxControl).Height = inControl.Height ControlRecord(MaxControl).Width = inControl.Width End If inControl.IntegralHeight = False On Error GoTo 0 AddControl = MaxControl MaxControl = MaxControl + 1 End Function Function PerWidth(pfrmIn As Form) As Long Dim i As Long i = FindForm(pfrmIn) If i < 0 Then i = AddForm(pfrmIn) End If PerWidth = (pfrmIn.ScaleWidth * 100) \ FormRecord(i).ScaleWidth End Function Function PerHeight(pfrmIn As Form) As Double Dim i As Long i = FindForm(pfrmIn) If i < 0 Then i = AddForm(pfrmIn) End If PerHeight = (pfrmIn.ScaleHeight * 100) \ FormRecord(i).ScaleHeight End Function Public Sub ResizeControl(inControl As Control, pfrmIn As Form) On Error Resume Next Dim i As Long Dim widthfactor As Single, heightfactor As Single Dim minFactor As Single Dim yRatio, xRatio, lTop, lLeft, lWidth, lHeight As Long yRatio = PerHeight(pfrmIn) xRatio = PerWidth(pfrmIn) i = FindControl(inControl, pfrmIn.Name) If inControl.Left < 0 Then lLeft = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000) Else lLeft = CLng((ControlRecord(i).Left * xRatio) \ 100) End If lTop = CLng((ControlRecord(i).Top * yRatio) \ 100) lWidth = CLng((ControlRecord(i).Width * xRatio) \ 100) lHeight = CLng((ControlRecord(i).Height * yRatio) \ 100) If TypeOf inControl Is Line Then If inControl.X1 < 0 Then inControl.X1 = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000) Else inControl.X1 = CLng((ControlRecord(i).Left * xRatio) \ 100) End If inControl.Y1 = CLng((ControlRecord(i).Top * yRatio) \ 100) If inControl.X2 < 0 Then inControl.X2 = CLng(((ControlRecord(i).Width * xRatio) \ 100) - 75000) Else inControl.X2 = CLng((ControlRecord(i).Width * xRatio) \ 100) End If inControl.Y2 = CLng((ControlRecord(i).Height * yRatio) \ 100) Else inControl.Move lLeft, lTop, lWidth, lHeight inControl.Move lLeft, lTop, lWidth inControl.Move lLeft, lTop End If End Sub Public Sub ResizeForm(pfrmIn As Form) Dim FormControl As Control Dim isVisible As Boolean Dim StartX, StartY, MaxX, MaxY As Long Dim bNew As Boolean If Not bRunning Then bRunning = True If FindForm(pfrmIn) < 0 Then bNew = True Else bNew = False End If If pfrmIn.Top < 30000 Then isVisible = pfrmIn.Visible On Error Resume Next If Not pfrmIn.MDIChild Then On Error GoTo 0 ' ' pfrmIn.Visible = False Else If bNew Then StartY = pfrmIn.Height StartX = pfrmIn.Width On Error Resume Next For Each FormControl In pfrmIn If FormControl.Left + FormControl.Width + 200 > MaxX Then MaxX = FormControl.Left + FormControl.Width + 200 End If If FormControl.Top + FormControl.Height + 500 > MaxY Then MaxY = FormControl.Top + FormControl.Height + 500 End If If FormControl.X1 + 200 > MaxX Then MaxX = FormControl.X1 + 200 End If If FormControl.Y1 + 500 > MaxY Then MaxY = FormControl.Y1 + 500 End If If FormControl.X2 + 200 > MaxX Then MaxX = FormControl.X2 + 200 End If If FormControl.Y2 + 500 > MaxY Then MaxY = FormControl.Y2 + 500 End If Next FormControl On Error GoTo 0 pfrmIn.Height = MaxY pfrmIn.Width = MaxX End If On Error GoTo 0 End If For Each FormControl In pfrmIn ResizeControl FormControl, pfrmIn Next FormControl On Error Resume Next If Not pfrmIn.MDIChild Then On Error GoTo 0 pfrmIn.Visible = isVisible Else If bNew Then pfrmIn.Height = StartY pfrmIn.Width = StartX For Each FormControl In pfrmIn ResizeControl FormControl, pfrmIn Next FormControl End If End If On Error GoTo 0 End If bRunning = False End If End Sub Public Sub SaveFormPosition(pfrmIn As Form) Dim i As Long If MaxForm > 0 Then For i = 0 To (MaxForm - 1) If FormRecord(i).Name = pfrmIn.Name Then FormRecord(i).Top = pfrmIn.Top FormRecord(i).Left = pfrmIn.Left FormRecord(i).Height = pfrmIn.Height FormRecord(i).Width = pfrmIn.Width Exit Sub End If Next i AddForm (pfrmIn) End If End Sub Public Sub RestoreFormPosition(pfrmIn As Form) Dim i As Long If MaxForm > 0 Then For i = 0 To (MaxForm - 1) If FormRecord(i).Name = pfrmIn.Name Then If FormRecord(i).Top < 0 Then pfrmIn.WindowState = 2 ElseIf FormRecord(i).Top < 30000 Then pfrmIn.WindowState = 0 pfrmIn.Move FormRecord(i).Left, FormRecord(i).Top, FormRecord(i).Width, FormRecord(i).Height Else pfrmIn.WindowState = 1 End If Exit Sub End If Next i End If End Sub Public Sub Resize_ALL(Form_Name As Form) Dim OBJ As Object For Each OBJ In Form_Name ResizeControl OBJ, Form_Name Next OBJ End Sub Public Sub DragForm(frm As Form) On Local Error Resume Next Call ReleaseCapture Call SendMessage(frm.hwnd, WM_NCLBUTTONDOWN, 2, 0) End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值