Copied range searches for next empty cell in a row but skips merged cells
我已经看到很多人在谈论这个问题,但是从将合并的单元格复制到单个单元格的angular来看,更多的人在谈论这个问题。我有一个从工作簿获取范围的过程,打开第二个工作簿,然后尝试将其粘贴到指定行中的下一个空白单元格中;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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将数据放入合并的单元格中,或者是否需要做一个单独的过程来检查合并的单元格,取消合并,然后在数据包含后合并它们。复制跨。
如果那样的话,我能够以类似于检查下一个空单元格的方式来做到这一点,然后检查它是否已合并,然后执行取消合并并再次合并。
If I understand what you are saying then yes, but if I may alter your example I want the contents of say A1 put into D1:D2 that are merged. So its a value of one cell going into two merged together a€" JamesDev 4 hours ago
这是您要尝试的吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 |