Excel: 如何将文本从多个单元格移动到第一个单元格?
Excel: How to move text from multiple cells to first cell?
我有一个拥抱exceltable:
我需要一个公式来将数据移动到第一个单元格,如下所示。
请帮我做到这一点。
这行得通。请务必将 source_rng 设置为您想要的范围。也应该扩大规模。编辑:现在意识到这不会保留颜色,将在那个上工作
Sub main()
Dim ws As Worksheet
Dim source_rng As Range
Dim target_rng As Range
Dim source_data() As Variant
Dim clean_data() As Variant
Dim column_count As Long
Dim row_count As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set ws = ActiveSheet
Set source_rng = ws.Range("A1:F19200")
column_count = source_rng.Columns.Count
row_count = source_rng.Rows.Count
ReDim source_data(1 To row_count, 1 To column_count)
ReDim clean_data(1 To row_count * column_count, 1)
source_data = source_rng
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
k = k + 1
End If
Next i
Next j
ReDim clean_data(1 To k, 1)
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
clean_data(k, 0) = source_data(j, i)
k = k + 1
End If
Next i
Next j
source_rng.Clear
Set target_range = ThisWorkbook.ActiveSheet.Range("A1:A" & UBound(clean_data))
target_range.value = clean_data
End Sub
我有一个拥抱exceltable:
我需要一个公式来将数据移动到第一个单元格,如下所示。
请帮我做到这一点。
这行得通。请务必将 source_rng 设置为您想要的范围。也应该扩大规模。编辑:现在意识到这不会保留颜色,将在那个上工作
Sub main()
Dim ws As Worksheet
Dim source_rng As Range
Dim target_rng As Range
Dim source_data() As Variant
Dim clean_data() As Variant
Dim column_count As Long
Dim row_count As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set ws = ActiveSheet
Set source_rng = ws.Range("A1:F19200")
column_count = source_rng.Columns.Count
row_count = source_rng.Rows.Count
ReDim source_data(1 To row_count, 1 To column_count)
ReDim clean_data(1 To row_count * column_count, 1)
source_data = source_rng
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
k = k + 1
End If
Next i
Next j
ReDim clean_data(1 To k, 1)
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
clean_data(k, 0) = source_data(j, i)
k = k + 1
End If
Next i
Next j
source_rng.Clear
Set target_range = ThisWorkbook.ActiveSheet.Range("A1:A" & UBound(clean_data))
target_range.value = clean_data
End Sub