VBA:整数数组到字符串数组以便于转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12655946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:54:13  来源:igfitidea点击:

VBA: Integer Array to String Array for easy conversion

vbaexcel-vbaexcel

提问by Reece Edwards

My first question was answered so quickly that I thought i'd ask another question! :)

我的第一个问题回答得如此之快,以至于我想我会问另一个问题!:)

It's probably really simple for you guys, but I can't seem to get it. I'm trying to convert a simple integer array to a string array because, with the problem i have, it easy to crossover a string rather than a int array (or so I think!)

这对你们来说可能真的很简单,但我似乎无法理解。我正在尝试将一个简单的整数数组转换为字符串数组,因为对于我遇到的问题,很容易交叉字符串而不是 int 数组(或者我认为是这样!)

But here is the code so far:

但这是到目前为止的代码:

    Dim Parent1s() As String
     For i = 1 To ChromoLength.Value
      y = y + Val(MyArray(rnd2, i))
      q = Val(MyArray(rnd2, i))
      Parents1(i) = q
  Next i

 For i = 0 To ChromoLength.Value
 Parent1s(i) = Val(Parents1(i))
 Next i
'Cells(8, 1) = Parent1s

I can't sem to get Parent1s to produce a string, keep getting a mismatch error Everything else has been dim'd correctly cause the rest of the code works when i hash out the parent1s statements.

我无法让 Parent1s 生成字符串,不断收到不匹配错误其他所有内容都已正确变暗,导致其余代码在我散列 parent1s 语句时正常工作。

Thanks guys!

谢谢你们!

回答by enderland

You can use the CStr()function to convert values to Strings.

您可以使用该CStr()函数将值转换为Strings.

Sub toString()

    Dim i As Integer
    Dim iStr As String
    i = 5

    iStr = CStr(i)

    MsgBox (iStr)


End Sub

However, I suspect your problems may be because you have not used Option Explicitand are running into problems with variables being named Parent1sas well as Parents1. If you intended for these to be so similar I would strongly encourage a better naming convention. Otherwise, the following seems like it should work:

但是,我怀疑您的问题可能是因为您没有使用Option Explicit并且遇到了变量命名Parent1s以及Parents1. 如果您希望它们如此相似,我强烈建议您采用更好的命名约定。否则,以下内容似乎应该有效:

For i = 0 To ChromoLength.Value
     Parent1s(i) = CStr(Parents1(i))
 Next i

回答by Daniel

If you are attempting to use Parent1s(i) = Val(Parents1(i))to convert Parents1to strings, this is not the right way to do it. Valattempts to convert a string, that may contain numbers and letters, to a numeric double. Actually, assuming Parents1 is a standard data type it should implicitly convert to a string with no problems. But if it doesn't, you can always use Cstr().

如果您尝试使用 Parent1s(i) = Val(Parents1(i))转换Parents1为字符串,这不是正确的方法。Val尝试将可能包含数字和字母的字符串转换为数字双精度值。实际上,假设Parents1 是标准数据类型,它应该可以毫无问题地隐式转换为字符串。但如果没有,您始终可以使用Cstr().

This may not be your issue, but you never dimension Parent1s().

这可能不是您的问题,但您从不考虑 Parent1s() 的尺寸。

To dimension Parent1s()add this line:

要标注尺寸,请Parent1s()添加以下行:

Redim Parent1s(0 to ChromoLength.Value)

Btw, if you've actually dimmed all of your variables and chosen your names deliberately, you should try to use variable names that will be clear to someone else (which incidentally might mean yourself in a year).

顺便说一句,如果您实际上已将所有变量变暗并故意选择了名称,则应尝试使用对其他人来说很清楚的变量名称(顺便说一句,这可能意味着一年后的您自己)。

In the future and also to help us help you better, you should either include the lines where you dimension your variables in your sample code or specifically tell us what data types you are using for each variable.

将来,为了帮助我们更好地帮助您,您应该在示例代码中包含您标注变量的行,或者具体告诉我们您对每个变量使用的数据类型。