将不同的单元格组合到同一行

时间:2014-02-19 15:58:58

标签: excel vba

我希望每条记录在同一行:

Row 1 Column 1: Parcel Number,  Row 1 Column 2: Owner Name, Row 1 Column 3: Address

示例:

包裹记录21033540290000 所有者Doe,John M,SR。 &安培;简H; TRS 地址2116 W Wall St. 包裹记录21033540450000 所有者布朗,MARSHA&绿色,玛丽; JT 地址2131 W Harvard Ave. 包裹记录21033230450000 所有者Smith,MICHELLE K. 地址4281 S Yale Ave

我希望格式与3列在同一行:

21033540290000  Doe, John M, SR. & Jane H; TRS          2116 W Wall St
21033540450000  Brown, MARSHA & GREEN, MARIE; JT        2131 W Harvard Ave
21033230450000  Smith, MICHELLE K                       4281 S Yale Ave

然后在将数据复制到与地块编号相同的行后删除所有者和地址行。

录制了一个宏,但是有问题循环和放大引用单元格和行。 Sub FormatRows() “ 'FormatRows宏 '格式化为同一行 “ '键盘快捷键:Ctrl + f

Range("B1").Select
Selection.Cut
Range("A1").Select
ActiveSheet.Paste
Range("B2").Select
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Range("B3").Select
Selection.Cut
Range("C1").Select
ActiveSheet.Paste
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Selection.Delete Shift:=xlUp
Range("B2").Select
Selection.Cut
Range("A2").Select
ActiveSheet.Paste
Range("B3").Select
Selection.Cut
Range("B2").Select
ActiveSheet.Paste
Range("B4").Select
Selection.Cut
Range("C2").Select
ActiveSheet.Paste
Rows("3:3").Select
Selection.Delete Shift:=xlUp
Selection.Delete Shift:=xlUp
Range("B3").Select
Selection.Cut
Range("A3").Select
ActiveSheet.Paste
Range("B4").Select
Selection.Cut
Range("B3").Select
ActiveSheet.Paste
Range("B5").Select
Selection.Cut
Range("C3").Select
ActiveSheet.Paste
Rows("4:4").Select
Selection.Delete Shift:=xlUp
Selection.Delete Shift:=xlUp
Range("B4").Select

End Sub

'我怎样才能让它循环到几千行?

谢谢!

1 个答案:

答案 0 :(得分:0)

这需要字符串处理

第1步:找到数字部分:

Dim strTemp As String
Dim strChar As String
strTemp = "Parcel Record 21033540290000 Owner Doe, John M, SR. & Jane H; TRS Address 2116 W Wall St "


flag = True
i = 1
On Error GoTo lblEnd:
While flag = True
    strChar = Strings.Mid(strTemp, i, 1)
    If IsNumeric(strChar) Then
        'your code here
    End If
    i = i + 1
Wend

lblEnd:
Err.Clear

第2步:找到“,”和“:”。只需更改上一代码的if部分:

 If strChar = "," Then
     'your code here
 End If

和此:

 If strChar = ":" Then
     'your code here
 End If