[英]Excel VBA merging cells in a function
我編寫了一個粗略的函數來根據范圍選擇和連接單元格。
Function GetSkills(CellRef As String, CellRefEnd As String, Delimiter As String)
Dim CellStart As Range
Dim CellEnd As Range
Dim LoopVar As Long
Dim StartRow As Long
Dim EndRow As Long
Dim Concat As String
Dim Col As Long
Set CellStart = Worksheets(1).Cells.Range("B" & CellRef)
Set CellEnd = Worksheets(1).Cells.Range("B" & CellRefEnd)
Col = CellStart.Column
StartRow = CellStart.Row
EndRow = CellEnd.Row
With Range(CellStart, CellEnd)
.Merge
.WrapText = True
End With
Concat = ""
For LoopVar = StartRow To EndRow
Concat = Concat & Cells(LoopVar, Col).Value
If LoopVar <> EndRow Then Concat = Concat & Delimiter & " "
Next LoopVar
GetSkills = Concat
End Function
在其中我試圖合並單元格,當我運行該函數時,我得到一個提示:
選擇包含多個數據值。 合並到一個單元格將只保留左上角的數據
我單擊“確定”,Excel 崩潰、重新啟動並再次提示對話框。 是否有另一種使用 VBA 合並單元格塊的方法?
通常合並單元格不是一個好主意。 這是一種裝飾性的格式化方法,可能會對 VBA 代碼造成嚴重破壞。
除了免責聲明,一些建議
Application.DisplayAlerts
抑制合並單元格消息代碼
Sub Test()
Call GetSkills(2, 4, ",")
End Sub
Sub GetSkills(CellRef As String, CellRefEnd As String, Delimiter As String)
Dim CellStart As Range
Dim CellEnd As Range
Dim Concat As String
Application.DisplayAlerts = False
Set CellStart = Worksheets(1).Cells.Range("B" & CellRef)
Set CellEnd = Worksheets(1).Cells.Range("B" & CellRefEnd)
Concat = Join(Application.Transpose(Range(CellStart, CellEnd)), Delimiter)
With Range(CellStart, CellEnd)
.Merge
.WrapText = True
.Value = Concat
End With
Application.DisplayAlerts = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.