簡體   English   中英

VBA合並單元格和第二個單元格的“粗體”文本

[英]VBA merge cells and **Bold** text of second cell

我正在編碼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(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

此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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM