将单元格块复制到行中?
Copy blocks of cells into rows?
我有一个包含 2 列数据的电子表格。在这两列中,有重复的信息块,这些信息块构成了联系人记录。他们有一个一致的模式。
这是模式:
我正在寻找一种方法将此数据复制到新的 Excel 选项卡中,每个联系人记录一行:
姓名、地址、单位、相互、Phone 号码
有谁知道通过函数或宏来做到这一点的方法吗?
谢谢!
这个宏可以解决问题。只需确保 sheet 与您的源数据在 运行 时处于活动状态。
Sub copy_data()
Dim source As Worksheet
Dim dest As Worksheet
Dim r As Long
Dim row As Long
Set source = ActiveSheet
Set dest = ThisWorkbook.Worksheets.Add
dest.Cells(1, 1).Value = "Name"
dest.Cells(1, 2).Value = "Address"
dest.Cells(1, 3).Value = "Unit"
dest.Cells(1, 4).Value = "Mutual"
dest.Cells(1, 5).Value = "Phone Number"
r = 1
row = 1
Do Until source.Cells(r, 1).Value = ""
row = row + 1
dest.Cells(row, 1).Value = source.Cells(r, 1).Value
dest.Cells(row, 2).Value = source.Cells(r + 1, 1).Value
dest.Cells(row, 3).Value = source.Cells(r + 1, 2).Value
dest.Cells(row, 4).Value = source.Cells(r + 2, 1).Value
dest.Cells(row, 5).Value = source.Cells(r + 2, 2).Value
r = r + 3
Loop
End Sub
我有一个包含 2 列数据的电子表格。在这两列中,有重复的信息块,这些信息块构成了联系人记录。他们有一个一致的模式。
这是模式:
我正在寻找一种方法将此数据复制到新的 Excel 选项卡中,每个联系人记录一行: 姓名、地址、单位、相互、Phone 号码
有谁知道通过函数或宏来做到这一点的方法吗?
谢谢!
这个宏可以解决问题。只需确保 sheet 与您的源数据在 运行 时处于活动状态。
Sub copy_data()
Dim source As Worksheet
Dim dest As Worksheet
Dim r As Long
Dim row As Long
Set source = ActiveSheet
Set dest = ThisWorkbook.Worksheets.Add
dest.Cells(1, 1).Value = "Name"
dest.Cells(1, 2).Value = "Address"
dest.Cells(1, 3).Value = "Unit"
dest.Cells(1, 4).Value = "Mutual"
dest.Cells(1, 5).Value = "Phone Number"
r = 1
row = 1
Do Until source.Cells(r, 1).Value = ""
row = row + 1
dest.Cells(row, 1).Value = source.Cells(r, 1).Value
dest.Cells(row, 2).Value = source.Cells(r + 1, 1).Value
dest.Cells(row, 3).Value = source.Cells(r + 1, 2).Value
dest.Cells(row, 4).Value = source.Cells(r + 2, 1).Value
dest.Cells(row, 5).Value = source.Cells(r + 2, 2).Value
r = r + 3
Loop
End Sub