简体   繁体   English

VBA:我如何连接这个

[英]VBA: How can I concatenate this

I'been doing a macro and when I have to print a bunch of info (strings and variables) in a cell it don't compile.我一直在做一个宏,当我必须在一个单元格中打印一堆信息(字符串和变量)时,它不会编译。 How is the syntax then?那么语法如何?

Sub TEST()
  
  Dim str1, str2 As String
  st1 = "Hi"
  st2 = "are"

  MsgBox (& str1& & "como" & str2 & "you")

End Sub

Use Option Explicit at the top of your code.在代码顶部使用Option Explicit Then you will be alerted if variables are undeclared.然后,如果变量未声明,您将收到警报。

You declare the variable st1 as a variant, the variable st2 as a string.您将变量st1声明为变体,将变量st2声明为字符串。 (if you don't write the "as string" after every variable, it will not be a string.) (如果你不在每个变量后面写“as string”,它就不是字符串。)

In the MsgBox, you use two different variables, str1 and str2 .在 MsgBox 中,您使用两个不同的变量str1str2

Such details matter.这样的细节很重要。

And, of course, you have a few too many & signs in the MsgBox and not enough space characters.而且,当然,您在 MsgBox 中有太多的&符号并且没有足够的空格字符。 Try尝试

Option Explicit
Sub TEST()
  
  Dim st1 As String, st2 As String
  st1 = "Hi"
  st2 = "are"
  MsgBox (st1 & " como " & st2 & " you")
  

End Sub

First, suggest using Option Explicit as you use different variables to what you Dim (eg you declare "str1" but use "st1").首先,建议使用 Option Explicit,因为您使用与 Dim 不同的变量(例如,您声明“str1”但使用“st1”)。 Next, str1 is declared as a variant, probably not what you intended.接下来, str1 被声明为一个变体,可能不是您想要的。 Try:尝试:

Option Explicit

Sub TEST()
  
  Dim str1 As String
  Dim str2 As String
  
  str1 = "Hi"
  str2 = "are"

  MsgBox (str1 & "como" & str2 & "you")

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM