连接显示除第一个值之外的第二个和第三个值

我有这个macros来连接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 

编辑:添加新的代码。

很难像没有人一样运行代码,但你有工作簿,我们将不得不自己尝试重现工作簿。 这是一个修改后的代码,因为我已经解释了你正在尝试做的事情。 我不能testing它,因为我没有你的工作簿。

 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 

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

由于列(在select行上)实际上是由所选列所在的列确定的,所以只有在列A中select时,才会在列Q中获得date。这可能是次要的一点,但我认为目标应始终为某一列。

构建日期

 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 

既然你是基于selectmacros指令,我已经添加了一个循环,将处理所有select的单元格。

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

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

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