【问题标题】:Copied range searches for next empty cell in a row but skips merged cells复制的范围连续搜索下一个空单元格,但跳过合并的单元格
【发布时间】:2012-10-14 12:05:02
【问题描述】:

我看到很多人谈论这个问题,但更多是从将合并的单元格复制到单个单元格的角度来看。我有一个从工作簿获取范围的过程,打开第二个工作簿并尝试将其粘贴到指定行的下一个空白单元格中;

Public Sub BankBalances()
Dim fname As String
Dim fname2 As String
Dim mt As String, yr As String
'get the selected month
mt = MonthBox.Value
'get the selected year
yr = YearBox.Value
'set the file path to the selected year, month and the saving format
fname = yr & mt & "DB" & ".xlsx"
'set the file path to the selected year, month to open the trial balance sheet
fname2 = yr & mt & "TB" & ".xlsx"
'get the ranges
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Workbooks(fname2).Worksheets("excel").Range("I100")
'check for the next empty cell in row 55
Set rng2 = Workbooks(fname).Worksheets("UK monthly dashboard").Cells(55, Columns.Count).End(xlToLeft).Offset(0, 1)
'set the new range to hold the data from the original range
rng2.Value = rng1.Value
'set the result to format according to the other information in the workbook
rng2.Value = Round(rng1.Value / 1000, 0)

End Sub

上面的代码会将数据粘贴到未合并行的下一个空白单元格中。我需要它才能粘贴到合并的单元格中。

我想知道是否有办法使用 VBA 将数据放入合并的单元格,或者我是否需要执行单独的过程来检查合并的单元格,取消合并,然后在数据完成后合并它们复制过来。

如果是这样,我是否能够以类似于检查下一个空单元格的方式执行此操作,然后检查它是否已合并,然后执行取消合并并再次合并。

【问题讨论】:

  • 它很相似,但因为这是一个问题,只是让范围复制它不是重复的。其次,这也是我的问题,第三,它只是保持开放状态,看看是否有人提出任何建议,但我很高兴那个被关闭
  • 因此,如果您复制单元格 A1:A5 并且您已合并 D1:D5,那么您是否希望 A1:A5 的内容“在”合并单元格“内部”?
  • 如果我明白你在说什么,那么是的,但如果我可以改变你的例子,我希望将 A1 的内容放入合并的 D1:D2 中。所以它是一个单元格进入两个合并在一起的值
  • 您需要合并单元格吗?我从不使用合并单元格,而是使用 Format Cells/Alignment/Horizo​​ntal/Centre across selection 中的“跨选区居中”。

标签: vba excel merge excel-2007 range


【解决方案1】:

如果我明白你在说什么,那么是的,但如果我可以改变你的例子,我希望将 A1 的内容放入 D1:D2 合并。所以它是一个单元格进入两个合并在一起的值 – JamesDev 4 小时前

这是你正在尝试的吗?

Sub Sample()
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        Set Rng1 = .Range("A1")
        Set Rng2 = .Range("D1:D2")

        '~~> Check if cells are merged
        If Rng2.MergeCells Then
            '~~> If merged then write to first cell
            Rng2.Cells(1, 1).Value = Rng1.Value
        Else
            Rng2.Value = Rng1.Value
        End If
    End With
End Sub

【讨论】:

  • 这正是我想要的......但它并没有足够奇怪地解决问题,它仍然跳过单元格并放置在所需目的地的右侧。单元格中都有破折号,我之前并没有考虑太多,但检查这意味着它们的值是 0。我试过删除它,看看它是否有效,但仍然没有
  • 好的,所以取出所有格式并删除值然后允许它工作,所以我很乐意将其标记为正确答案。查看范围以查看是否有 0、删除它并删除所有格式、复制范围并重新应用格式是否简单?
猜你喜欢
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2018-01-20
  • 2014-12-11
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
相关资源
最近更新 更多