Em tìm được 2 đoạn mã này về cách merge dữ liệu giữa 2 cột trong excel mà vẫn giữ nguyên định dạng, nhưng không biết cách chạy đoạn mã này, anh chị nào thử chạy được thì chỉ em với, thanks rất nhiều
ĐOẠN MÃ 1:
Sub Merge_Cells()
Dim iOS As Integer
Dim rngFrom1 As Range
Dim rngFrom2 As Range
Dim rngTo As Range
Dim lenFrom1 As Integer
Dim lenFrom2 As Integer
Set rngFrom1 = Cells(1, 1)
Set rngFrom2 = Cells(1, 2)
Set rngTo = Cells(1, 3)
lenFrom1 = rngFrom1.Characters.Count
lenFrom2 = rngFrom2.Characters.Count
rngTo.Value = rngFrom1.Text & rngFrom2.Text
For iOS = 1 To lenFrom1
With rngTo.Characters(iOS, 1).Font
.Name = rngFrom1.Characters(iOS, 1).Font.Name
.Bold = rngFrom1.Characters(iOS, 1).Font.Bold
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom1.Characters(iOS, 1).Font.ColorIndex
End With
Next iOS
For iOS = 1 To lenFrom2
With rngTo.Characters(lenFrom1 + iOS, 1).Font
.Name = rngFrom2.Characters(iOS, 1).Font.Name
.Bold = rngFrom2.Characters(iOS, 1).Font.Bold
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom2.Characters(iOS, 1).Font.ColorIndex
End With
Next iOS
End Sub
ĐOẠN MÃ 2:
Sub MergeCells(rngFrom1 As Range, rngFrom2 As Range, rngTo As Range, Separateur As String)
Dim iOS As Integer
Dim lenFrom1 As Long
Dim lenFrom2 As Long
lenFrom1 = rngFrom1.Characters.Count
lenFrom2 = rngFrom2.Characters.Count
rngTo.WrapText = True
rngTo.Value = rngFrom1.Text & Separateur & rngFrom2.Text
For iOS = 1 To lenFrom1
With rngTo.Characters(iOS, 1).Font
.Name = rngFrom1.Characters(iOS, 1).Font.Name
.Bold = rngFrom1.Characters(iOS, 1).Font.Bold
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom1.Characters(iOS, 1).Font.ColorIndex
.Color = rngFrom1.Characters(iOS, 1).Font.Color
.FontStyle = rngFrom1.Characters(iOS, 1).Font.FontStyle
.Italic = rngFrom1.Characters(iOS, 1).Font.Italic
.OutlineFont = rngFrom1.Characters(iOS, 1).Font.OutlineFont
.Shadow = rngFrom1.Characters(iOS, 1).Font.Shadow
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.Strikethrough = rngFrom1.Characters(iOS, 1).Font.Strikethrough
.Subscript = rngFrom1.Characters(iOS, 1).Font.Subscript
.Superscript = rngFrom1.Characters(iOS, 1).Font.Superscript
.Underline = rngFrom1.Characters(iOS, 1).Font.Underline
' .Background = rngFrom1.Characters(iOS, 1).Font.Background
' .Parent = rngFrom1.Characters(iOS, 1).Font.Parent
' .Creator = rngFrom1.Characters(iOS, 1).Font.Creator
' .Application = rngFrom1.Characters(iOS, 1).Font.Application
End With
Next iOS
For iOS = 1 To lenFrom2
With rngTo.Characters(lenFrom1 + Len(Separateur) + iOS, 1).Font
.Name = rngFrom2.Characters(iOS, 1).Font.Name
.Bold = rngFrom2.Characters(iOS, 1).Font.Bold
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom2.Characters(iOS, 1).Font.ColorIndex
.Color = rngFrom2.Characters(iOS, 1).Font.Color
.FontStyle = rngFrom2.Characters(iOS, 1).Font.FontStyle
.Italic = rngFrom2.Characters(iOS, 1).Font.Italic
.OutlineFont = rngFrom2.Characters(iOS, 1).Font.OutlineFont
.Shadow = rngFrom2.Characters(iOS, 1).Font.Shadow
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.Strikethrough = rngFrom2.Characters(iOS, 1).Font.Strikethrough
.Subscript = rngFrom2.Characters(iOS, 1).Font.Subscript
.Superscript = rngFrom2.Characters(iOS, 1).Font.Superscript
.Underline = rngFrom2.Characters(iOS, 1).Font.Underline
' .Background = rngFrom2.Characters(iOS, 1).Font.Background
' .Parent = rngFrom2.Characters(iOS, 1).Font.Parent
' .Creator = rngFrom2.Characters(iOS, 1).Font.Creator
'.Application = rngFrom2.Characters(iOS, 1).Font.Application
End With
Next iOS
End Sub
ĐOẠN MÃ 1:
Sub Merge_Cells()
Dim iOS As Integer
Dim rngFrom1 As Range
Dim rngFrom2 As Range
Dim rngTo As Range
Dim lenFrom1 As Integer
Dim lenFrom2 As Integer
Set rngFrom1 = Cells(1, 1)
Set rngFrom2 = Cells(1, 2)
Set rngTo = Cells(1, 3)
lenFrom1 = rngFrom1.Characters.Count
lenFrom2 = rngFrom2.Characters.Count
rngTo.Value = rngFrom1.Text & rngFrom2.Text
For iOS = 1 To lenFrom1
With rngTo.Characters(iOS, 1).Font
.Name = rngFrom1.Characters(iOS, 1).Font.Name
.Bold = rngFrom1.Characters(iOS, 1).Font.Bold
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom1.Characters(iOS, 1).Font.ColorIndex
End With
Next iOS
For iOS = 1 To lenFrom2
With rngTo.Characters(lenFrom1 + iOS, 1).Font
.Name = rngFrom2.Characters(iOS, 1).Font.Name
.Bold = rngFrom2.Characters(iOS, 1).Font.Bold
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom2.Characters(iOS, 1).Font.ColorIndex
End With
Next iOS
End Sub
ĐOẠN MÃ 2:
Sub MergeCells(rngFrom1 As Range, rngFrom2 As Range, rngTo As Range, Separateur As String)
Dim iOS As Integer
Dim lenFrom1 As Long
Dim lenFrom2 As Long
lenFrom1 = rngFrom1.Characters.Count
lenFrom2 = rngFrom2.Characters.Count
rngTo.WrapText = True
rngTo.Value = rngFrom1.Text & Separateur & rngFrom2.Text
For iOS = 1 To lenFrom1
With rngTo.Characters(iOS, 1).Font
.Name = rngFrom1.Characters(iOS, 1).Font.Name
.Bold = rngFrom1.Characters(iOS, 1).Font.Bold
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom1.Characters(iOS, 1).Font.ColorIndex
.Color = rngFrom1.Characters(iOS, 1).Font.Color
.FontStyle = rngFrom1.Characters(iOS, 1).Font.FontStyle
.Italic = rngFrom1.Characters(iOS, 1).Font.Italic
.OutlineFont = rngFrom1.Characters(iOS, 1).Font.OutlineFont
.Shadow = rngFrom1.Characters(iOS, 1).Font.Shadow
.Size = rngFrom1.Characters(iOS, 1).Font.Size
.Strikethrough = rngFrom1.Characters(iOS, 1).Font.Strikethrough
.Subscript = rngFrom1.Characters(iOS, 1).Font.Subscript
.Superscript = rngFrom1.Characters(iOS, 1).Font.Superscript
.Underline = rngFrom1.Characters(iOS, 1).Font.Underline
' .Background = rngFrom1.Characters(iOS, 1).Font.Background
' .Parent = rngFrom1.Characters(iOS, 1).Font.Parent
' .Creator = rngFrom1.Characters(iOS, 1).Font.Creator
' .Application = rngFrom1.Characters(iOS, 1).Font.Application
End With
Next iOS
For iOS = 1 To lenFrom2
With rngTo.Characters(lenFrom1 + Len(Separateur) + iOS, 1).Font
.Name = rngFrom2.Characters(iOS, 1).Font.Name
.Bold = rngFrom2.Characters(iOS, 1).Font.Bold
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.ColorIndex = rngFrom2.Characters(iOS, 1).Font.ColorIndex
.Color = rngFrom2.Characters(iOS, 1).Font.Color
.FontStyle = rngFrom2.Characters(iOS, 1).Font.FontStyle
.Italic = rngFrom2.Characters(iOS, 1).Font.Italic
.OutlineFont = rngFrom2.Characters(iOS, 1).Font.OutlineFont
.Shadow = rngFrom2.Characters(iOS, 1).Font.Shadow
.Size = rngFrom2.Characters(iOS, 1).Font.Size
.Strikethrough = rngFrom2.Characters(iOS, 1).Font.Strikethrough
.Subscript = rngFrom2.Characters(iOS, 1).Font.Subscript
.Superscript = rngFrom2.Characters(iOS, 1).Font.Superscript
.Underline = rngFrom2.Characters(iOS, 1).Font.Underline
' .Background = rngFrom2.Characters(iOS, 1).Font.Background
' .Parent = rngFrom2.Characters(iOS, 1).Font.Parent
' .Creator = rngFrom2.Characters(iOS, 1).Font.Creator
'.Application = rngFrom2.Characters(iOS, 1).Font.Application
End With
Next iOS
End Sub
Sửa lần cuối: