简体   繁体   English

如何将 2 个单元格值与 VBA 合并为一个单元格?

[英]How to combine 2 cell value into one cell with VBA?

在此处输入图像描述

How to use VBA combine Col A and B together with ";",and remove Col A and B after combined,如何使用 VBA 将 Col A 和 B 与“;”组合在一起,并在组合后删除 Col A 和 B,

在此处输入图像描述

Try below sub-试试下面的子

Sub MergeCells()
Dim rng As Range
Dim LastRow As Long

  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  
  For Each rng In Range("A2:A" & LastRow)
    rng = rng & ";" & rng.Offset(0, 1)
    rng.Offset(0, 1).Clear
  Next

End Sub

Concatenate Columns连接列

Option Explicit

Sub ConcatColumns()
    
    Const wsName As String = "Sheet1"
    Const Cols As String = "A:B"
    Const Col As String = "A"
    Const fRow As Long = 2
    Const Delimiter As String = ";"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
    
    Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, Col).End(xlUp).Row
    If lRow < fRow Then Exit Sub ' no data
    Dim rCount As Long: rCount = lRow - fRow + 1
    Dim rg As Range: Set rg = ws.Rows(fRow).Columns(Cols).Resize(rCount)
        
    rg.Columns(1).Value = Evaluate(rg.Address(0, 0) & "&""" _
        & Delimiter & """&" & rg.Columns(2).Address(0, 0))
    
    rg.Columns(2).ClearContents
    
End Sub

Thanks all, very useful answer谢谢大家,非常有用的答案

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过VBA宏在Excel中的一个特定单元格中合并多个单元格值(动态)? - How to combine multiple cells value (dynamic) in one specific cell in excel via VBA macro? Excel VBA中的自定义函数,该函数在返回多个匹配值并将其组合到一个单元格的范围内查找单元格值 - Custom function in excel vba that lookup a cell value in a range that returns multiple match values and combine them in one cell 合并与活动单元格 VBA 相邻的单元格中的两个单元格的值 - Combine the value of two cells in the cell which is adjacent to Active cell VBA VBA:检查一个单元格是否包含某个值以及如何将该值复制/粘贴到另一个单元格 - VBA: Check if one cell contains some value and how to copy/paste that value to another cell 将行合并为一个单元格 - Combine rows into one cell 根据另一个单元格VBA的值锁定/解锁一个单元格 - Lock/Unlock One Cell Based on Value of Another Cell VBA 一次仅读取一个单元格来自Excel vba的单元格值 - read only one cell at a time Cell value from Excel vba 日期和时间框用户窗体VBA合并为一个单元格 - Date and time box userform VBA combine to one cell 如何使用vba中的集合与一个单元格的值进行比较来更新另一个单元格 - How to use a collection in vba to update another cell comparing with a value of one cell 如何使用 VBA 将一张纸上的 3 个单元格组合成另一张纸上的 1 个单元格? - How to use VBA to combine 3 cells from one sheet into 1 cell in another sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM