尝试发送范围的单元格以在VBA Excel 2007中运行并返回一个string

很新的VBA编程,所以任何帮助表示赞赏。 我认为我卡住的部分,至less错误总是出现在我的函数调用,当我试图创build一个单元格范围发送到一个函数,将值变成一个csvstring。 这里的最终目标是组装一个string发送到shell命令来执行。 我收到的错误是在这一行types不匹配:

File_Index = Range2Csv(“E20:”&N_small_files)

这是我的代码:

Option Explicit Sub TDMS_Click() Dim Big_File As String Dim Small_File As String Dim File_Index As String Dim N_small_files As String Dim File_Duration As Integer Dim TDMS_exe As String Dim EXE_command As String TDMS_exe = "blah/blah blah/blah.exe" N_small_files = ActiveCell.Address(RowAbsolute:=False, ColumnAbsolute:=True) File_Index = Range2Csv("E20:" & N_small_files) Big_File = Worksheets("Sheet1").Cells(6, 3) Small_File = Worksheets("Sheet1").Cells(9, 3) File_Duration = Worksheets("Sheet1").Cells(12, 3) EXE_command = TDMS_exe & " -- " & File_Duration & Big_File & Small_File & """" & File_Index & """" Range("H40").Value = EXE_command End Sub '********************************************** '* PURPOSE: Concatenates range contents into a '* delimited text string '* '* FUNCTION SIGNATURE: Range2Csv(Range, String) '* '* PARAMETERS: '* Range - the range of cells whose contents '* will be included in the CSV result '* String - delimiter used to separate values '* (Optional, defaults to a comma) '* '* AUTHOR: www.dullsharpness.com '* '********************************************** Public Function Range2Csv(inputRange As Range, Optional delimiter As String) As String Dim concattedList As String 'holder for the concatted CSVs Dim rangeCell As Range 'holder cell used in For-Each loop Dim rangeText As String 'holder for rangeCell's text 'default to a comma delimiter if none is provided If delimiter = "" Then delimiter = "," concattedList = "" 'start with an empty string 'Loop through each cell in the range to append valid contents For Each rangeCell In inputRange.Cells rangeText = rangeCell.Value 'capture the working value 'Only operate on non-blank cells (ie Length > 0) If Len(rangeText) > 0 Then 'Strip any delimiters contained w/in the value itself rangeText = WorksheetFunction.Substitute(rangeText, delimiter, "") If (Len(concattedList) > 0) Then 'prepend a delimiter to the new value if we 'already have some list items concattedList = concattedList + delimiter + rangeText Else 'else if the list is blank so far, 'just set the first value concattedList = rangeText End If End If Next rangeCell 'Set the return value Range2Csv = concattedList End Function 

感谢您的期待

蒂姆

这是你正在尝试传递一个string值而不是范围引用的问题。

File_Index = Range2Csv(“E20:”&N_small_files)

这是修复

File_Index = Range2Csv(Range(“E20:”&N_small_files))

 Sub TDMS_Click() Dim Big_File As String Dim Small_File As String Dim File_Index As String Dim N_small_files As String Dim File_Duration As Integer Dim TDMS_exe As String Dim EXE_command As String TDMS_exe = "blah/blah blah/blah.exe" N_small_files = ActiveCell.Address(RowAbsolute:=False, ColumnAbsolute:=True) File_Index = Range2Csv(Range("E20:" & N_small_files)) Big_File = Worksheets("Sheet1").Cells(6, 3) Small_File = Worksheets("Sheet1").Cells(9, 3) File_Duration = Worksheets("Sheet1").Cells(12, 3) EXE_command = TDMS_exe & " -- " & File_Duration & Big_File & Small_File & """" & File_Index & """" Range("H40").Value = EXE_command End Sub Public Function Range2Csv(inputRange As Range, Optional delimiter As String = ",") As String Dim s As String Dim c As Range For Each c In inputRange s = s & c.Value & delimiter Next Range2Csv = Left(s, Len(s) - Len(delimiter)) End Function