简体   繁体   English

将B列的拆分数据粘贴到D&E列中

[英]Pasting Split Data From Column B into Columns D & E

In column B there is the 5 character item code + the item description. 在B列中,有5个字符的项目代码+项目描述。 I managed to make a macro that copy/pastes the 5 character item codes from column B into column D and the item descriptions into column E. 我设法制作一个宏,将B列中的5个字符项代码复制/粘贴到D列,将项目描述复制/粘贴到E列中。

Example: 例:

          Column B              Column C  Column D     Column E
XX787 DO BOLOGNESE 2X2.28KG FR   <other>    XX787    DO BOLOGNESE 2X2.28KG FR

I am facing the following issues: 我面临以下问题:

  • About 1% of the item codes are not 5 characters but 8 characters, for example 例如,大约1%的商品代码不是5个字符,而是8个字符
  • This percentage that exists out of 8 characters exists of numbers and starts with 0 存在8个字符的百分比存在数字,以0开头

As you can see, the last 2 are examples of item codes that have a different format, and even though there is only a small amount of them, they have to be taken into account of course. 如您所见,最后2个是具有不同格式的项目代码的示例,即使只有少量项目代码,当然也必须考虑它们。

So with my current coding, the following issues arise: 因此,根据我目前的编码,会出现以下问题:

Hier een照片

I think the macro should do the following to make this work properly: 我认为宏应该执行以下操作以使其正常工作:

For item codes in column D: Copy/paste all the characters BEFORE the first space from column B For item descriptions in column E: Copy/paste all the characters AFTER the first space 对于D列中的项目代码:复制/粘贴B列第一个空格之前的所有字符对于E列中的项目描述:复制/粘贴第一个空格后的所有字符

The code I am currently running for this is the following: 我目前为此运行的代码如下:

Sub Seperate_Item_Code_And_Description_Code()
    'Copy/paste the item codes and descriptions from column B to column D and E seperately

    Range("B12").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("D12"), DataType:=xlFixedWidth, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    FieldInfo:=Array(Array(0, 1), Array(6, 1)), TrailingMinusNumbers:= _
    True
End Sub

Try this coded and conditional text-to-columns. 尝试这种编码和条件文本到列。

Option Explicit

Sub wqewtrqw()
    Dim s As Long, a As Long, aVALs As Variant

    With Worksheets("Sheet5")
        aVALs = .Range(.Cells(12, "B"), .Cells(.Rows.Count, "B").End(xlUp)).Value2
        ReDim Preserve aVALs(LBound(aVALs, 1) To UBound(aVALs, 1), 1 To 2)
        For a = LBound(aVALs, 1) To UBound(aVALs, 1)
            s = InStr(1, aVALs(a, 1), Chr(32))
            aVALs(a, 2) = Mid(aVALs(a, 1), s + 1)
            aVALs(a, 1) = Left(aVALs(a, 1), s - 1)
        Next a
        .Cells(12, "D").Resize(UBound(aVALs, 1), UBound(aVALs, 2)) = aVALs
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM