|
楼主 |
发表于 2019-10-3 20:27
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 weiyingde 于 2019-10-3 21:54 编辑
带参数的过程,相当于函数。可以反复调用。
Public Sub 输出(sht As Worksheet, r1 As Integer, c1 As Integer, Num As Integer, Optional istr As String)
'作用:将指定区域的数据,合并为字符串(参数istr为“zf”时),或写作为数组形式(参数istr为“sz”时),便于写进VBA代码之中。
'参数说明:
' sht:为指定工作表
' r1:为指定列数据的第一个数据所在的行数;
' c1:为数据所在列的列数。
' Num:为生成字符每行的个数
' istr:为生成的字符串形式。“zf"时,为VBA代码中的字符形式;"sz"为VBA代码中的数组形式。
Dim arr() as string, r As Integer
With sht
For r = .Cells(65536, c1).End(3).Row To 1 Step -1
If Len(.Cells(r, c1)) >= 20 Then .Cells(r, c1).ClearContents
Next
rw = .Cells(65536, c1).End(3).Row
Select Case istr
Case "zf"
For i = 1 To Int((rw - r1 + 1) / Num) + 1
For j = (i - 1) * Num + r1 To Num * i + r1 - 1
isr = isr & IIf(j <= rw, Cells(j, c1), "") & IIf(j < rw, ",", "")
Next
n = n + 1
ReDim Preserve arr(1 To n)
isr = IIf(i = 1, "isr = ", "") & Chr(34) & isr & Chr(34) & IIf(n <> Int((rw - r1 + 1) / Num) + 1, " & _", "")
arr(n) = isr
isr = ""
Next
.Cells(rw + 1, c1).Resize(UBound(arr), 1) = Application.WorksheetFunction.Transpose(arr)
Case "sz"
For i = 1 To Int((rw - r1 + 1) / Num) + 1
For j = (i - 1) * Num + r1 To Num * i + r1 - 1
isr = isr & IIf(j <= rw, Chr(34), "") & Cells(j, c1) & IIf(j <= rw, Chr(34), "") & IIf(j < rw, ",", "")
Next
n = n + 1
ReDim Preserve arr(1 To n)
isr = IIf(i = 1, "arr=Array(", "") & isr & IIf(n <> Int((rw - r1 + 1) / Num) + 1, " _", ")")
arr(n) = isr
isr = ""
Next
.Cells(rw + 1, c1).Resize(UBound(arr), 1) = Application.WorksheetFunction.Transpose(arr)
End Select
End With
End Sub
Sub 调用1()
Call 输出(ActiveSheet, 2, 2, 30, "zf")
End Sub
Sub 调用2()
Call 输出(ActiveSheet, 2, 2, 30, "sz")
End Sub
|
|