Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
Excel 将多个单元格拆分为列_Excel_Vba - Fatal编程技术网

Excel 将多个单元格拆分为列

Excel 将多个单元格拆分为列,excel,vba,Excel,Vba,我有多个像字符串一样的单元格 因为。前cel A1 m2_10cm[0.10],m2_20cm[0.20],m2_5cm[0.05],m3[1.9] 和A2单元 m3_22[2.2],m3_19[1.9] 我能把它分成一列吗 Column B Cell 1 m2_10cm[0.10] Cell 2 m2_20cm[0.20] Cell 3 m2_5cm[0.05] Cell 4 m3[1.9] Cell 5 m3_22[2.2] Ce

我有多个像字符串一样的单元格

因为。前cel A1

m2_10cm[0.10],m2_20cm[0.20],m2_5cm[0.05],m3[1.9]
和A2单元

m3_22[2.2],m3_19[1.9]
我能把它分成一列吗

  Column  B
    Cell 1   m2_10cm[0.10]
    Cell 2 m2_20cm[0.20]
    Cell 3 m2_5cm[0.05]
    Cell 4 m3[1.9]
    Cell 5 m3_22[2.2]
    Cell 6 m3_19[1.9]

我很乐意提供任何帮助

这里有一个解决方案,您需要隐藏其他每一列: 假设A1中有值,然后输入以下公式:

B1: =IF(ISERR(FIND(",";A1;1));A1;LEFT(A1;FIND(",";A1;1)-1))
C1: =IF(ISERR(FIND(",";A1;1));"";RIGHT(A1;LEN(A1)-FIND(",";A1;1)))
B1将包含列表中的第一个值,C1将包含减去第一个值的列表。现在您可以将这些公式复制到D1和E1,它们现在看起来像

D1: =IF(ISERR(FIND(",";C1;1));C1;LEFT(C1;FIND(",";C1;1)-1))
E1: =IF(ISERR(FIND(",";C1;1));"";RIGHT(C1;LEN(C1)-FIND(",";C1;1)))
现在继续复制这个公式,直到你需要的时候为止


完成此操作后,您可以隐藏包含缩短列表的所有列,从C开始,然后是E,等等。

使用Excel中的VBA代码:

注意:谷歌如何在工作表上创建一个命令按钮,并将其粘贴为代码

Option Explicit

' note: vbNullString is the same as "" (empty string)

Const START_ROW = 1
Const SRC_COL = 1    ' column A
Const DST_COL = 2    ' column B

' this gets triggered when the button is pressed
Private Sub CommandButton1_Click()
    ' call the routine
    Call Go
End Sub

Function Go()
    ' assume the button is on the sheet to be processed
    Dim ws As Excel.Worksheet
    Set ws = Excel.ActiveSheet

    Dim srcRow As Integer ' current row being processed
    Dim dstRow As Integer ' current row to put result in
    srcRow = START_ROW: dstRow = START_ROW

    ' keep going while column 'A' is not blank
    While ws.Cells(srcRow, SRC_COL) <> vbNullString
        Call Split(ws, ws.Cells(srcRow, SRC_COL), dstRow)
        srcRow = srcRow + 1
    Wend
End Function

Sub Split(ws As Excel.Worksheet, srcStr As String, ByRef dstRow As Integer)
    If (srcStr = vbNullString) Then
        'remove comment if you want blanks at the end
        ' ex. Apple,Banana, 
        '     will create 3 entries, notice the comma at the end 
        'ws.Cells(dstRow, DST_COL) = vbNullString
        'dstRow = dstRow + 1
        Exit Sub
    endif

    ' find ","
    Dim pos As Integer
    pos = InStr(1, srcStr, ",")
    If (pos = 0) Then
        ' no "," - put the whole string
        ws.Cells(dstRow, DST_COL) = Trim(srcStr)
        dstRow = dstRow + 1
    Else
        ' has "," - put the left part of the string
        ' ex: apple,banana,carrot
        '     put "apple"
        '     continue processing "banana,carrot"
        ws.Cells(dstRow, DST_COL) = Trim(Mid(srcStr, 1, pos - 1))
        ' move to next row and process the right of the string
        dstRow = dstRow + 1
        Call Split(ws, Mid(srcStr, pos + 1), dstRow)
    End If
End Sub
选项显式
'注意:vbNullString与”“(空字符串)相同
常量开始行=1
常数SRC_COL=1'列A
常数DST_COL=2'列B
'按下按钮时会触发此操作
私有子命令按钮1_单击()
"叫例行公事吧!
呼叫开始
端接头
函数Go()
'假设按钮位于要处理的工作表上
将ws设置为Excel.Worksheet
设置ws=Excel.ActiveSheet
Dim srcRow作为整数“正在处理的当前行”
将dstRow设置为整数“要放入结果的当前行”
srcRow=START_行:dstRow=START_行
'继续,而列'A'不是空的
而ws.Cells(srcRow,SRC_COL)则为vbNullString
调用拆分(ws,ws.Cells(srcRow,SRC_COL),dstRow)
srcRow=srcRow+1
温德
端函数
子拆分(ws作为Excel.Worksheet,srcStr作为字符串,ByRef dstRow作为整数)
如果(srcStr=vbNullString),则
'如果要在结尾留空,请删除注释
例如苹果、香蕉、,
'将创建3个条目,请注意末尾的逗号
'ws.Cells(dstRow,DST_COL)=vbNullString
'dstRow=dstRow+1
出口接头
恩迪夫
“查找”
作为整数的Dim pos
位置=仪表(1,srcStr,“,”)
如果(位置=0),则
“不”,“-把整个字符串
ws.Cells(dstRow,DST_COL)=Trim(srcStr)
dstRow=dstRow+1
其他的
“has”,“-放置字符串的左侧部分
例如:苹果、香蕉、胡萝卜
“放“苹果”
“继续加工香蕉、胡萝卜”
ws.Cells(dstRow,DST_COL)=修剪(中间(srcStr,1,pos-1))
'移动到下一行并处理字符串的右侧
dstRow=dstRow+1
呼叫拆分(ws、Mid(srcStr、pos+1)、dstRow)
如果结束
端接头

您是否尝试过将文本转换为列,然后复制->粘贴特殊->转置?您甚至可以尝试录制自己执行的文本转换为列,以查看结果。感谢您的快速响应,但在我的情况下,这还不够。我需要用公式做这件事,因为我把它整理到数据验证列表中。你想要一个公式吗?有多个(可能隐藏)中间结果的单元格可以吗?你也可以用同样的方法做这件事,把公式放在A2和A3中,然后复制到A4和A5,等等。。。然后隐藏第3、5、7行等。