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

Excel VBA:如何用Concat函数结合手机号与自动年月(或手动输入)

解决方案

一、用公式自动实现拼接

直接在Sheet2的B2单元格输入以下公式,就能自动将Sheet1的手机号与当前月份年份拼接成目标格式:

=CONCAT(Sheet1!A2," - ",TEXT(TODAY(),"mmm yy"))
  • TEXT(TODAY(),"mmm yy") 是核心逻辑:TODAY() 获取当前系统日期,TEXT 函数将日期格式化为缩写月份(如Nov)加两位年份(如23)的样式。
  • 若需固定为某一特定月份而非当前,只需把TODAY()替换为具体日期(例如DATE(2023,11,1))即可。

二、VBA实现手动输入备选方案

如果公式自动获取日期出现异常,可通过VBA代码实现手动输入日期后完成拼接:

  1. 按Alt + F11打开VBA编辑器;
  2. 右键左侧工程窗口中的当前工作簿,选择「插入」→「模块」;
  3. 粘贴以下代码:
Sub ConcatenatePhoneAndDate()
    Dim phone As String
    Dim inputDate As Variant
    Dim formattedDate As String
    
    ' 获取Sheet1的手机号
    phone = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
    
    ' 尝试自动获取当前日期格式,失败则弹出输入框
    On Error Resume Next
    formattedDate = Format(Date, "mmm yy")
    On Error GoTo 0
    
    If formattedDate = "" Then
        inputDate = InputBox("请输入日期(格式如2023/11/1):")
        If IsDate(inputDate) Then
            formattedDate = Format(inputDate, "mmm yy")
        Else
            MsgBox "输入的日期格式无效,请重试。"
            Exit Sub
        End If
    End If
    
    ' 拼接并写入Sheet2的B2
    ThisWorkbook.Sheets("Sheet2").Range("B2").Value = phone & " - " & formattedDate
End Sub
  1. 按F5运行代码,若自动获取失败,会弹出输入框让你手动输入日期,确认后自动完成拼接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 09:13:12