Script VBA per formattare condizionatamente un testo specifico in una cella concatenata

StackOverflow https://stackoverflow.com//questions/22060605

  •  23-12-2019
  •  | 
  •  

Domanda

Sono nuovo per eccellere e provare a scrivere uno script VBA per formattare condizionatamente alcune cellule.

Ho la colonna A Ogni singola cella sembra questa.

.

Nome del progetto: nome uno Ao: nome due
Bo: nome tre
CO: nome quattro
Fai: Nome Five

Un dettaglio extra è che non tutte le cellule hanno tutte e cinque le righe. Ad esempio una cella potrebbe avere solo:

.

AO: nome due
Bo: nome tre

Quello che devo fare è formatterlo in modo che il nome del progetto :, ao :,, bo :,, co:, e fare: sono tutti in grassetto mentre il nome è in grassetto, in corsivo e colorato. Non posso dire come aggiungere colore qui ma sembrerebbe qualcosa del genere.

.

Nome del progetto: Nome uno (con colore)

AO: Nome Due
Bo: Nome tre
CO: Nome quattro
Fai: Nome Five

Mi sto chiedendo se c'è un modo per creare uno script VBA per farlo automaticamente? La mia soluzione alternativa negli ultimi giorni è stata selezionata individualmente il testo in ogni cella e applicando la formattazione lì, ed è stato inferno!

Versione Excel: 2010

È stato utile?

Soluzione

modificato

Questo paio di subroutine farà il lavoro.La formattazione può essere applicata a una gamma di celle contemporaneamente.

' Make a keyboard shortcut for this macro:
Sub FormatActiveCells()
    Dim c As Range
    For Each c In Selection.Cells
        FormatCell c
    Next
End Sub

' This subroutine does the work
Sub FormatCell(c As Range)
    Dim pos1 As Integer, pos2 As Integer
    ' Determine if line 1 is project name
    pos1 = InStr(1, c.Text, "Project Name:")
    If pos1 > 0 Then
        ' Make "Project Name:" bold
        c.Characters(pos1, Len("Project Name:")).Font.FontStyle = "Bold"
        ' Advance past colon character
        pos1 = pos1 + Len("Project Name:")
        ' Find end-of-line character
        pos2 = InStr(pos1, c.Text, Chr(10))
        ' Make text between "Project Name:" and end-line italicized and colored
        c.Characters(pos1, pos2 - pos1).Font.FontStyle = "Bold Italic"
        c.Characters(pos1, pos2 - pos1).Font.Color = RGB(0, 0, 255)
        ' Point both positions to one character past end-of-line
        pos2 = pos2 + 1
        pos1 = pos2
    Else
        ' Point both positions to first character
        pos1 = 1
        pos2 = 1
    End If

    ' Format additional lines
    Do
        ' Find colon character
        pos2 = InStr(pos1, c.Text, ":")
        ' If not found, we're done
        If pos2 = 0 Then Exit Do
        ' Make text from start of line to colon bold
        c.Characters(Start:=pos1, Length:=pos2 - pos1).Font.FontStyle = "Bold"
        ' Find end-of-line
        pos2 = InStr(pos2 + 1, c.Text, Chr(10))
        ' If not found, we're done
        If pos2 = 0 Then Exit Do
        ' Point both positions to one character past end-of-line
        pos2 = pos2 + 1
        pos1 = pos2
        DoEvents
    Loop
End Sub
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top