Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Excel 2010 VBA逐步遍历字符串,并按顺序在每个单元格中放置一个字符_String_Excel_Vba - Fatal编程技术网

String Excel 2010 VBA逐步遍历字符串,并按顺序在每个单元格中放置一个字符

String Excel 2010 VBA逐步遍历字符串,并按顺序在每个单元格中放置一个字符,string,excel,vba,String,Excel,Vba,很多年前,我就习惯了用C语言进行字符串切片,但我正在尝试使用VBA来完成这项特定任务 现在我已经创建了一个字符串“thisastring”,并创建了一个新工作簿 我现在需要的是使用字符串切片将't'放在字符串的末尾,比如说,A1,'h'放在A2,'I'放在A3等等 Dim str as String str = Sheet1.Cells(1,1).Value for i = 1 to Len(str) 'output string 1 character at a time in co

很多年前,我就习惯了用C语言进行字符串切片,但我正在尝试使用VBA来完成这项特定任务

现在我已经创建了一个字符串“thisastring”,并创建了一个新工作簿

我现在需要的是使用字符串切片将't'放在字符串的末尾,比如说,A1,'h'放在A2,'I'放在A3等等

Dim str as String
str = Sheet1.Cells(1,1).Value

for i = 1 to Len(str)
    'output string 1 character at a time in column C
    sheet1.cells(i,3).value = Mid(str,i,1)
next i
之后,我的下一个字符串将进入,如B1等,直到所有字符串被切片

我已经搜索过了,但似乎大多数人都想用另一种方式(连接一个范围)

有什么想法吗?

使用mid功能

=MID($A$1,1,1)
第二个参数是起始位置,因此可以将其替换为row或col函数,以便动态拖动公式

如果您想纯粹在VBA中实现它,我相信mid函数也存在于其中,所以只需在字符串中循环即可

Dim str as String
str = Sheet1.Cells(1,1).Value

for i = 1 to Len(str)
    'output string 1 character at a time in column C
    sheet1.cells(i,3).value = Mid(str,i,1)
next i
*编辑*

如果要对一个数组中的多个字符串执行此操作,可以使用以下方法:

Dim str(1 to 2) as String
str(1) = "This is a test string"
str(2) = "Some more test text"

for j = Lbound(str) to Ubound(str)
    for i = 1 to Len(str(j))
        'output strings 1 character at a time in columns A and B
        sheet1.cells(i,j).value = Mid(str(j),i,1)
    next i
next j

山姆,谢谢。我试图做的是将一个值从内存中的字符串数组放入一个单元格,每个单元格一个字符。工作表中不存在该字符串。@Cimbian,您在问题中表示希望将字符串“放在,比如A1中,A2中的“h”,A3中的“i”,等等,放在字符串的末尾。Sam的答案把它放在C列,但你可以很容易地修改它,要么放在另一列,要么放在内存中的数组中。好了,我现在开始想它了。谢谢大家!:)我增加了一个额外的例子。如果答案对你有用,你能接受吗please@Cimbian,如果这回答了您的问题,请单击答案顶部的复选标记+回答得好,山姆。