VBA合并单元格和第二个单元格的**粗体**文本
我正在编写一个VBA函数来合并两个单元格,然后用粗体格式突出显示单元格2的文本 合并进展顺利VBA合并单元格和第二个单元格的**粗体**文本,vba,excel,substring,text-formatting,Vba,Excel,Substring,Text Formatting,我正在编写一个VBA函数来合并两个单元格,然后用粗体格式突出显示单元格2的文本 合并进展顺利 呼叫Sub进展顺利 但不应用文本格式 我相信这可能是由于在单元格填充字符串之前执行子脚本造成的-但这纯粹是猜测-这是我的第一个VBA脚本 Function boldIt(navn As String, ekstra As String) Dim ln1 As Integer Dim ln2 As Integer Dim st1 As String ln1 = Len(navn) ln2 = Len(
呼叫Sub进展顺利
但不应用文本格式 我相信这可能是由于在单元格填充字符串之前执行子脚本造成的-但这纯粹是猜测-这是我的第一个VBA脚本
Function boldIt(navn As String, ekstra As String)
Dim ln1 As Integer
Dim ln2 As Integer
Dim st1 As String
ln1 = Len(navn)
ln2 = Len(navn) + Len(ekstra)
If (ln1 = ln2) Then
boldIt = navn
Else
boldIt = navn & " - " & ekstra
boldTxt ln1, ln2
End If
End Function
Public Sub boldTxt(startPos As Integer, charCount As Integer)
With ActiveCell.Characters(Start:=startPos, Length:=charCount).Font
.FontStyle = "Bold"
End With
End Sub
我想我只需要运行两个潜艇;函数没有返回任何内容
Option Explicit
Sub boldIt()
Dim secondOne As String
With Selection
secondOne = .Cells(2).Value2
Application.DisplayAlerts = False
.Merge
Application.DisplayAlerts = True
.Cells(1) = .Cells(1).Value2 & secondOne
boldTxt .Cells(1), Len(.Cells(1).Value2) - Len(secondOne) + 1, Len(secondOne)
End With
End Sub
Public Sub boldTxt(rng As Range, startPos As Integer, charCount As Integer)
With rng.Characters(Start:=startPos, Length:=charCount).Font
.FontStyle = "Bold"
End With
End Sub
该子单元循环遍历列,获取两个单元格的字符串,组合字符串并将其添加到目标单元格,同时粗体显示第二个单元格的文本 感谢@Jeeped的指点
Sub boldIt()
Dim pos_bold As Integer
Dim celltxt As String
For i = 2 To 200000
' first cell will always be populated - if not - exit
If (Range("Plan!E" & i).Value = "") Then Exit For
' if second cell is empty - take only first cell as normal txt
If (Range("Plan!F" & i).Value = "") Then
Range("Kalender!F" & i).Value = Range("Plan!E" & i).Value
Else
' calculate start of bold text
pos_bold = Len(Range("Plan!E" & i).Value) + 1
' create the string
celltxt = Range("Plan!E" & i).Value & " - " & Range("Plan!F" & i).Value
' add string to field and add bold to last part
With Worksheets("Kalender").Range("F" & i)
.Value = celltxt
.Characters(pos_bold).Font.Bold = True
End With
End If
Next i
End Sub
该函数工作正常,字段按它应该的方式填充-只是它不加粗文本(尝试了没有Sub的多表版本)。我不相信您理解函数没有返回任何内容。函数应该返回一个值;如果函数只执行操作而不返回任何内容,那么使用函数代替子函数是没有意义的。在这中间,我使用了两个sub。你所做的工作:-)但是,我想保留原始单元格,并将数据添加到一个新的单元格中-在无法工作的整个列中循环。选择性格式只能用于存储在单元格中的实际文本字符串。在函数完成之前,单元格中没有文本字符串。完成后,单元格中有一个公式(返回文本字符串),而不是纯文本字符串。