简体   繁体   English

需要与Excel Value函数等效的真实VBA

[英]Need a real VBA equivalent for Excel Value function

As mentioned in the title, I need a VBA equivalent to the Excel Value function. 如标题中所述,我需要一个等效于Excel Value函数的VBA。 My data set looks like this: Data set example 我的数据集如下所示: 数据集示例

What I am looking for is VBA code equivalent to this: =Value(A2)+Value(B2) . 我正在寻找的是与此等效的VBA代码: =Value(A2)+Value(B2) That would go in column C 那将在C列中

The output must be the same as that function. 输出必须与该函数相同。 For the given example, column C should end up looking like this: End product 对于给定的示例,列C的最终外观应如下所示: 最终产品

More than that, it needs to only have the value in the cell after the macro is run, rather than displaying the value and still having that formula in it. 不仅如此,它只需要在运行宏后在单元格中具有值,而不是显示该值并仍在其中包含该公式。

Here is what I have done so far: 到目前为止,这是我所做的:

For i = 1 To LastRow
   strValue = Val(sht.Range("A" & i))
   strValue1 = Val(sht.Range("B" & i))
   sht.Range("C" & i).Value = strValue + strValue1
Next i

I also tried variations on this, a couple of which are shown below: 我还尝试了对此的变体,如下所示:

For i = 1 To LastRow
   strValue = Evaluate(sht.Range("A" & i))
   strValue1 = Evaluate(sht.Range("B" & i))
   sht.Range("C" & i).Value = strValue + strValue1
Next i

For i = 1 To LastRow
   strValue = sht.Range("A" & i)
   strValue1 = sht.Range("B" & i)
   strVal = Evaluate(strValue)
   strVal1 = Evaluate(strValue1)
   sht.Range("C" & i).Value = strVal + strVal1
Next i

I can't find anything that will work for me. 我找不到任何适合我的东西。 The output in C for the example set ends up being just 9. Pretty sure it is taking the first number in A and adding it to the first number in B. So when the hour in B changes to 1 C displays 10. 对于示例集,C中的输出最终仅为9。请确保它是将A中的第一个数字添加到B中的第一个数字。因此,当B中的小时更改为1 C时,将显示10。

I also tried simply: 我也简单地尝试过:

For i=1 To LastRow
   sht.Range("C" & i).Value = sht.Range("A" & i).Value + sht.Range("B" & i).Value
Next i

That just concatenated the text to the format 9/03/15 00:00:00 只是将文本连接为格式9/03/15 00:00:00

Any and all help appreciated. 任何和所有帮助表示赞赏。 Bonus if you can point me in the right direction for changing the final C values from that number (ie. 42250.00017) to the custom date/time format 'yyyy-mm-dd hh:mm:ss' . 如果您能指出正确的方向,以将最终的C值从该数字(即42250.00017)更改为自定义日期/时间格式'yyyy-mm-dd hh:mm:ss' ,那么将有好处。

Edit: Here is my code up to the sticking point. 编辑:这是我的代码到症结所在。 Everything else works as I want it to, the only problem is with the last For loop. 其他所有东西都按我的意愿工作,唯一的问题是最后一个For循环。

Sub sbOrganizeData()

Dim i As Long
Dim k As Long
Dim sht As Worksheet
Dim LastRow As Long
Dim sFound As String
Dim rng As Range
Dim sheet As Worksheet
Dim Sheet2 As Worksheet
Dim strFile As String
Dim strCSV As String
Dim strValue As Double
Dim strValue1 As Double
Dim strVal As Long
Dim strVal1 As Long

Application.DisplayAlerts = False
Sheets("all016").Delete
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
Set sheet = Sheets.Add
Set Sheet2 = Sheets.Add
sheet.Name = "all016"
Sheet2.Name = "Sheet1"

strFile = ActiveWorkbook.Path
strCSV = "*.csv"
sFound = Dir(strFile & "\*.csv")
If sFound <> "" Then
    Workbooks.Open Filename:=strFile & "\" & sFound
End If
Range("A1").CurrentRegion.Copy Destination:=Workbooks("solar.xlsm").Sheets("all016").Range("A1")
Workbooks(sFound).Close

Set sht = ThisWorkbook.Sheets("all016")
LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

sht.Range("C1").EntireColumn.Insert

For i = 1 To LastRow
   'Code that doesn't quite work here'

   sht.Range("C" & i).NumberFormat = "yyyy-mm-dd hh:mm:ss"
Next i

The issue is that the dates and times are strings so something like this will work: 问题在于日期和时间是字符串,因此类似的东西可以工作:

For i = 2 To LastRow
   strValue = Evaluate("VALUE(TRIM(" & sht.Range("A" & i).Address(1,1,,1) & "))")
   strValue1 = Evaluate("VALUE(TRIM(" & sht.Range("B" & i).Address(1,1,,1) & "))")
   sht.Range("C" & i).Value = strValue + strValue1
   'the format
   sht.Range("C" & i).NumberFormat = "mm/dd/yy hh:mm:ss"
Next i

You have to reference the .Value2 field of the range element as: 您必须将range元素的.Value2字段引用为:

For i = 1 To LastRow
   sht.Range("C" & i).Value2 = sht.Range("A" & i).Value2 + sht.Range("B" & i).Value2
Next i

The value is free of formatting and just in Excel's time/date code as you want your final result to be. 该值不受格式限制,并且只需要您希望最终结果为Excel的时间/日期代码即可。 Cheers, 干杯,

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

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