Excel “连接”显示除第一个值外的第二个和第三个值

Excel “连接”显示除第一个值外的第二个和第三个值,excel,concatenation,vba,Excel,Concatenation,Vba,我有一个宏,它连接了3个单元格中的值,问题是它只连接了第一个和第二个值,在它只连接第一个值之前,第一个值是3,第二个值是3月,第三个值是2015年,所以结果就是“3march” 这是我的代码: Private Sub CommandButton1_Click() Sheets("Info").Activate Range("M2:Q2").Select Selection.Copy Range("A2").Cells(1).Select

我有一个宏,它连接了3个单元格中的值,问题是它只连接了第一个和第二个值,在它只连接第一个值之前,第一个值是3,第二个值是3月,第三个值是2015年,所以结果就是“3march”

这是我的代码:

    Private Sub CommandButton1_Click()

    Sheets("Info").Activate

     Range("M2:Q2").Select
     Selection.Copy
     Range("A2").Cells(1).Select

     Do While ActiveCell.Value <> ""
      ActiveCell.Offset(1, 0).Select
    Loop
  Selection.Offset(0, 12).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
      SkipBlanks:=False, Transpose:=False
       Selection.Offset(0, -12).Select
       Selection.Cells(1).Select


  'ahora viene la parte de pegar los valores,,,
  Selection.Cells(1).Value = TextBox1.Value
  Selection.Cells(1, 2).Value = ComboBox1.Value
  Selection.Cells(1, 3).Value = ComboBox4.Value
  Selection.Cells(1, 4).Value = ComboBox5.Value
  Selection.Cells(1, 5).Value = ComboBox6.Value
  Selection.Cells(1, 6).Value = ComboBox9.Value
   Selection.Cells(1, 7).Value = ComboBox13.Value
   Selection.Cells(1, 8).Value = TextBox3.Value
    Selection.Cells(1, 9).Value = TextBox4.Value
    Selection.Cells(1, 10).Value = TextBox8.Value
    Selection.Cells(1, 11).Value = TextBox6.Value
     Selection.Cells(1, 12).Value = ComboBox12.Value

     'HERE IS THE PROBLEM!!!!!!
      f_env = Selection.Cells(1, 3).Value & "-" & Selection.Cells(1,          4).Value & "-" & Selection.Cells(1, 5).Value

         Selection.Cells(1, 17).Value = f_env



     Unload UserForm1
     End Sub
Private子命令按钮1\u单击()
工作表(“信息”)。激活
范围(“M2:Q2”)。选择
选择,复制
范围(“A2”)。单元格(1)。选择
当ActiveCell.Value“”时执行此操作
ActiveCell.Offset(1,0)。选择
环
选择。偏移(0,12)。选择
Selection.Paste特殊粘贴:=xlPasteFormulas,操作:=xlNone_
SkipBlanks:=False,转置:=False
选择。偏移量(0,-12)。选择
选择。单元格(1)。选择
“阿霍拉·维恩·帕德·佩加·洛斯·瓦洛雷斯,”,,,
Selection.Cells(1).Value=TextBox1.Value
Selection.Cells(1,2).Value=ComboBox1.Value
Selection.Cells(1,3).Value=ComboBox4.Value
Selection.Cells(1,4).Value=ComboBox5.Value
Selection.Cells(1,5).Value=ComboBox6.Value
Selection.Cells(1,6).Value=ComboBox9.Value
Selection.Cells(1,7).Value=ComboBox13.Value
Selection.Cells(1,8).Value=TextBox3.Value
Selection.Cells(1,9).Value=TextBox4.Value
Selection.Cells(1,10).Value=TextBox8.Value
Selection.Cells(1,11).Value=TextBox6.Value
Selection.Cells(1,12).Value=ComboBox12.Value
这就是问题所在!!!!!!
f_env=Selection.Cells(1,3).Value&“-”和Selection.Cells(1,4).Value&“-”和Selection.Cells(1,5).Value
Selection.Cells(1,17).Value=f_env
卸载UserForm1
端接头
编辑: 添加了新代码

很难运行代码,因为只有您拥有工作簿,我们必须自己尝试并复制工作簿。这里是一个修改后的代码,我已经解释了您要做的事情。我不能测试它,因为我没有你的工作簿

Private Sub CommandButton1_Click()
    Dim sh As Worksheet
    Dim Crng As Range    'copy range
    Dim LrWs As Long    'last row
    Dim Prng As Range    'paste range
    Dim Drng As Range
    Dim f_env As String

    Set sh = Worksheets("Info")

    With sh

        Set Crng = .Range("M2:Q2")
        LrWs = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        Set Prng = .Cells(LrWs, 13)
        Crng.Copy
        Prng.PasteSpecial xlPasteAll
        Application.CutCopyMode = 0

        Set Drng = .Cells(LrWs, 1)

        Drng.Value = TextBox1.Value
        Drng.Offset(0, 1).Value = ComboBox1.Value
        Drng.Offset(0, 2).Value = ComboBox4.Value
        Drng.Offset(0, 3).Value = ComboBox5.Value
        Drng.Offset(0, 4).Value = ComboBox6.Value
        Drng.Offset(0, 5).Value = ComboBox9.Value
        Drng.Offset(0, 6).Value = ComboBox13.Value
        Drng.Offset(0, 7).Value = TextBox3.Value
        Drng.Offset(0, 8).Value = TextBox4.Value
        Drng.Offset(0, 9).Value = TextBox8.Value
        Drng.Offset(0, 10).Value = TextBox6.Value
        Drng.Offset(0, 11).Value = ComboBox12.Value

    End With

    f_env = ComboBox4.Value & "-" & ComboBox5.Value & "-" & ComboBox6.Value

    Drng.Offset(0, 16) = f_env

End Sub
如果您无法正常工作,请将我们链接到示例工作簿。

因为列(在所选内容行上)实际上是由所选内容所在的列决定的,只有当所选内容位于列a中时,您才能在列Q中获得日期。这可能是一个次要问题,但我相信目标应该始终是某个列

Sub gather_date()
    Dim sel As Range

    With Selection.Parent
        For Each sel In Selection.Columns(1).Cells
            If Application.CountA(sel.Resize(1, 3)) = 3 Then
                .Cells(sel.Row, 17) = CDate(Join(Array(sel.Value2, Left(sel.Offset(0, 1).Value2, 3), sel.Offset(0, 2).Value2), "-"))
                .Cells(sel.Row, 17).NumberFormat = "d-mmmm-yyyy"
            End If
        Next sel
    End With

End Sub
因为您使用的是基于select的宏,所以我添加了一个循环来处理select中的所有单元格

这也可以在Q2中使用以下工作表公式执行

=DATEVALUE(CONCATENATE(B2, "-", C2, "-", D2))

格式化为日期并根据需要填写。

您使用的是
选择
对象。在VBA中,这是@Mr.Mascaro-在此上下文中不一定是@TimWilliams(假设代码在用户先前选择的范围内运行)@TimWilliams,即使它基于选择,也应将选择设置为另一个对象。这是基本的OOP。如果一个好的程序员接手,它会使代码在以后更易于维护。当我用另一个宏来做这件事时,它也适用于我。请再次检查我的问题,请输入我的完整代码。感谢添加的代码,删除选择并使用偏移量。如果偏移量不在正确的位置,就可以使用偏移量。用偏移量替换单元格选项是有效的。谢谢我还可以用什么来代替选择?