You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求Excel VB代码:当A1:A200单元格为空时合并B1:C1单元格内容

VBA Code to Merge B & C Columns When Column A is Empty

Hey there! I’ve got the perfect VBA solution for your Excel task. Let’s dive right in:

The Code

Sub MergeBAndCWhenAIsEmpty()
    Dim ws As Worksheet
    Dim i As Integer
    
    ' Set your target worksheet (update "Sheet1" to match your sheet name)
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Loop through rows 1 to 200
    For i = 1 To 200
        ' Check if column A in the current row is blank (Trim handles hidden spaces)
        If Trim(ws.Cells(i, "A").Value) = "" Then
            ' Combine B and C values into B (add a space separator - tweak this as needed!)
            ws.Cells(i, "B").Value = ws.Cells(i, "B").Value & " " & ws.Cells(i, "C").Value
            
            ' Optional: Uncomment the line below to clear column C after merging
            ' ws.Cells(i, "C").ClearContents
        End If
    Next i
End Sub

Key Details & Customization

  • Worksheet Target: Make sure to replace "Sheet1" with the actual name of your worksheet (e.g., "SalesData").
  • Separator: The code uses a space (" ") between B and C values. You can change this to a comma (", "), line break (vbNewLine), or any other separator that fits your needs.
  • Clear Column C: If you want to empty column C after merging its content with B, just remove the single quote (') from the ws.Cells(i, "C").ClearContents line.

How to Implement

  1. Open your Excel file.
  2. Press Alt + F11 to launch the VBA Editor.
  3. In the Project Explorer pane (left side), right-click your workbook > Insert > Module.
  4. Paste the code into the new module window.
  5. Run the macro by pressing F5, or assign it to a button on your worksheet for quick access later.

内容的提问来源于stack exchange,提问作者Marc Pluck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:15:57