简体   繁体   English

VBA Excel 根据字符串长度(字符数)定义单元格值

[英]VBA Excel defining cell value based on the length of the string (number of characters)

I would like to have my coordinates sorted.我想对我的坐标进行排序。 Unfortunately, some of them feature longer strings, hence it's harder to get around them as you see below.不幸的是,它们中的一些具有更长的字符串,因此如下所示,很难绕过它们。

在此处输入图像描述

The highlighted coordinates are wrong, unfortunately.不幸的是,突出显示的坐标是错误的。 The error is the derivative of a longer string.错误是较长字符串的导数。

I was trying to find a way to base my code on the string length condition:我试图找到一种将我的代码基于字符串长度条件的方法:

https://www.mrexcel.com/board/threads/vba-macro-to-count-characters-in-a-cell.1056822/ https://www.mrexcel.com/board/threads/vba-macro-to-count-characters-in-a-cell.1056822/

https://www.exceldome.com/solutions/count-number-of-characters-in-a-cell/ https://www.exceldome.com/solutions/count-number-of-characters-in-a-cell/

https://www.automateexcel.com/vba/calculate-number-of-words-in-string/ https://www.automateexcel.com/vba/calculate-number-of-words-in-string/

https://www.excelfunctions.net/vba-len-function.html https://www.excelfunctions.net/vba-len-function.html

and my code snippet looks as you can see below我的代码片段如下所示

 With wors
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
SecondLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
 End With

 For Each element In wors.Range("E2:E" & LastRow)
   If Len(IsNumeric(element.Value)) < 10 Then
    element.Value = element.Value / 1000000
 Else
 element.Value = element.Value / 10000000
 End If
 Next

 For Each element In wors.Range("F2:F" & LastRow)
 If IsNumeric(element.Value) Then
    element.Value = element.Value / 1000000
 End If
 Next

I am getting an error: Variable required - can't assign to this expression我收到一个错误:需要变量 - 无法分配给此表达式

Compile Error - Variable Required-can't assign to this expression 编译错误 - 需要变量 - 无法分配给此表达式

How can I define the final cell value based on the total number of characters in the initial string?如何根据初始字符串中的字符总数定义最终单元格值?

If Len(IsNumeric(element.Value)) < 10 Then is the error You are trying to take the Len of a boolean which is why the error is thrown. If Len(IsNumeric(element.Value)) < 10 Then is the error 您正在尝试获取booleanLen ,这就是引发错误的原因。

The following should achieve what you want I think:以下应该达到你想要的我的想法:

 For Each element In wors.Range("E2:E" & LastRow)
   If IsNumeric(element.Value) Then
     If Len(element.Value) < 10 Then
       element.Value = element.Value / 1000000
     Else
       element.Value = element.Value / 10000000
     End If
   End If
 Next

You will also need to do something similar for column F because those appear to have the same length-of-data issue您还需要对column F执行类似的操作,因为它们似乎具有相同的数据长度问题

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

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