连接显示除第一个值之外的第二个和第三个值
Concatenate shows the second and third value except the first value
我有这个连接来自 3 个单元格的值的宏,问题是它只连接第一个和第二个值,在它只连接第一个值之前是 3,第二个值是 march,第三个值是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 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
Link 如果您无法正常工作,请给我们一个示例工作簿。
因为列(在 selection 的行上)实际上是由 selection 所在的列确定的,所以只有在 select 时,您才会在 Q 列中获得日期离子在 A 列中。这可能是一个小问题,但我相信目的地应该总是 是某个列。
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 的宏,我添加了一个循环来处理 selection 中的所有单元格。
这也可以在第二季度使用以下工作表公式执行。
=DATEVALUE(CONCATENATE(B2, "-", C2, "-", D2))
格式为日期并根据需要填写。
我有这个连接来自 3 个单元格的值的宏,问题是它只连接第一个和第二个值,在它只连接第一个值之前是 3,第二个值是 march,第三个值是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 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
Link 如果您无法正常工作,请给我们一个示例工作簿。
因为列(在 selection 的行上)实际上是由 selection 所在的列确定的,所以只有在 select 时,您才会在 Q 列中获得日期离子在 A 列中。这可能是一个小问题,但我相信目的地应该总是 是某个列。
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 的宏,我添加了一个循环来处理 selection 中的所有单元格。
这也可以在第二季度使用以下工作表公式执行。
=DATEVALUE(CONCATENATE(B2, "-", C2, "-", D2))
格式为日期并根据需要填写。