Excel VBA - 使用已调用的子字符串 [英] Excel VBA - Use an existing string in called sub

查看:274
本文介绍了Excel VBA - 使用已调用的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的,所以提前道歉



我在Excel中的一半用户形式,我正在尝试从我的代码使用调用 - 我有12个按钮都做同样的事情,唯一的区别是每个按钮子都依赖于按钮标题。我的问题是,我无法找出一种方法来使用我已经在Buttons Sub中声明的String,然后在被调用的Sub中使用它。我知道你可以做到这一点,但是我的谷歌技能让我失望了:(



请问有人能告诉我如何做到这一点吗? / p>

这是一个非常小的代码片段,但是你得到的是jist:

  Public Sub CommandButton4_Click()
Dim Name As String

Name = CommandButton4.Caption

调用Sort1

End Sub `

另一个(为了尝试和错误也试过这个功能) p>

  Public Sub Sort1(Name As String)

Label11.Caption = Name
Sheets(Name )。选择

End Sub


解决方案

p>您所指的是 将参数 传递给另一个子例程或函数,假设您想要使用函数多次获取第一个字母一个字符串,其示例是:

 函数LeftOne(StrSample As String)As String 
LeftOne = Left(StrSample,1)
结束函数
pre>

上述函数可以在另一个函数或子例程中使用,只要您满足其要求: StrSample 。通过在函数的arguments字段中声明 StrSample As String ,您基本上要求对此的任何调用都需要将字符串传递给它。任何其他的都会抛出一个错误。



全线 LeftOne(StrSample As String)As String 可以读为:我的功能是LeftOne,给我一个字符串,我会在做一些事情之后给你一个字符串。请注意,名称 StrSample 是一个任意名称。



无论如何,调用以上是简单的: p>

  Sub MsgInABox()
Dim StrToFeed As String
StrToFeed =BK201
MsgBox LeftOne StrToFeed)'返回B.
End Sub

在你的例子中,如果你想通过名称 Sort1 ,您的尝试是绝对正确的。



让我们知道这是否有帮助。


I'm pretty new to this so apologies in advance

I'm half way through a userform in Excel and I'm trying to cut some fat off my code by using Call - I have 12 buttons that all do the same thing, the only difference is that each buttons sub is dependant on the buttons caption. My problem is that I can't figure out a way to use a String I've already declared in the Buttons Sub, then use it in the called Sub. I know you can do it, but my googling skills have failed me :(

Please could someone show me how to do this? Hope that all makes sense...

Here is a very small snippet of my code, but you get the jist:

    Public Sub CommandButton4_Click()
    Dim Name As String

    Name = CommandButton4.Caption

    Call Sort1

    End Sub`

And the other one (Also tried this as function for the sake of trial and error)

    Public Sub Sort1(Name As String)

    Label11.Caption = Name
    Sheets(Name).Select

    End Sub

解决方案

What you're referring to is passing an argument to another subroutine or function. Let's say you want to use a function a lot of times to get the first letter of a string. A sample of this is:

Function LeftOne(StrSample As String) As String
    LeftOne = Left(StrSample, 1)
End Function

The above function can be used inside another function or subroutine provided you meet its requirement: StrSample. By declaring StrSample As String in the arguments field of the function, you are basically requiring that any calls to this should require a string to be passed to it. Anything else would throw an error.

The full line LeftOne(StrSample As String) As String can be read as: "I am function LeftOne. Pass me a string and I'll return to you a string after doing something with it." Note that the name StrSample is an arbitrary name.

Anyway, calling the above is as simple as:

Sub MsgInABox()
    Dim StrToFeed As String
    StrToFeed = "BK201"
    MsgBox LeftOne(StrToFeed) 'Returns B.
End Sub

In your example, if you want to pass Name to Sort1, your attempt is absolutely correct.

Let us know if this helps.

这篇关于Excel VBA - 使用已调用的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆