简体   繁体   English

excel-根据第一个单元格中的数字将单元格数据拆分为其他单元格

[英]excel - Splitting cell data into other cells based on a number in the first cell

I have one cell in excel containing the following data 我在excel中有一个单元格,其中包含以下数据

108-0,109-1,110-0,111-2,112-0,113-192,114-87,115-100,116-80,117-60 108-0,109-1,110-0,111-2,112-0,113-192,114-87,115-100,116-80,117-60

I need to make a script or if statement, that can split the data from the cell into their own cells. 我需要制作一个脚本或if语句,可以将数据从单元格拆分为自己的单元格。

Like the data: 像数据一样:

108-0 would be put into cell A108 with the data 0, and 109-1 would be put into cell A109 with the data 1. 将108-0放入数据为0的单元格A108中,并将109-1放入数据为1的单元格A109中。

How would that be accomplished? 那将如何实现? Any hints what I should read about? 有什么提示我应该读些什么?

Thanks in advance 提前致谢

I forgot to mention that the excel sheet should do it automatically. 我忘了提到excel工作表应该自动执行。 I i am importing several hundred of those 100-1 values, so they should be put into their own cells automatically. 我要导入几百个100-1值,因此应将它们自动放入自己的单元格中。

Result - This worked for me: 结果-这对我有用:

Sub tst()
Dim X As Variant
X = Split(Range("A1").Value, ",")
Range("A1").Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
End Sub

You can use UDF: 您可以使用UDF:

Function splitThem(r As Range, delimeter As String)
    splitThem = WorksheetFunction.Transpose(Split(r, delimeter))
End Function

Just select A108:A117 , enter next formula in formula bar =splitThem(A1,",") and press CTRL + SHIFT + ENTER to evaluate it 只需选择A108:A117 ,在编辑栏中输入下一个公式=splitThem(A1,",") ,然后按CTRL + SHIFT + ENTER进行评估

UPD: UPD:

let your values be in A1:D1 range, than you can use: 让您的值在A1:D1范围内,然后可以使用:

Sub test()
    Dim c As Range
    Dim str As Variant
    On Error Resume Next
    For Each c In Range("A1:D1")
        c.Offset(1).Resize(UBound(str)) = WorksheetFunction.Transpose(Split(c, ","))
    Next
End Sub

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

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