从B列粘贴到D列

时间:2016-08-31 10:11:04

标签: excel vba

我必须将项目代码从B列复制/粘贴到D列。

B列中的大多数细胞都是这样的:

  

XX787 DO BOLOGNESE 2X2.28KG FR

在这种情况下,商品代码是前5个字符。

B列中的一些单元格如下:

  

01333379大米长粒PB研磨10 BB

在这种情况下,项目代码是8个字符,由数字组成+第一个字符为零。

问题:

  • 当我从列B复制/粘贴到D列时,不显示开头的零。我尝试将所有单元格值粘贴为文本,以便显示零,但它没有帮助。
    我使用此代码格式化为文本:{ "array": { "Table1": [ { "h1": 0, "h2": 1069 }, { "h1": 587, "h2": 947 } ], "Table2": [ { "h1": 13, "h2": 0 }, { "h1": 30, "h2": 0 }, { "h1": 75, "h2": 0 } ] } }
  • 第一个字符放在B列,但如果有更多字符,则其余字符不会被放入。

2 个答案:

答案 0 :(得分:1)

这段代码对我来说很好,并没有删除前导零:

<target name="javadoc" description="create Javadoc documentation">
    <javadoc ...lots of irrelevant attributes skipped...>
        <fileset dir="src">
            <include name="**/*.java"/>
            <exclude name="my/jaxb/generated/source/*.java"/>
        </fileset>
        <classpath>
            <path refid="myclasspath_to_referenced_libraries"/>
            <pathelement location="bin"/>
        </classpath>
    </javadoc>
</target>

解释Dim i As Integer 'Set the numberformat of column D to text Range("D1").EntireColumn.NumberFormat = "@" 'Loop through the used rows For i = 1 To UsedRange.SpecialCells(xlCellTypeLastCell).Row Step 1 'assign the item code to column D Cells(i, "D").Value = Split(Cells(i, "B").Value, " ")(0) Next i 功能: Split()将指定的String(在本例中为Split())拆分为数组,您可以指定分隔符(在本例中为空格(Cells(i, "B").Value))。

由于returnvalue是一个数组,我们可以告诉VBA只返回第一个值,该值应该是项目代码。这是通过在行尾添加" "来完成的。

答案 1 :(得分:0)

在运行宏之前,只需将目标单元格格式更改为文本。如果你在运行宏之后这样做,那么零就不会存在。

相关问题