簡體   English   中英

復制的范圍連續搜索下一個空單元格,但跳過合並的單元格

[英]Copied range searches for next empty cell in a row but skips merged cells

我看到很多人都在談論這個問題,但從將合並的單元格復制到單個單元格的角度來看,更多的人在談論這個問題。 我有一個從工作簿獲取范圍的過程,打開第二個工作簿,然后嘗試將其粘貼到指定行中的下一個空白單元格中;

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的內容放入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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM