如何保留每个单元格中合并单元格的值?

发布于 2024-11-16 17:40:13 字数 79 浏览 3 评论 0原文

我创建了一个包含合并单元格的工作表,但合并单元格的值仅存储在第一个单元格中。无论如何,为了在每个单元格中保持相同的值,我使用的公式需要它。谢谢!

I created a sheet with merged cells, but the value of the merged cells is only stored in the first cell. Is there anyway, to keep the same value in each of the cells, I need that for a formula I use. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

万水千山粽是情ミ 2024-11-23 17:40:13

在 Excel 2003 中,此宏执行以下操作:

Public Sub UnmergeAndFill()
    With Selection
        If .MergeCells Then
            .MergeCells = False
            Selection.Cells(1, 1).Copy
            ActiveSheet.Paste 'Or PasteSpecial xlPasteFormulasAndNumberFormats
        End If
    End With
End Sub

通过按 Alt-F11Ctrl-R、菜单 Insert/Module 创建宏

  1. ,粘贴代码;
  2. 或者Alt-F8,输入新名称(例如UnmergeAndFill),单击Make创建?不知道英文按钮文本

按 Alt-F8 调用宏,选择它,运行。或者将其映射到一个键

In Excel 2003 this macro does the job:

Public Sub UnmergeAndFill()
    With Selection
        If .MergeCells Then
            .MergeCells = False
            Selection.Cells(1, 1).Copy
            ActiveSheet.Paste 'Or PasteSpecial xlPasteFormulasAndNumberFormats
        End If
    End With
End Sub

Create the macro by

  1. pressing Alt-F11, Ctrl-R, menu Insert/Module, paste the code;
  2. alternatively: Alt-F8, type a new name (UnmergeAndFill, e.g.), click Make or Create (? don't know the English button text)

Invoke the Macro by pressing Alt-F8, select it, Run. Alternatively map it to a key

谈下烟灰 2024-11-23 17:40:13

您可以使用自定义 VBA 函数来直接给出合并单元格的值,无论您选择哪一个。在这种情况下,没有必要重复这些值。

  1. 切换到 VBA 视图 (Alt-F11)
  2. 通过 Insert > 创建新模块模块
  3. 在您的项目中,浏览到新模块(您可能希望通过资源管理器下方的 (name) 属性为其指定一个新名称)
  4. 在模块中复制以下代码(在 ThisWorkbook 中粘贴代码不起作用)

代码:

Option Explicit

Function GetMergedValue(location As Range)
    If location.MergeCells = True Then
        GetMergedValue = location.MergeArea(1, 1)
    Else
        GetMergedValue = location
    End If
End Function
  1. 您现在可以在 Excel 代码中使用公式

=GetMergedValue(A1)

其中 A1 是合并单元格的一部分。

You could use a custom VBA function that gives directly the value of the merged cell, no matter which one you select. In that case it is not necessary to duplicate the values.

  1. Switch to VBA view (Alt-F11)
  2. Create a new module via Insert > Module
  3. In your project, browse to the new module (you might want to give it a new name via the (name) property just under the explorer)
  4. Copy the following code in the module (pasting the code in ThisWorkbook will not work)

Code:

Option Explicit

Function GetMergedValue(location As Range)
    If location.MergeCells = True Then
        GetMergedValue = location.MergeArea(1, 1)
    Else
        GetMergedValue = location
    End If
End Function
  1. You can now use the formula in excel

Code:

=GetMergedValue(A1)

Where A1 is a part of a merged cell.

冷情 2024-11-23 17:40:13

我知道这是一个相当老的问题,但这是我在寻找答案时遇到的第一个地方,而接受的答案根本没有帮助。然而,我确实在 MrExcel 上发现了一个很好的答案,我认为值得将其放在这个线程上,以便其他人在谷歌上搜索答案:
http://www.mrexcel.com/forum/general-excel-discussion-other-questions/487941-data-multiple-cells-within-merged-cell-possible.html

保存查找链接,答案非常简单;如果您使用 Excel 格式刷而不是合并单元格来合并单元格,它会保留合并单元格“下方”的数据/公式。您只需要在其他地方以正确的格式创建临时合并的单元格块,以用作格式刷的模板。您可以稍后删除它们。不过,需要注意的一件事是,像这样的“隐藏”数据对于粗心的人来说可能是一个陷阱,因为编辑可见单元格不会更改不可见的单元格。

I know this is a rather old question, but this is the first place I landed when looking for an answer, and the accepted answer did not help at all. I DID discover an excellent answer, however, on MrExcel which I thought was worth putting on this thread for the benefit of anyone else googling for an answer:
http://www.mrexcel.com/forum/general-excel-discussion-other-questions/487941-data-multiple-cells-within-merged-cell-possible.html

To save looking up the link, the answer is remarkably simple; if you merge cells using Excel Format Painter, rather than Merge Cells, it preserves the data/formulae 'underlying' the merged cells. You just need to create a temporary merged block of cells in the right format somewhere else, to use as a template for the Format Painter. You can delete them afterwards. One thing to watch out for, though, is having 'hidden' data like this can be a trap for the unwary, since editing the visible cell does not change the invisible ones.

白日梦 2024-11-23 17:40:13

您可以创建一个新列(或行)并在第一个单元格中应用此公式并将其向下拖动:

我想在 A 列中您已合并单元格(例如 A1:A3 和 A5:A8 已合并)。

  1. 在 A 列之前插入一列

  2. 在 A1 中键​​入:

    <前><代码>=B1

  3. 将以下公式复制到 A2 中:

    <预置><代码>=IF(B2="",A1,B2)

  4. 向下拖动在 A2 中输入的公式

在公式中使用新创建的列,使用后即可可以隐藏它。

you can make a new column (or row) and apply this formula in first cell and drag it down:

I suppose in column A you have merged cells (for example A1:A3 & A5:A8 are merged).

  1. Insert a column before column A

  2. In A1 type:

    =B1
    
  3. Copy the formula below in A2 :

    =IF(B2="",A1,B2)
    
  4. Drag down the formula u typed in A2

In your formulas use the newly created column and after use you can hide it.

要走就滚别墨迹 2024-11-23 17:40:13

你不能将实际值存储在其他地方吗?而不是合并单元格?是的,使用公式显示合并单元格上的值。

can you not store the actual values somewhere else? instead of the merged cell? and yes , use a formula to display the value on the merged cell.

半暖夏伤 2024-11-23 17:40:13

假设“A”列已合并单元格 - 将其放入 B1 中并复制它以填充该列的其余部分:

=IF(ISBLANK(A1);OFFSET(B1;-1;0);A1)

它检查左侧的单元格是否有值,如果有,则返回其值,如果没有,则采用该值从上单元。

请注意,它不适用于空单元格。首先用一些独特的东西填充“A”列中的空单元格,例如“(空)”,并在填充“B”列后将“A”和“B”中的空单元格替换回空。

Assume column "A" has merged cells - put this in B1 and copy it to fill the rest of the column:

=IF(ISBLANK(A1);OFFSET(B1;-1;0);A1)

It checks if the cell to the left has a value if it has it returns its value, if not, it takes the value from the upper cell.

Note that it doesn't work for the empty cells. First fill empty cells in column "A" with something unique, like "(empty)" and replace it back with emptiness both in "A" and "B" after filling column "B".

冰火雁神 2024-11-23 17:40:13

我改进了 sehe 的宏,可以根据您的选择执行尽可能多的合并单元格。

代码:

Public Sub UnmergeAndFillMultiple()
    Dim w As Range, m As Range, n As Range
    For Each w In Selection.Cells
        If w.MergeCells Then
            Set n = w.MergeArea.Cells(1, 1)
            Set m = w.MergeArea
            w.MergeCells = False
            n.Copy
            m.PasteSpecial
        End If
    Next
End Sub

I improved on sehe's macro to do as many merged cells as you select.

Code:

Public Sub UnmergeAndFillMultiple()
    Dim w As Range, m As Range, n As Range
    For Each w In Selection.Cells
        If w.MergeCells Then
            Set n = w.MergeArea.Cells(1, 1)
            Set m = w.MergeArea
            w.MergeCells = False
            n.Copy
            m.PasteSpecial
        End If
    Next
End Sub
许仙没带伞 2024-11-23 17:40:13

假设 A13 到 A52 已合并。
a = Cells(13,1) 将返回一个值,但是
a = Cells(14,1) 不会“空”
修复:
a = Cells(X,1).MergeArea(1, 1) '如果 x=13 到 52 将在 A13 中返回值。
不知道我是如何让它发挥作用的,但它确实有效。

Say A13 through A52 are merged.
a = Cells(13,1) will return a value but
a = Cells(14,1) will not "Empty"
Fix:
a = Cells(X,1).MergeArea(1, 1) 'if x=13 through 52 will return value in A13.
don't know how i got this to work but it works.

羁〃客ぐ 2024-11-23 17:40:13
 Dim rowcnt As Long, i As Long
    rowcnt = Cells(Rows.Count, "A").End(xlUp).Row
    For i = rowcnt To 3 Step -1
        With Cells(i, 1)
            If .Value = Cells(i - 1, 1).Value Then
                .Font.ColorIndex = 9
            End If
        End With
    Next i
 Dim rowcnt As Long, i As Long
    rowcnt = Cells(Rows.Count, "A").End(xlUp).Row
    For i = rowcnt To 3 Step -1
        With Cells(i, 1)
            If .Value = Cells(i - 1, 1).Value Then
                .Font.ColorIndex = 9
            End If
        End With
    Next i
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文