Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将单元格范围发送到VBA Excel 2007中的函数并返回字符串_Vba_Excel - Fatal编程技术网

尝试将单元格范围发送到VBA Excel 2007中的函数并返回字符串

尝试将单元格范围发送到VBA Excel 2007中的函数并返回字符串,vba,excel,Vba,Excel,非常新的VBA编程,所以任何帮助是感激的。我想我遇到的问题,至少在我的函数调用中总是会出现错误,是当我试图创建一系列单元格发送给一个函数,将值转换为csv字符串时。这里的最终目标是组装一个字符串,发送给shell命令执行。我收到的错误是此行的类型不匹配: 文件索引=范围2CSV(“E20:&N小文件”) 这是我的密码: Option Explicit Sub TDMS_Click() Dim Big_File As String Dim Small_File As String Dim Fil

非常新的VBA编程,所以任何帮助是感激的。我想我遇到的问题,至少在我的函数调用中总是会出现错误,是当我试图创建一系列单元格发送给一个函数,将值转换为csv字符串时。这里的最终目标是组装一个字符串,发送给shell命令执行。我收到的错误是此行的类型不匹配:

文件索引=范围2CSV(“E20:&N小文件”)

这是我的密码:

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 (i.e. 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
谢谢你的关注


Tim

这里是您的问题,您试图传入字符串值而不是范围引用

文件索引=范围2CSV(“E20:&N小文件”)

这是解决办法

文件索引=范围2CSV(范围(“E20:&N小文件))


这是您试图传入字符串值而不是范围引用时遇到的问题

文件索引=范围2CSV(“E20:&N小文件”)

这是解决办法

文件索引=范围2CSV(范围(“E20:&N小文件))


完美的谢谢,太好了!非常感谢。
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