如何在函数中增加多个范围/单元格引用

时间:2015-05-11 08:31:06

标签: excel vba excel-vba

我正在开发一个能够在其中具有多个细胞内容的功能,所以我可以参考它,如下所示

csvRange(A2,C2:E2,G2)

输出单元格值,当在列中向下拖动时,将引用增加到下一行。我目前得到了这个:

Function csvRange(myRange As Range)
Dim csvRangeOutput

For Each entry In myRange
    csvRangeOutput = csvRangeOutput & entry.Value & Chr(34) & "," & Chr(34)
Next
csvRange = csvRangeOutput
End Function

这适用于一个范围但不是倍数

同样作为一个曲线球,只是为了我自己的应用程序,我需要在每个单元格之间添加它

Chr(34) & "," & Chr(34)

原因是csv应用程序,所以如果我在函数中有A1 = Test1和B1 = Test2,结果将是

Test1","Test2

我知道excel有一个导出功能,但它不能用于我的应用程序

1 个答案:

答案 0 :(得分:2)

这可能有助于您像csvRange("C2:F7;C5:D6")一样使用

Function csvRange(myRange As String) As String
Dim csvRangeOutput As String, _
    AbsAdd As String, _
    ColNum As Double, _
    A() As String
A = Split(myRange, ";")


For i = LBound(A) To UBound(A)
'    If InStr(1, A(i), ":") Then
        'Range is 1D or 2D
        For Each cel In Range(A(i))
            csvRangeOutput = csvRangeOutput & cel.Value & Chr(34) & "," & Chr(34)
        Next cel
'    Else
'        'Describe range is a single cell => we scan the whole column
'        AbsAdd = Range("E1").Address(True, True, xlR1C1)
'        ColNum = CDbl(Right(AbsAdd, Len(AbsAdd) - InStr(1, AbsAdd, "C")))
'        For k = 1 To ActiveSheet.Cells(Rows.Count, ColNum).End(xlUp).Row
'            If cel.Value<>vbNullString then csvRangeOutput = csvRangeOutput & cel.Value & Chr(34) & "," & Chr(34)
'        Next k
'    End If
Next i


csvRange = csvRangeOutput
End Function