VBA学习笔记 3

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一些最近可能用到的函数或者表达式的测试, 第一二个的测试数据如下:</span>

1. 检测第三列所列举的动作,并把相应的值填入a列中,如果非法则警告。

用到的函数有switch, msgbox, 代码如下:

Sub test1()
    'check the value of Ci and fill out a corresponding value in Ai
    ' if invalid then show a message, the result is as below
    'Insert
    'Delete
    'Update
    'show a message  states 'invalid action'
    
    Dim strC As String
    For i = 1 To 4
         strC = Sheet1.Cells(i, 3).Value
         Sheet1.Cells(i, 1).Value = Switch(LCase(strC) = "add", "Insert", _
                                             LCase(strC) = "delete", "Delete", _
                                             LCase(strC) = "change", "Update")
         If Sheet1.Cells(i, 1).Value = "" Then
            MsgBox "Invalid action"
        End If
        Next i
End Sub

2. 判断输入的数据是否数字,如果不是则替换可能出现的的符号‘-’,并且把多去的空格去掉

用到的内置函数有IsNumeric(), Trim(), Replace()

Sub test2()
    ' process account number, if it's not numeric, trim spaces and replace the character "-" to "", the result is as below:
    '565266
    '4565.21
    '456521
    '456521
    
    For i = 1 To 4
        If IsNumeric(Sheet1.Cells(i, 4).Value) Then
            Debug.Print Trim(Sheet1.Cells(i, 4).Value)
        Else
            Debug.Print Replace(Trim(Sheet1.Cells(i, 4).Value), "-", "")
        End If
        Next i
End Sub


3.  测试函数的参数, byVal, byRef 

Sub test3()
    ' test the difference of byVal and byRef when using fuctions, the result is as below
    '10
    '20
    '20
    '10
    
    Dim a  As Integer
    Dim b As Integer
        a = 10
        b = 20
        ' swap a and b using swapbyval
        If swapbyval(a, b) Then
            Debug.Print a
            Debug.Print b
       End If
       ' swap a and b using swapbyRef
       If swapByRef(a, b) Then
            Debug.Print a
            Debug.Print b
       End If
End Sub
'swap a and b byRef
Function swapByRef(ByRef a As Integer, ByRef b As Integer) As Boolean
    Dim temp As Integer
    temp = a
    a = b
    b = temp
    swapByRef = True
End Function
'swap a and b byVal
Function swapbyval(ByVal a As Integer, ByVal b As Integer) As Boolean
    Dim temp As Integer
    temp = a
    a = b
    b = temp
    swapbyval = True
End Function



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常好的问题!让我来回答你的问题。 首先,关于VBA学习笔记(一):自动添加代码,你可以使用VBA的宏来自动添加代码。具体步骤如下: 1. 打开你要添加代码的Excel文件,并按下Alt + F11进入VBA编辑器界面。 2. 在左侧的“项目资源管理器”窗格中,展开你的Workbook,并双击“这台电脑”(或“Microsoft Excel 对象”),打开相应的代码窗口。 3. 在代码窗口的顶部,选择“工具” > “宏” > “录制新宏”。 4. 在弹出的“新建宏”对话框中,为新宏命名,并选择一个快捷键,以便日后调用。 5. 点击“确定”后,VBA会自动开始录制你的操作。 6. 在代码窗口中,添加你需要自动执行的代码。 7. 完成代码添加后,点击“停止录制”按钮,保存你的新宏。 接下来,关于VBA修改注册表,你可以使用VBA的“WshShell”对象来实现。具体步骤如下: 1. 在VBA编辑器中,打开你要修改注册表的代码窗口。 2. 在代码窗口的顶部,添加以下代码: ``` Dim WshShell As Object Set WshShell = CreateObject("WScript.Shell") ``` 这将创建一个名为“WshShell”的对象,用于执行后续的注册表修改操作。 3. 接下来,你可以使用该对象的“RegWrite”方法来修改注册表。例如,以下代码将修改Windows启动时自动运行的程序列表: ``` WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\MyApp", "C:\MyApp.exe", "REG_SZ" ``` 该代码将在“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run”键下添加一个名为“MyApp”的值,其值为“C:\MyApp.exe”,类型为“REG_SZ”。 4. 完成代码添加后,保存并执行你的代码。 希望这些步骤对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值