简体   繁体   English

无法从NUMERIC单元格获取STRING值,但单元格为“文本”

[英]Cannot get a STRING value from a NUMERIC cell, but cell is “text”

I am trying to store a numerical value (1, 2, or 3) in a variable after reading it from an excel table. 我试图从excel表中读取数值后,将数值(1、2或3)存储在变量中。 I am using apache poi. 我正在使用apache poi。

I don't think any code is necessary here, as it is a conceptual issue. 我认为这里不需要任何代码,因为这是一个概念性问题。

When I make the column of numbers into "number" format in the excel sheet and try to store it in an int or double it says "Cannot convert type String to type int" 当我在Excel工作表中将数字列转换为“数字”格式并尝试将其存储为intdouble它说“无法将String类型转换为int类型”

When I make the column of numbers into "text" format in the excel sheet and try to store it in a String variable, it says "Cannot get a STRING value from a NUMERIC cell" 当我在Excel工作表中将数字列变成“文本”格式并尝试将其存储在String变量中时,它说“无法从NUMERIC单元格获取STRING值”

How can I read and store these values in this conundrum? 如何在这些难题中读取和存储这些值?

If your range is not too big, you can loop through the entries: 如果范围不太大,则可以遍历以下条目:

Sub mac()

'put each cell value in range into an integer array
Dim rng As Range
Dim ints() As Integer
Set rng = Sheets(1).Range("A1:A4")

'doesn't work
'ints = rng.Value

'should work if there are no data problems
ReDim ints(1 To rng.Cells.Count)
For i = 1 To 4
     ints(i) = rng.Cells(i, 1).Value
Next i

End Sub

However, I suspect that you have an issue because at least one of your entries has a leading quote or some similar issue. 但是,我怀疑您有问题,因为至少有一个条目的前导报价或类似问题。 For example, a cell entry of "1" will not get read in as the integer 1. You will need some extra code to handle your specific data problem. 例如,单元格条目“ 1”将不会以整数1的形式读入。您将需要一些额外的代码来处理您的特定数据问题。

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

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