我在可以提供工作表的地方编写了此函数,它应该将第一列从字符串转换为数值。但是当我从子程序中调用该函数时,它无法完成工作。
Function ConvertTextToNumber(Worksheet As Worksheet)
If (Worksheet.Name = "TB") Or (Worksheet.Name = "JE") Then
[A:A].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
ElseIf Worksheet.Name = "GLACCOUNT1100" Then
[D:D].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End If
End Function
然后我在此子区域内将其称为
Sub Select_Global_Account()
Dim length As Long, i As Long
Dim Start_range As Long, End_range As Long
' range input for the account number
Start_range = shStart.Range("P25").Value
End_range = shStart.Range("S25").Value
'================== converToNumber the acount column
Call helper.ConvertTextToNumber(shTB)
'===================
Call Iterate_In_Range(Start_range, End_range)
' JE report
Call JEReport.JEReport
' GL report
Call GL1100.AmountDate
其余代码工作正常,只有帮助程序。ConvertTextToNumber(shTB)不会将文本更改为字符串
答案 0 :(得分:0)
请尝试使用此代码。请注意,Value = Value
无法将字符串更改为数字。因此,下面的代码首先提取Value
的数值,然后将该新值分配给单元格。
Function ConvertTextToNumber(Ws As Worksheet)
' please don't use variable names that might
' confuse either you or Excel or both
Dim Clm As Long
Dim Rng As Range
Dim Cell As Range
With Ws
Select Case Ws.Name
Case "TB", "JE"
Clm = 1 ' column A
Case "GLACCOUNT1100"
Clm = 4 ' column D
Case Else
Exit Sub
End Select
' convert cell from row 2 to the last used cell
Set Rng = .Range(.Cells(2, Clm), .Cells(.Rows.Count, Clm).End(xlUp))
For Each Cell In Rng
.Value = Val(.Value)
.NumberFormat = "General"
Next Cell
End With
End Function
我添加了代码来指示如果作为参数传递的工作表不符合条件该怎么办。