我有一个10 x 2
维的数据。数据如下所示-
year rate
05-06 10%
06-07 20.222%
07-08 13.17%
.
.
.
我想将此数据打印为图表对象上方存在的文本框中的字符串,并且第一列的格式必须为文本,第二列的格式必须被截断为带百分号的小数点后一位。我通过映射包含调用以下函数的公式的单元格,将数据串联为字符串,并将其粘贴到图表上的文本框对象。
字符串格式应如下所示-
05-06 : 10.0% ; 06-07 : 20.2% ; 07-08 : 13.2% ...
我已将此数据存储为命名范围rateCurrent
,并且已使用以下代码生成可见行的字符串。
= ConcatenateVisible(rateCurrent, ":", ";")
暂时,假设我粘贴了从第3列第8行开始的数据。
Function ConcatenateVisible(rng As Variant, seperator As String, separator1 As String)
For Each cll In rng
If cll.EntireRow.Hidden = False And rng.Column = 3 Then
Debug.Print rng.Row
ConcatenateVisible = ConcatenateVisible & Format(cll.Value, "@") & seperator
Debug.Print cll.Value
Else
Debug.Print rng.Row
ConcatenateVisible = ConcatenateVisible & Format(cll.Value, "0.0%") & seperator1
End If
Next
ConcatenateVisible = Left(ConcatenateVisible, Len(ConcatenateVisible) - Len(seperator))
End Function
由于某种原因,第二个循环无法正常工作,我收到如下所示的输出-
05-06 : 10.00000000000 : 06-07 : 20.2222222222 : 07-08 : 13.1765433333 ....
我也尝试了以下功能,当添加了if循环以中断格式时-
Public Function MakeList(ByVal myRange As Range) As String
On Error GoTo Errhand:
Dim c As Range
Dim MyDict As Object: Set MyDict = CreateObject("Scripting.Dictionary")
For i = 1 To myRange.Cells.Count
For Each c In myRange
If Not Rows(c.Row).Hidden Then
If Not MyDict.exists(c.Value2) Then MyDict.Add c.Value2, 1
End If
Next
Debug.Print c, MyDict.keys
If i Mod 2 = 0 Then
MakeList = Join(MyDict.keys, ": ")
Else
MakeList = Join(MyDict.keys, "; ")
End If
Next
cleanExit:
Set MyDict = Nothing
Set c = Nothing
Exit Function
Errhand:
Debug.Print Err.Number, Err.Description
GoTo cleanExit
End Function
任何提示,帮助或建议都将不胜感激。 TIA。
答案 0 :(得分:1)
尝试一下:
Option Explicit
Function concatenateVisible(rng As Range, Optional separator As String = " : ", _
Optional separator1 As String = " ; ") As String
Dim rw As Range
Dim str As String
str = ""
For Each rw In rng.Rows
If rw.Hidden = False And Len(rw.Cells(1, 1)) > 0 Then
str = str & separator1 & _
rw.Cells(1, 1) & separator & Format(rw.Cells(1, 2), "0.0%")
End If
Next rw
concatenateVisible = Mid(str, Len(separator1))
End Function