excel vba 从列中复制值并将值粘贴到单元格中
excel vba copy value from a column and paste value in a cell
我有如下数据。第一列属于 A 列,第二列属于 B 列。
1 q
1 q
2 q
2 q
2 q
3 q
我想在 A 列中的值发生变化时插入空行。要插入行,我使用 this site 中的宏。
'select column a before running the macro
Sub InsertRowsAtValueChange()
'Update 20140716
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
For i = WorkRng.Rows.Count To 2 Step -1
If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then
WorkRng.Cells(i, 1).EntireRow.Insert
End If
Next
Application.ScreenUpdating = True
End Sub
之后,我想从 A 列复制每组值并粘贴到 C 列的单元格中。粘贴它们时,我想将值粘贴到 a 中的单元格 行格式(通过连接它们)并用 space 分隔它们.在下面的例子中,单元格 c1 应该有 1 1
,单元格 c4 应该有 2 2 2
,单元格 c8 应该有 3
如何做到这一点?我尝试使用首先复制每组值然后在转置成一行后粘贴它们来记录宏。但是我很难再次复制值并将它们粘贴到一个单元格中
我有这个功能,它的工作方式与内置 Concatenate()
类似,但为您提供过滤功能。我似乎没有完全帮助你可能会为你的最终目标提供另一种方法。
Function ConcatenateIf(CriteriaRange As Range, Condition As Variant, _
ConcatenateRange As Range, Optional Separator As String = ",") As Variant
Dim i As Long
Dim strResult As String
On Error GoTo ErrHandler
If CriteriaRange.Count <> ConcatenateRange.Count Then
ConcatenateIf = CVErr(xlErrRef)
Exit Function
End If
For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition Then
strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i
If strResult <> "" Then
strResult = Mid(strResult, Len(Separator) + 1)
End If
ConcatenateIf = strResult
Exit Function
ErrHandler:
ConcatenateIf = CVErr(xlErrValue)
End Function
以下代码的前后对比:
Option Explicit
Sub InsertRowsAtValueChange()
Dim rng As Range, itms As Variant, cel As Range, i As Long, firstRow As Long
Set rng = Range("A3:A1000")
firstRow = rng.Row - 1
Application.ScreenUpdating = False
For i = rng.Rows.Count To 1 Step -1
If rng.Cells(i, 1).Value2 <> rng.Cells(i - 1, 1).Value2 Then
If i < rng.Row - 1 Then
Set cel = rng(i, 1)
Else
rng.Cells(i, 1).EntireRow.Insert
Set cel = rng(i + 1, 1)
End If
With cel.CurrentRegion
itms = .Columns(1)
If .Columns(1).Rows.Count > 1 Then itms = Join(Application.Transpose(itms))
cel.Offset(0, 2) = itms
End With
End If
If i = 1 Then Exit For
Next
Application.ScreenUpdating = True
End Sub
我有如下数据。第一列属于 A 列,第二列属于 B 列。
1 q
1 q
2 q
2 q
2 q
3 q
我想在 A 列中的值发生变化时插入空行。要插入行,我使用 this site 中的宏。
'select column a before running the macro
Sub InsertRowsAtValueChange()
'Update 20140716
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
For i = WorkRng.Rows.Count To 2 Step -1
If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then
WorkRng.Cells(i, 1).EntireRow.Insert
End If
Next
Application.ScreenUpdating = True
End Sub
之后,我想从 A 列复制每组值并粘贴到 C 列的单元格中。粘贴它们时,我想将值粘贴到 a 中的单元格 行格式(通过连接它们)并用 space 分隔它们.在下面的例子中,单元格 c1 应该有 1 1
,单元格 c4 应该有 2 2 2
,单元格 c8 应该有 3
如何做到这一点?我尝试使用首先复制每组值然后在转置成一行后粘贴它们来记录宏。但是我很难再次复制值并将它们粘贴到一个单元格中
我有这个功能,它的工作方式与内置 Concatenate()
类似,但为您提供过滤功能。我似乎没有完全帮助你可能会为你的最终目标提供另一种方法。
Function ConcatenateIf(CriteriaRange As Range, Condition As Variant, _
ConcatenateRange As Range, Optional Separator As String = ",") As Variant
Dim i As Long
Dim strResult As String
On Error GoTo ErrHandler
If CriteriaRange.Count <> ConcatenateRange.Count Then
ConcatenateIf = CVErr(xlErrRef)
Exit Function
End If
For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition Then
strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i
If strResult <> "" Then
strResult = Mid(strResult, Len(Separator) + 1)
End If
ConcatenateIf = strResult
Exit Function
ErrHandler:
ConcatenateIf = CVErr(xlErrValue)
End Function
以下代码的前后对比:
Option Explicit
Sub InsertRowsAtValueChange()
Dim rng As Range, itms As Variant, cel As Range, i As Long, firstRow As Long
Set rng = Range("A3:A1000")
firstRow = rng.Row - 1
Application.ScreenUpdating = False
For i = rng.Rows.Count To 1 Step -1
If rng.Cells(i, 1).Value2 <> rng.Cells(i - 1, 1).Value2 Then
If i < rng.Row - 1 Then
Set cel = rng(i, 1)
Else
rng.Cells(i, 1).EntireRow.Insert
Set cel = rng(i + 1, 1)
End If
With cel.CurrentRegion
itms = .Columns(1)
If .Columns(1).Rows.Count > 1 Then itms = Join(Application.Transpose(itms))
cel.Offset(0, 2) = itms
End With
End If
If i = 1 Then Exit For
Next
Application.ScreenUpdating = True
End Sub