excel - Excel: How to move text from multiple cells to first cell?
问题描述
i have a hug excel table:
I need a formula to move data to the first cell, like below.
please help me to do that.
解决方案
这行得通。只要确保将 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
推荐阅读
- ajax - 在 Laravel 中切换活动/非活动状态
- java - 如何在 Java 中包含和比较字符串和数组
- c# - 用于编辑 WordprocessingDocument 的 wpf 控件
- java - 如何以正确的方式实现构建器设计模式?
- web - MediaStream---为什么我的音轨的静音属性是真的?
- html - 在一张表中插入多个数据
- moodle - 在moodle / totara中创建一个本地插件,在用户创建的每门课程中注册用户,并在课程创建中注册所有用户
- reactjs - 带有子集合的查询必须使用“storeAs”我应该如何解决这个问题?
- node.js - Discord Bot,无法将其提供给使用命令的提及用户
- gradle - Sonarqube 看不到 Jacoco 的覆盖结果