所以我有几列的数字,因此可以说3例如
Column B Column C Column D
**520** 600 **550**
基于其他过程,可以说A列和C列是粗体的。我想将它们连接到一个单元格中,并保留大胆的格式,这样:
Column A
**520**,600,**550**
将通过可变数量的行重复,有些列有两个粗体,有些粗体,有些没有。
任何人都有一种简单的方法来遵循大胆的格式?我将字符串用于猫并写入新单元格,但我不知道如何将格式获取到新的串联单元格。以下代码仅是内部循环,我将2用于单行而不是用于简单的变量。
我的内部循环和大胆没有携带的东西:
Sub OneCell()
Dim i as Integer 'column number
Dim str1 as String 'String storage
i = 2 'initialize col num
str1 = Cells(2, i).value 'initialize str1
'Outer loop
Do
'....code for outer loop iterating rows
'Inner loop to concatenate values into one string
Do
str1 = str1 & "," & Cells(2,i+1).Value
i = i + 1
Loop until Cells(2,i+1).Value = ""
'Put concatenated string in cell(2,1)
Cells(2,1).Value = str1
Loop Until 'some condition of rows is met
End Sub
我只是在发布此信息,因为我开始了一段时间,然后被侧面进行了。
Sub OneCell()
Dim i As Integer 'column number
Dim str1 As String 'String storage
Dim v(1 To 100, 1 To 3) 'the 100 limit is arbitrary
Dim j As Long
i = 2 'initialize col num
str1 = Cells(2, i).Value 'initialize str1
Do
If Cells(2, i + 1).Font.Bold Then
j = j + 1
v(j, 1) = 1
v(j, 2) = Len(str1) + 2
v(j, 3) = Len(Cells(2, i + 1))
Else
j = j + 1
v(j, 1) = 0
End If
str1 = str1 & "," & Cells(2, i + 1).Value
i = i + 1
Loop Until Cells(2, i + 1).Value = ""
With Cells(2, 1)
.NumberFormat = "@"
.font.bold=false
.Value = str1
End With
For i = 1 To j
If v(i, 1) = 1 Then
Range("A2").Characters(v(i, 2), v(i, 3)).Font.Bold = True
End If
Next i
End Sub
这实际上比我想象的要棘手,但是我确实找到了一种使它起作用的方法(换句话说,我测试了代码)。可能有一种更有效的方法,但是此方法可在3个单元格选择中使用。您需要修改范围以适合您的程序。
Sub ConcatWithBold()
Dim str1 As String
'first build string and identify bolded cells with *
Dim c As Range
For Each c In Selection
If c.Font.Bold = True Then
str1 = str1 & "," & "*" & c.Value
Else
str1 = str1 & "," & c.Value
End If
Next
str1 = Mid(str1, 2) 'to remove first comma
Dim iCnt As Integer
For iCnt = 1 To Len(str1)
If Mid(str1, iCnt, 1) = "*" Then
'it's a bolded cell so make next 3 characters bold
With Selection.Cells(1, 1).Offset(1)
.Characters(iCnt, 3).Caption = Mid(str1, iCnt + 1, 3) 'set characters (add 1 to iCnt to skip asterik marking bold)
.Characters(iCnt - 1, 3).Font.Bold = True 'make bold (-1 to include first character)
iCnt = iCnt + 3 'jump to next comma
End With
Else
With Selection.Cells(1, 1).Offset(1)
.Characters(iCnt, 1).Caption = Mid(str1, iCnt, 1)
.Characters(iCnt - 1, 1).Font.Bold = False '(-1 to include character just set
End With
End If
Next
End Sub
,所以我的评论在您的答案下方出现了,@sjr。非常生病(我知道我不应该这样做,但现在似乎全是在起作用,我深表歉意)。我得到了一点调整。
开始时I初始化i到1(如果不这样做,则大胆移动)
然后在带有块语句之前添加回中期属性str1 = mid(str1,2)
我之所以这样做,是因为我发现第一个单元格是大胆的,它不会大胆地将第一组字符(但现在它)。现在,我遇到了另一个问题。我需要能够将整个内容设置为可变范围内的整个循环,因为这将在5-15次的情况下,具体取决于我的行数量。我尝试使用这些单元格属性作为范围参考适当。