检查单元格中数据的长度并添加一个零
Check the length of data in a cell and add a zero
我有一个包含以下格式数据的电子表格:
Col1 Col2
ROW1: 21211 Customer 3873721
ROW2: 101111 Customer 2321422
ROW3: 91214 Customer 2834712
ROW4: 231014 Customer 3729123
我需要能够创建一个宏来遍历每一行并确定构成第 1 行数据的字符数。
例如:
If data contained in the first cell or ROW1 consisted of a total of 6
characters then this will remain the same. If it consisted of 5
characters then a zero needs to be added to the front of it.
我正在使用 Excel 2003.
在这种情况下不需要 VBA。一个简单的 IF Statement
应该可以解决问题。假设您要评估的列是 A 列,将其放在 B 列中并向下复制:
=+IF(LEN(A1)=5,"0" & A1,A1)
只要出现在您的示例数据中的所有值都是 5 或 6 个字符,这将有效。
OP 似乎对以下自定义格式感到满意:
#000000
这很容易应用,但主要可能是个好主意,因为大多数其他前置 0
的方法只能通过将数值转换为字符串来实现(否则 Excel 会自动去除前导零)。如果这是有选择地完成的(长度为 5 而不是长度 6),一列看似数字的内容最终可能会变成混合类型(具有不同的行为),因此会造成混淆或不便。
我有一个包含以下格式数据的电子表格:
Col1 Col2
ROW1: 21211 Customer 3873721
ROW2: 101111 Customer 2321422
ROW3: 91214 Customer 2834712
ROW4: 231014 Customer 3729123
我需要能够创建一个宏来遍历每一行并确定构成第 1 行数据的字符数。
例如:
If data contained in the first cell or ROW1 consisted of a total of 6 characters then this will remain the same. If it consisted of 5 characters then a zero needs to be added to the front of it.
我正在使用 Excel 2003.
VBA。一个简单的 IF Statement
应该可以解决问题。假设您要评估的列是 A 列,将其放在 B 列中并向下复制:
=+IF(LEN(A1)=5,"0" & A1,A1)
只要出现在您的示例数据中的所有值都是 5 或 6 个字符,这将有效。
OP 似乎对以下自定义格式感到满意:
#000000
这很容易应用,但主要可能是个好主意,因为大多数其他前置 0
的方法只能通过将数值转换为字符串来实现(否则 Excel 会自动去除前导零)。如果这是有选择地完成的(长度为 5 而不是长度 6),一列看似数字的内容最终可能会变成混合类型(具有不同的行为),因此会造成混淆或不便。