繁体   English   中英

Excel VBA文本到列

[英]Excel VBA Text To Columns

早安和提前感谢,我正在尝试运行VBA宏以使我的最终用户能够将条形码扫描到excel然后使用命令按钮将条形码拆分为其组件类型,使用文本到列vba宏,然后将其分解到项目,第1页和最后一页框。 由于有3种不同长度的条形码必须在不同的地方分开,作为权宜之计,我在工作簿中创建了3个选项卡,以便他们可以扫描和分解项目。 理想情况下,我希望能够在1个工作表上完成所有操作并让编码识别需要拆分的内容和位置。 下面是我必须为其中一个项目工作的编码,但它只识别第一组数组,而不是后续的3.可以有人告诉我如何添加额外的数组以获得不同的条形码分割,正确的立场

Sub BarcodeSplit()
On Error GoTo myEnd:

    ' BarcodeSplit Macro
    '
    ' Keyboard Shortcut: Ctrl+b
    '
        ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
        ActiveCell.Select
        ActiveCell.FormulaR1C1 = "Item"
        ActiveCell.Offset(1, -1).Range("A1").Select
        Range("B2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.TextToColumns Destination:=Range("B2"), DataType:=xlFixedWidth, _
            FieldInfo:=Array(Array(0, 2), Array(10, 2), Array(17, 2), Array(28, 2)), _
            FieldInfo:=Array(Array(0, 2), Array(10, 2), Array(17, 2), Array(28, 2)), _
            FieldInfo:=Array(Array(0, 2), Array(14, 2), Array(21, 2), Array(32, 2)), _
            FieldInfo:=Array(Array(0, 2), Array(13, 2), Array(20, 2), Array(31, 2)), _
            TrailingMinusNumbers:=True
    myEnd:
End Sub

下面是条形码的示例,然后是文本到列拆分的外观

FP10SS200011915113111022001131110240004 FP10ss2000  1191511 31110220011 31110240004
FP10D400000031256232508001662325120000  FP10D40000  0031256 23250800166 2325120000
FP10MDA-SS050000207496320374001463203745000 FP10MDA-SS05    0020749 63203740014 63203745000
FP10PCDSS050000005566801250501068012510006  FP10PCDSS0500   0000556 68012505010 68012510006

再次感谢你们。 马丁

如果您实施我的评论,您的代码将看起来像这样。

Dim SelectedRange as Range
Dim Cell as Range

Range("B2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Set SelectedRange = Application.Selection
        For each Cell in SelectedRange
            If len(Cell) = 39 Then   'length of first barcode Then
                Cell.TextToColumns Destination:=Range(Cell), DataType:=xlFixedWidth, _
                FieldInfo:=Array(Array(0, 2), Array(10, 2), Array(17, 2), Array(28, 2)), _

                TrailingMinusNumbers:=True

            ElseIf len(Cell) = ?? Then'length second barcode Then
                Cell.TextToColumns Destination:=Range(Cell), DataType:=xlFixedWidth, _
                FieldInfo:=Array(   'Enter array here

                TrailingMinusNumbers:=True

            ElseIf len(Cell) = ?? Then'length third barcode Then
                Cell.TextToColumns Destination:=Range(Cell), DataType:=xlFixedWidth, _
                FieldInfo:=Array(   'Enter array here

                TrailingMinusNumbers:=True
            End If
         Next Cell
    myEnd:
    End Sub

暂无
暂无

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

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