मैं एक विशिष्ट कॉलम में जानकारी के आधार पर एकाधिक कॉलम के लिए कॉलम के आधार पर कोशिकाओं के विलय को स्वचालित करना चाहता हूं।
नीचे दी गई तस्वीर के आधार पर कॉलम सी में मानस्तंभों ए के लिए एक साथ विलय करने की आवश्यकता वाली पंक्तियों की संख्या निर्धारित करेगा। कॉलम सी में मूल्य में प्रत्येक परिवर्तन के साथ - विलय फिर से शुरू होगा।
Private Sub MergeCells_C()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rngMerge As Range, cell As Range
Set rngMerge = Range("C1:C1000") "Set the range limits here
MergeAgain:
For Each cell In rngMerge
If cell.Value = cell.Offset(1, 0).Value And IsEmpty(cell) = False Then
Range(cell, cell.Offset(1, 0)).Merge
GoTo MergeAgain
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
उत्तर:
जवाब के लिए 0 № 1यह मेरे लिए काम किया:
Sub MergeCellsByC()
Dim c As Range, sht As Worksheet, currV
Dim n As Long, rw As Range, r As Range
Set sht = ActiveSheet
Set c = sht.Range("C4") "adjust to suit....
currV = Chr(0) "start with a dummy value
Do
If c.Value <> currV Then
If n > 1 Then
Set rw = c.EntireRow.Range("A1:K1") "A1:K1 relative to the row we"re on...
Application.DisplayAlerts = False
"loop across the row and merge the cells above
For Each r In rw.Cells
r.Offset(-n).Resize(n).Merge
Next r
Application.DisplayAlerts = True
End If
currV = c.Value
n = 1
Else
n = n + 1 "increment count for this value
End If
If Len(c.Value) = 0 Then Exit Do "exit on first empty cell
Set c = c.Offset(1, 0) "next row down
Loop
End Sub