首页 \ 问答 \ excel - 从一对多工作表创建多对一工作表(excel - Create a many-to-one sheet from a one-to-many sheet)

excel - 从一对多工作表创建多对一工作表(excel - Create a many-to-one sheet from a one-to-many sheet)

我在Excel中有一张表,类似于:

| Value1 | Data1 | Data1b | 1,3,4,8 |
| Value2 | Data2 | Data2b | 2       |
| Value3 | Data3 | Data3b | 6,7,8   |

我想把那张表格做成另一张表格,将最后一列分成不同的行并保持其他数据同步。 因此,当第一张纸更新时,第二张纸也会更新。 如果在第一张表中的最后一列添加了一个数字,则会在第二张表中添加一个新行。

第二张表应如下所示:

| Value1 | Data1 | Data1b | 1 |
| Value2 | Data2 | Data2b | 2 |
| Value1 | Data1 | Data1b | 3 |
| Value1 | Data1 | Data1b | 4 |
| Value3 | Data3 | Data3b | 6 |
| Value3 | Data3 | Data3b | 7 |
| Value1 | Data1 | Data1b | 8 |
| Value3 | Data3 | Data3b | 8 |

更新 :下面是我试图使用的代码。 首先,我有最好的方法吗? 然后清理然后重新填充正确的方法来更新第二张表格? 最后,如何在更新第一张纸时自动运行?

更新 :唯一仍然不起作用的是最后的那种,有没有人知道为什么?

Private FROM_SHEET As String
Private TO_SHEET As String
Private START_ROW As Long
Private NUM_COL As Long

Sub oneToMany()

    FROM_SHEET = "Sheet1"
    TO_SHEET = "Sheet2"
    START_ROW = 2
    NUM_COL = 4

    Dim fromSheet As Worksheet
    Dim toSheet As Worksheet
    Dim newRow As Long

    Set fromSheet = Sheets(FROM_SHEET)
    Set toSheet = Sheets(TO_SHEET)

    toSheet.UsedRange.ClearContents

    newRow = START_ROW
    For i = START_ROW To fromSheet.Cells(fromSheet.Rows.Count, 1).End(xlUp).Row
        Dim col As String
        Dim nums() As String

        col = fromSheet.Cells(i, NUM_COL)
        nums = Split(col, ",")

        For Each num In nums
            fromSheet.Rows(i).Copy toSheet.Rows(newRow)
            toSheet.Cells(newRow, NUM_COL) = Trim(num) 'Should copy then overwrite?
            newRow = newRow + 1
        Next num
    Next

    'Sort not working
    toSheet.Range(toSheet.Cells(START_ROW, START_COL), toSheet.Cells(lastRow, lastCol)).Sort _
        key1:=toSheet.Range(toSheet.Cells(START_ROW, NUM_COL), toSheet.Cells(lastRow, NUM_COL)), _
        order1:=xlAscending, Header:=xlNo

End Sub

I have a sheet in Excel, Something like:

| Value1 | Data1 | Data1b | 1,3,4,8 |
| Value2 | Data2 | Data2b | 2       |
| Value3 | Data3 | Data3b | 6,7,8   |

I'd like to take that sheet and make another that divides up that final column into separate rows and keeps the other data in sync. So when the first sheet is updated, the second is also updated. And if a number is added to that final column in the first sheet, a new row is added to the second sheet.

The second sheet should look like this:

| Value1 | Data1 | Data1b | 1 |
| Value2 | Data2 | Data2b | 2 |
| Value1 | Data1 | Data1b | 3 |
| Value1 | Data1 | Data1b | 4 |
| Value3 | Data3 | Data3b | 6 |
| Value3 | Data3 | Data3b | 7 |
| Value1 | Data1 | Data1b | 8 |
| Value3 | Data3 | Data3b | 8 |

UPDATE: Below is the code I'm attempting to use. First of all, is what I have the best way? And is clearing then repopulating the right way to go about updating the second sheet? Finally, how do I make this automatically run when one updates the first sheet?

UPDATE: The only thing still not working is the sort at the end, does anyone have any idea why?

Private FROM_SHEET As String
Private TO_SHEET As String
Private START_ROW As Long
Private NUM_COL As Long

Sub oneToMany()

    FROM_SHEET = "Sheet1"
    TO_SHEET = "Sheet2"
    START_ROW = 2
    NUM_COL = 4

    Dim fromSheet As Worksheet
    Dim toSheet As Worksheet
    Dim newRow As Long

    Set fromSheet = Sheets(FROM_SHEET)
    Set toSheet = Sheets(TO_SHEET)

    toSheet.UsedRange.ClearContents

    newRow = START_ROW
    For i = START_ROW To fromSheet.Cells(fromSheet.Rows.Count, 1).End(xlUp).Row
        Dim col As String
        Dim nums() As String

        col = fromSheet.Cells(i, NUM_COL)
        nums = Split(col, ",")

        For Each num In nums
            fromSheet.Rows(i).Copy toSheet.Rows(newRow)
            toSheet.Cells(newRow, NUM_COL) = Trim(num) 'Should copy then overwrite?
            newRow = newRow + 1
        Next num
    Next

    'Sort not working
    toSheet.Range(toSheet.Cells(START_ROW, START_COL), toSheet.Cells(lastRow, lastCol)).Sort _
        key1:=toSheet.Range(toSheet.Cells(START_ROW, NUM_COL), toSheet.Cells(lastRow, NUM_COL)), _
        order1:=xlAscending, Header:=xlNo

End Sub

原文:https://stackoverflow.com/questions/41747593
更新时间:2023-11-13 15:11

最满意答案

假设;# #s之间的值始终为数字,则可以在字符串的replace()调用中使用正则表达式/;#\d*;#/g

var input = 'Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce';

var output = input.replace(/;#\d*;#/g, "<br>");

document.write(output);

要显示正则表达式的作用,这里是一个可视化

例


Assuming the value between the ;#s is always numeric, you can use the regular expression /;#\d*;#/g within a replace() call on the string:

var input = 'Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce';

var output = input.replace(/;#\d*;#/g, "<br>");

document.write(output);

To show what the regular expression does, here is a visualisation:

Example

相关问答

更多
  • 所有你需要做的就是逃避$符号,因为它在正则表达式中有意义。 将其更改为 html = html.replace(/\$txt\$/ig, ''); 它应该没关系:) 编辑:$表示正则表达式中的行尾:) all you need to do is escape the $ sign since it has a meaning in a regular expression. Change it to html = html.replace(/\$txt\$/ig, ...
  • 由于我不确定你想在哪个地方计算元素,这里有一个count()的解释: 您可以使用count(node-set) (将节点集替换为所需的节点集来计数)来计算启动它的所有节点。 例如: 计算XML中的所有元素: 从您现在的路径计算所有元素: 计算元素下面的路径中的所有
  • 如果要替换“@gmail”。 用“@yahoo。”,然后简单地做: UPDATE users SET email = REPLACE(email, '@gmail.', '@yahoo.') WHERE email LIKE '%@gmail.%'; 如果您希望域始终是gmail,那么您可以尝试重建电子邮件: UPDATE users SET email = CONCAT(substring_index(email, '@', 1), '@', ...
  • 正则表达式是“贪婪的”,意味着它们会尝试匹配可能的最长子字符串。 因为.*意味着任何字符,它也将包括你的分隔符<< 。 因此, .*到达字符串的结尾,然后在之后找不到<< ,所以匹配将失败。 您必须在表达式中将其排除: text.replace(/>>[^<]*<>Some other text<<"); Regexes are "greedy", meaning they'll try to match the longest substring possible. Since .* mea ...
  • 您的第一个问题是ant文件必须是格式良好的XML文件,并且格式良好的XML不能在属性值中包含<字符。 使用< 代替。 第二个问题是由双引号字符( " )分隔的属性值不能包含未转义的嵌入式双引号字符。请改用。
    | 是一个正则表达式元字符,你也使用它作为分隔符(这是2个问题)。 你应该逃避| 签名,并使用另一个分隔符(您也不需要i和s标志): $rstr = '~\|~'; 你的正则表达式是~~|is ,并有以下错误信息: Warning: preg_replace(): Unknown modifier '|' in /home/o3oYmU/prog.php on line 3 | is a regex meta-character, and you also use it as the delimiter ...
  • 根据Bobince的建议,以下内容对我有用: analyse_file() - > http://www.php.net/manual/en/function.fgetcsv.php#101238 function file_get_contents_utf8($fn) { $content = file_get_contents($fn); return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, ...
  • 假设;# #s之间的值始终为数字,则可以在字符串的replace()调用中使用正则表达式/;#\d*;#/g : var input = 'Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce'; var outpu ...
  • 第二种方法使用1 。 链接。 2 。 jquery在执行函数时执行大量检查。 3 。 访问DOM。 很明显, 第一种方法更快,因为它是普通的javascript。虽然它不会在你的场景中产生如此巨大的差异,但如果你没有明智地使用它,第二种方法可能会非常慢,因为当jQuery构建元素时string,它通过迭代添加所有顶级项目。 所以你可能需要将html包装在一个项目中。 使用类名访问DOM,使用数组等时,Jquery更快。 希望,它澄清事情! PS更快,不要与更好或推荐混淆。 The second method ...
  • 是的,并且尽可能简单: '®'.replace('®','anything'); yes, and is as simple as can be: '®'.replace('®','anything');

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)