excel - Excel VBA 中不必要的值连接和类型错误
问题描述
我的 VBA 代码从特定软件输出(Carlson Survey 软件)中获取一个 .txt 文件并进行一些计算,然后将其转换为 .CSV 文件。我特别遇到了计算组件的问题,其中我的文本文件的一列(使用逗号分隔符带入 excel)没有进行我告诉它的计算,并且似乎正在连接自身(删除小数点后的所有内容) . 我的假设是,因为我将这些值放入一个设置为字符串的数组(必须设置为字符串,否则我会遇到类型错误),这会导致小数点后的连接。我不知道为什么计算似乎没有运行,因为程序似乎执行得很好。
以及用于快速参考的 VBA 脚本(有问题的特定部分是“进行数据转换”部分:
Private Sub Workbook_Open()
Sheets("Sheet1").Cells.ClearContents
'---------------------------------------------------------------------------------------
'Choose and open the .TXT file for conversion
Dim answer As Integer
answer = MsgBox("Do you want to process a .TXT file for use in InfoSWMM?", vbYesNo + vbQuestion, "Select .TXT File")
If answer = vbNo Then
Exit Sub
End If
Dim Ret
Ret = Application.GetOpenFilename("Text Files (*.txt),*.txt")
If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$A$1") _
)
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
'---------------------------------------------------------------------------------------
'Do data conversion, SECTION NEEDS UPDATING LACKING FEATURES, BUGS
Dim row As Integer
Dim col As Integer
Dim i As Integer
Dim tester(3) As String 'Bug[1] related, type error (see below). String type fixes type error, but causes undesired concatenation
Dim col_test As Integer
Dim rim As Integer
For row = 1 To ActiveSheet.UsedRange.Rows.Count
If IsEmpty(ActiveSheet.Cells(row, 1).Value) = True Then
Exit For
End If
'Change these values in case feature code library is changed in Carlson, also need to add extra fields
If ActiveSheet.Cells(row, 5).Value = "SD" Or ActiveSheet.Cells(row, 5).Value = "WQ" Then
col_test = 20
rim = ActiveSheet.Cells(row, 4).Value
For i = 0 To 3
tester(i) = ActiveSheet.Cells(row, col_test).Value 'Bug[1] here, type error if not a String.
col_test = col_test + 4
Next i
ActiveSheet.Cells(row, 37).Value = rim - Application.Max(tester) 'Bug[2] here, not performing calculation.
End If
Next row
'---------------------------------------------------------------------------------------
'Save converted file as .CSV
MsgBox "Choose the desired save location for the .CSV file."
Dim InitialName As String
Dim PathName As Variant
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
InitialName = "sfm_output"
PathName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="CSV (Comma delimited) (*.csv), *.csv")
ws.Copy
ActiveWorkbook.SaveAs Filename:=PathName, _
FileFormat:=xlCSV, CreateBackup:=False
MsgBox "Process completed successfully." & vbNewLine & "File saved to:" & vbNewLine & PathName
'---------------------------------------------------------------------------------------
'Close all Workbooks
Application.DisplayAlerts = False
Application.Quit
End Sub
任何帮助是极大的赞赏。谢谢。
解决方案
您是否尝试过 CSTRING 或 CINT 函数?
例如:tester(i) = CString(ActiveSheet.Cells(row, col_test).Value)
推荐阅读
- heroku - 用 CC 验证了我的免费帐户,仍然无法使用 sendgrid。不支持电话号码
- javascript - Javascript优化浏览器重排
- nginx - nginx如何重定向所有分页
- python - 将连续变量分箱到给定数量的类中
- python - 在我在 bash 中运行的 python 电子邮件脚本中,在尝试运行它之后,它给了我这个“'email'不是一个包”错误。有任何想法吗?
- amazon-s3 - 想要从 .gitlab-ci.yml 文件中针对两个单独的分支运行两个单独的管道,以部署到两个单独的 aws 帐户中
- cmake - CMake configure_file 和 Debug/Release 目录
- java - 如何在没有OOM的情况下编写字节数组?
- javascript - 连接到 Heroku Postgres - 服务器不支持 SSL 连接
- c# - 如何解析显示外部进程(C#)状态的第三方控制台应用程序?