0
對於VBA編程來說非常新穎,因此不勝感激任何幫助。我認爲我被卡住的部分,至少錯誤總是出現在我的函數調用中,當我試圖創建一個單元格範圍發送到一個函數,將值變成一個csv字符串。這裏的最終目標是組裝一個字符串發送到shell命令來執行。我收到的錯誤是在這一行類型不匹配:嘗試發送範圍的單元格以便在VBA Excel 2007中運行,並返回一個字符串
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 (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
完美!謝謝。 –