VBA和Ascii艺术

5

我正在尝试编写由Ascii Art形成的文本。
例如"Hi"。
这对我来说很难,所以我在这里寻求你的帮助。
下面是我目前所做的:


Option Explicit
' I tried with a Type.
Private Type LetterH
    H1 As String
    H2 As String
    H3 As String
    H4 As String
    H5 As String
    H6 As String
    H7 As String
End Type

Sub BuildAsciiWrite(strTxt As String)

Dim H As LetterH
    ' Fill the Type for H letter.
    H.H1 = "HHH    HHH"
    H.H2 = "HHH    HHH"
    H.H3 = "HHH    HHH"
    H.H4 = "HHHHHHHHHH"
    H.H5 = "HHH    HHH"
    H.H6 = "HHH    HHH"
    H.H7 = "HHH    HHH"

' Then I tried with Arrays:

Dim LtH(1 To 7) As String
    ' Fill the Array for H letter.
    LtH(1) = "HHH    HHH"
    LtH(2) = "HHH    HHH"
    LtH(3) = "HHH    HHH"
    LtH(4) = "HHHHHHHHHH"
    LtH(5) = "HHH    HHH"
    LtH(6) = "HHH    HHH"
    LtH(7) = "HHH    HHH"

Dim LtI(1 To 7) As String
    ' Fill the Array for I letter.
    LtI(1) = "IIIIIIIIIII"
    LtI(2) = "    III    "
    LtI(3) = "    III    "
    LtI(4) = "    III    "
    LtI(5) = "    III    "
    LtI(6) = "    III    "
    LtI(7) = "IIIIIIIIIII"

    ' All strTxt UPPERCASE.
    strTxt = UCase(strTxt)

' Array strArrayTxt contains strTxt one letter for one of the text.
Dim strArrayTxt() As String
    ' Redim Array for the lenght of strTxt.
    ReDim strArrayTxt(1 To Len(strTxt))
' Loop all letters of strTxt.
Dim intLoop1 As Integer
    For intLoop1 = 1 To Len(strTxt)
        ' Fill Array with letters of strTxt.
        strArrayTxt(intLoop1) = Mid$(strTxt, intLoop1, 1)
    ' Next letter.
    Next intLoop1
    ' Empty Var.
    intLoop1 = 0

' Var for the complete text we'll create.
Dim strWrite As String
' Another Array for all 26 letters of the alphabeth.
Dim Letters() As String
ReDim Letters(1 To 26)
    For intLoop1 = 1 To 26
        Letters(intLoop1) = Chr$(64 + intLoop1)
    Next intLoop1

' At this point I got:
' Type LetterH (an Array) with all the 7 strings that I can retrieve with H1, H2 and so on.
' Array LtH (1 To 7) with all the 7 strings building the "H" in Ascii.
' Array LtI (1 To 7) with all the 7 strings building the "I" in Ascii.
' Array strArrayTxt(1 To Len(strTxt)) with all the letters that form my choose word.
' Array Letters(1 To 26) with all the 26 letters of the alphabeth.

' Then I tried:
Dim intLoop2 as Integer    
    For intLoop2 = 1 To intLunghScritta
        For intLoop1 = 1 To 26
            If strArrayTesto(intLoop2) = Letters(intLoop1) Then
                ' This give me error.
                'strWrite = strArrayTesto(intLoop2).strArrayTesto(intLoop2) & intLoop1

                ' I can write in Immediate when find in Array Letters() the same letter find in
                ' Array strArrayTxt().
                Debug.Print strArrayTxt(intLoop2) & " = " & Letters(intLoop1)
            End If
        Next intLoop1
    Next intLoop2


End Sub
' Test SUB.
Sub Test_BuildAsciiWrite()
Dim strTxt As String
    strTxt = "Hi"
    BuildAsciiWrite (strTxt)
End Sub

我不知道怎么连接字符串,因为如果我从第一个字母开始形成单词“HI”,我可以在For...Next循环中找到“H”,我可以提取第一个字母“H”,但我该如何说VBA遍历所有数组并带来所谓的LetterH?
没有办法获得带有Letter& [letter find]的数组名称。

编辑。

感谢@Spencer Barnes,我解决了我的问题。
这就是我所做的,希望将来能为某人服务。
对不起,我只是从我的模块中复制和粘贴,所以所有的变量、常数、注释和其他文本都是意大利语(翻译过于困难和漫长),但是VBA是可以的,我可以构建我的 Ascii-Art 文本。


Option Explicit
Option Private Module

' La Costante contiene uno Spazio di testo.
Public Const Spazio As String = " "

' La Costante contiene i caratteri iniziali di linea (solo "'+").
Public Const CaratteriIniziali As String = "'+"

' La Costante contiene i caratteri finali di linea (solo "+vbCrLf").
Public Const CaratteriFinali = "+" & vbCrLf



Sub Prova_CreaScrittaAscii()
Dim strTesto As String
    strTesto = "Ciao"
    Call CreaScrittaAscii(strTesto, True)
End Sub



Sub CreaScrittaAscii(strTesto As String, Optional ByVal bolCommentoExcel As Boolean = True)

' Gestione errore.
On Error GoTo GesErr

' L'Array viene caricato coi valori delle lettere Ascii-Art.
Dim Lettere(1 To 26, 1 To 7) As String
' Stringa passata dalla MsgBox.
Dim strMsg As String
' La stringa contiene la prima e l'ultima riga del testo.
Dim strPU As String
' La stringa contiene la riga vuota.
Dim strV As String
' La Var conterrà il testo completo della scritta che si verrà a creare.
Dim strScritta As String
' La Var servirà per il primo ciclo nell'Array.
Dim intCiclo1 As Integer
' La Var servirà per il secondo ciclo nell'Array.
Dim intCiclo2 As Integer
' La Var servirà per trovare la posizione della lettera nell'alfabeto.
Dim lngNumeroLettera As Long
' La Var conterrà la stringa che si viene a formare riga per riga.
Dim strCostruisciRiga As String
' L'Array conterrà, divisa per righe, il testo già formattato in Ascii-Art.
Dim CostruisciRiga(1 To 7) As String

' Carico l'Array per la Lettera A.
Lettere(1, 1) = "    AAA    "
Lettere(1, 2) = "  AAA AAA  "
Lettere(1, 3) = " AAA   AAA "
Lettere(1, 4) = "AAAAAAAAAAA"
Lettere(1, 5) = "AAA     AAA"
Lettere(1, 6) = "AAA     AAA"
Lettere(1, 7) = "AAA     AAA"

' Carico l'Array per la Lettera B.
Lettere(2, 1) = "BBBBBBBBB "
Lettere(2, 2) = "BBB    BBB"
Lettere(2, 3) = "BBB    BBB"
Lettere(2, 4) = "BBBBBBBBB "
Lettere(2, 5) = "BBB    BBB"
Lettere(2, 6) = "BBB    BBB"
Lettere(2, 7) = "BBBBBBBBB "

' Carico l'Array per la Lettera C.
Lettere(3, 1) = " CCCCCCCC "
Lettere(3, 2) = "CCC    CCC"
Lettere(3, 3) = "CCC       "
Lettere(3, 4) = "CCC       "
Lettere(3, 5) = "CCC       "
Lettere(3, 6) = "CCC    CCC"
Lettere(3, 7) = " CCCCCCCC "

' Carico l'Array per la Lettera D.
Lettere(4, 1) = "DDDDDDDDD "
Lettere(4, 2) = "DDD    DDD"
Lettere(4, 3) = "DDD    DDD"
Lettere(4, 4) = "DDD    DDD"
Lettere(4, 5) = "DDD    DDD"
Lettere(4, 6) = "DDD    DDD"
Lettere(4, 7) = "DDDDDDDDD "

' Carico l'Array per la Lettera E.
Lettere(5, 1) = "EEEEEEEEEE"
Lettere(5, 2) = "EEE"
Lettere(5, 3) = "EEE"
Lettere(5, 4) = "EEEEEEEE"
Lettere(5, 5) = "EEE"
Lettere(5, 6) = "EEE"
Lettere(5, 7) = "EEEEEEEEEE"

' Carico l'Array per la Lettera F.
Lettere(6, 1) = "FFFFFFFFFF"
Lettere(6, 2) = "FFF       "
Lettere(6, 3) = "FFF       "
Lettere(6, 4) = "FFFFFFFF  "
Lettere(6, 5) = "FFF       "
Lettere(6, 6) = "FFF       "
Lettere(6, 7) = "FFF       "

' Carico l'Array per la Lettera G.
Lettere(7, 1) = " GGGGGGGG "
Lettere(7, 2) = "GGG    GGG"
Lettere(7, 3) = "GGG       "
Lettere(7, 4) = "GGG       "
Lettere(7, 5) = "GGG   GGGG"
Lettere(7, 6) = "GGG    GGG"
Lettere(7, 7) = " GGGGGGGG "

' Carico l'Array per la Lettera H.
Lettere(8, 1) = "HHH    HHH"
Lettere(8, 2) = "HHH    HHH"
Lettere(8, 3) = "HHH    HHH"
Lettere(8, 4) = "HHHHHHHHHH"
Lettere(8, 5) = "HHH    HHH"
Lettere(8, 6) = "HHH    HHH"
Lettere(8, 7) = "HHH    HHH"

' Carico l'Array per la Lettera I.
Lettere(9, 1) = "IIIIIIIIIII"
Lettere(9, 2) = "    III    "
Lettere(9, 3) = "    III    "
Lettere(9, 4) = "    III    "
Lettere(9, 5) = "    III    "
Lettere(9, 6) = "    III    "
Lettere(9, 7) = "IIIIIIIIIII"

' Carico l'Array per la Lettera J.
Lettere(10, 1) = "JJJJJJJJJJJ"
Lettere(10, 2) = "    JJJ    "
Lettere(10, 3) = "    JJJ    "
Lettere(10, 4) = "    JJJ    "
Lettere(10, 5) = "    JJJ    "
Lettere(10, 6) = "JJJ JJJ    "
Lettere(10, 7) = " JJJJJ     "

' Carico l'Array per la Lettera K.
Lettere(11, 1) = "KKK    KKK"
Lettere(11, 2) = "KKK   KKK "
Lettere(11, 3) = "KKK  KKK  "
Lettere(11, 4) = "KKKKKKK   "
Lettere(11, 5) = "KKK  KKK  "
Lettere(11, 6) = "KKK   KKK "
Lettere(11, 7) = "KKK    KKK"

' Carico l'Array per la Lettera L.
Lettere(12, 1) = "LLL       "
Lettere(12, 2) = "LLL       "
Lettere(12, 3) = "LLL       "
Lettere(12, 4) = "LLL       "
Lettere(12, 5) = "LLL       "
Lettere(12, 6) = "LLL       "
Lettere(12, 7) = "LLLLLLLLLL"

' Carico l'Array per la Lettera M.
Lettere(13, 1) = "MMMM    MMMM "
Lettere(13, 2) = "MMMMMM MMMMMM"
Lettere(13, 3) = "MMM MMMMM MMM"
Lettere(13, 4) = "MMM  MMM  MMM"
Lettere(13, 5) = "MMM       MMM"
Lettere(13, 6) = "MMM       MMM"
Lettere(13, 7) = "MMM       MMM"

' Carico l'Array per la Lettera N.
Lettere(14, 1) = "NNNN    NNN"
Lettere(14, 2) = "NNNNN   NNN"
Lettere(14, 3) = "NNNNNN  NNN"
Lettere(14, 4) = "NNN NNN NNN"
Lettere(14, 5) = "NNN  NNNNNN"
Lettere(14, 6) = "NNN   NNNNN"
Lettere(14, 7) = "NNN    NNNN"

' Carico l'Array per la Lettera O.
Lettere(15, 1) = " OOOOOOOO "
Lettere(15, 2) = "OOO    OOO"
Lettere(15, 3) = "OOO    OOO"
Lettere(15, 4) = "OOO    OOO"
Lettere(15, 5) = "OOO    OOO"
Lettere(15, 6) = "OOO    OOO"
Lettere(15, 7) = " OOOOOOOO "

' Carico l'Array per la Lettera P.
Lettere(16, 1) = "PPPPPPPPP "
Lettere(16, 2) = "PPP    PPP"
Lettere(16, 3) = "PPP    PPP"
Lettere(16, 4) = "PPPPPPPPP "
Lettere(16, 5) = "PPP       "
Lettere(16, 6) = "PPP       "
Lettere(16, 7) = "PPP       "

' Carico l'Array per la Lettera Q.
Lettere(17, 1) = " QQQQQQQQ  "
Lettere(17, 2) = "QQQ    QQQ "
Lettere(17, 3) = "QQQ    QQQ "
Lettere(17, 4) = "QQQ    QQQ "
Lettere(17, 5) = "QQQ  Q QQQ "
Lettere(17, 6) = "QQQ   QQQ  "
Lettere(17, 7) = " QQQQQQ QQQ"

' Carico l'Array per la Lettera R.
Lettere(18, 1) = "RRRRRRRRR "
Lettere(18, 2) = "RRR    RRR"
Lettere(18, 3) = "RRR    RRR"
Lettere(18, 4) = "RRRRRRRRR "
Lettere(18, 5) = "RRR    RRR"
Lettere(18, 6) = "RRR    RRR"
Lettere(18, 7) = "RRR    RRR"

' Carico l'Array per la Lettera S.
Lettere(19, 1) = " SSSSSSSS "
Lettere(19, 2) = "SSS    SSS"
Lettere(19, 3) = "SSS       "
Lettere(19, 4) = "SSSSSSSSSS"
Lettere(19, 5) = "       SSS"
Lettere(19, 6) = "SSS    SSS"
Lettere(19, 7) = " SSSSSSSS "

' Carico l'Array per la Lettera T.
Lettere(20, 1) = "TTTTTTTTTTT"
Lettere(20, 2) = "    TTT    "
Lettere(20, 3) = "    TTT    "
Lettere(20, 4) = "    TTT    "
Lettere(20, 5) = "    TTT    "
Lettere(20, 6) = "    TTT    "
Lettere(20, 7) = "    TTT    "

' Carico l'Array per la Lettera U.
Lettere(21, 1) = "UUU    UUU"
Lettere(21, 2) = "UUU    UUU"
Lettere(21, 3) = "UUU    UUU"
Lettere(21, 4) = "UUU    UUU"
Lettere(21, 5) = "UUU    UUU"
Lettere(21, 6) = "UUU    UUU"
Lettere(21, 7) = " UUUUUUUU "

' Carico l'Array per la Lettera V.
Lettere(22, 1) = "VVV     VVV"
Lettere(22, 2) = "VVV     VVV"
Lettere(22, 3) = "VVV     VVV"
Lettere(22, 4) = "VVV     VVV"
Lettere(22, 5) = " VVV   VVV "
Lettere(22, 6) = "  VVVVVVV  "
Lettere(22, 7) = "    VVV    "

' Carico l'Array per la Lettera W.
Lettere(23, 1) = "WWW       WWW"
Lettere(23, 2) = "WWW       WWW"
Lettere(23, 3) = "WWW       WWW"
Lettere(23, 4) = "WWW  WWW  WWW"
Lettere(23, 5) = "WWW WWWWW WWW"
Lettere(23, 6) = " WWWWW WWWWW "
Lettere(23, 7) = "  WWW   WWW  "

' Carico l'Array per la Lettera X.
Lettere(24, 1) = "XXX    XXX"
Lettere(24, 2) = "XXX    XXX"
Lettere(24, 3) = " XXX  XXX "
Lettere(24, 4) = "  XXXXXX  "
Lettere(24, 5) = " XXX  XXX "
Lettere(24, 6) = "XXX    XXX"
Lettere(24, 7) = "XXX    XXX"

' Carico l'Array per la Lettera Y.
Lettere(25, 1) = "YYY   YYY"
Lettere(25, 2) = "YYY   YYY"
Lettere(25, 3) = " YYY YYY "
Lettere(25, 4) = "  YYYYY  "
Lettere(25, 5) = "   YYY   "
Lettere(25, 6) = "   YYY   "
Lettere(25, 7) = "   YYY   "

' Carico l'Array per la Lettera Z.
Lettere(26, 1) = "ZZZZZZZZZ"
Lettere(26, 2) = "     ZZZ "
Lettere(26, 3) = "    ZZZ  "
Lettere(26, 4) = "   ZZZ   "
Lettere(26, 5) = "  ZZZ    "
Lettere(26, 6) = " ZZZ     "
Lettere(26, 7) = "ZZZZZZZZZ"
    
    ' Se la Var strTesto contiene caratteri minuscoli, li converte tutti in maiuscoli.
    strTesto = UCase(strTesto)
    
    ' Se bolCommentoExcel è True, allora.
    If bolCommentoExcel = True Then
        ' Prima e ultima riga.
        strPU = "'" & StringaRipeti(98, "+") & CaratteriFinali
        ' Riga vuota.
        strV = "'+" & StringaRipeti(97, Spazio) & CaratteriFinali
        ' Prima riga (solo "+").
        strScritta = strScritta & strPU
        ' Riga vuota.
        strScritta = strScritta & strV
    End If
    
    ' Se bolCommentoExcel è True, allora.
    If bolCommentoExcel = True Then

        ' Ciclo per ognuna delle 7 righe del carattere Ascii-Art.
        For intCiclo1 = 1 To 7
            ' Ciclo per ogni lettera della strTesto.
            For intCiclo2 = 1 To Len(strTesto)
                ' Getting the 1-26 number
                lngNumeroLettera = InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(strTesto, intCiclo2, 1)))
                strCostruisciRiga = strCostruisciRiga & Lettere(lngNumeroLettera, intCiclo1) & Spazio
            ' Prossima lettera nella strTesto.
            Next intCiclo2
            ' L'Array viene riempito con la riga costruita in strCostruisciRiga.
            CostruisciRiga(intCiclo1) = strCostruisciRiga
            ' La Var viene svuotata.
            strCostruisciRiga = Empty
        Next intCiclo1
        
        ' Se la lunghezza della scritta che si verrà a creare è maggiore di 95, allora.
        If Len(CostruisciRiga(1)) > 95 Then
            ' Avvisa.
            strMsg = MsgBox("Il numero di spazi necessari a contenere la scritta:" & _
                    Chr(13) & Chr(10) & strTesto & _
                    Chr(13) & Chr(10) & "(" & Len(CostruisciRiga(1)) & " caratteri necessari)" & _
                    Chr(13) & Chr(10) & "è superiore ai 95 caratteri disponibili." & _
                    Chr(13) & Chr(10) & "Correggere. Esco.", _
                    vbCritical + vbOKOnly, "A T T E N Z I O N E !")
            ' Esce dalla Sub.
            GoTo Uscita
        End If
    
        ' Ciclo per ognuna delle 7 righe dell'Array CostruisciRiga.
        For intCiclo1 = 1 To 7
            ' Concateno i caratteri iniziali della riga.
            strScritta = strScritta & CaratteriIniziali
            ' Inserisce tanti spazi vuoti quanti sono la differenza tra 97 e la lunghezza della stringa
            ' nell'Array, diviso 2 (prende solo la parte fissa prima della eventuale virgola.
            strScritta = strScritta & StringaRipeti(Fix((97 - Len(CostruisciRiga(1))) / 2), Spazio)
            ' Aggiunge la riga in elaborazione nell'Array.
            strScritta = strScritta & CostruisciRiga(intCiclo1)
            ' Inserisce tanti spazi vuoti finali quanti sono la differenza tra 97,
            ' i caratteri vuoti iniziali e la lunghezza della stringa nell'Array.
            strScritta = strScritta & StringaRipeti((97 - (Fix((97 - Len(CostruisciRiga(1))) / 2)) - (Len(CostruisciRiga(1)))), Spazio)
            ' Concateno il carattere di fine linea.
            strScritta = strScritta & CaratteriFinali
        ' Riga successiva nell'Array.
        Next intCiclo1
    
        ' Penultima riga (vuota).
        strScritta = strScritta & strV
    
        ' Ultima riga (solo "+").
        strScritta = strScritta & strPU
    
    ElseIf bolCommentoExcel = False Then

        ' Ciclo per ognuna delle 7 righe del carattere Ascii-Art.
        For intCiclo1 = 1 To 7
            ' Ciclo per ogni lettera della strTesto.
            For intCiclo2 = 1 To Len(strTesto)
                ' Getting the 1-26 number
                lngNumeroLettera = InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(strTesto, intCiclo2, 1)))
                strCostruisciRiga = strCostruisciRiga & Lettere(lngNumeroLettera, intCiclo1) & Spazio
            ' Prossima lettera nella strTesto.
            Next intCiclo2
            ' L'Array viene riempito con la riga costruita in strCostruisciRiga.
            CostruisciRiga(intCiclo1) = strCostruisciRiga
            ' La Var viene svuotata.
            strCostruisciRiga = Empty
        Next intCiclo1
        
        ' Ciclo per ognuna delle 7 righe dell'Array CostruisciRiga.
        For intCiclo1 = 1 To 7
            ' Aggiunge la riga in elaborazione nell'Array.
            strScritta = strScritta & CostruisciRiga(intCiclo1)
            ' Concateno il carattere di fine linea.
            strScritta = strScritta & vbCrLf
        ' Riga successiva nell'Array.
        Next intCiclo1
    
    End If
    
    ' Chiama la Function ScriviFileTemp.
    ScriviFileTemp (strScritta)

' Esce dalla Sub, dopo aver svuotato la/e variabile/i.
Uscita: strTesto = Empty
        Erase Lettere
        strMsg = Empty
        strPU = Empty
        strV = Empty
        strScritta = Empty
        intCiclo1 = Empty
        intCiclo2 = Empty
        lngNumeroLettera = Empty
        strCostruisciRiga = Empty
        Erase CostruisciRiga
        Exit Sub
' Questa riga di uscita viene raggiunta in caso di errore.
GesErr: MsgBox "Errore nella Sub" & vbCrLf & _
        "'CreaScrittaAscii'" & vbCrLf & vbCrLf & _
        "Errore Numero: " & Err.Number & vbCrLf & _
        "Descrizione dell'errore:" & vbCrLf & _
        Err.Description, vbCritical, "C'è stato un errore!"
        Resume Uscita
' Fine della Sub.
End Sub





Public Function ScriviFileTemp(ByVal strTesto As String, _
                               Optional ByVal strPercorso As String, _
                               Optional ByVal strNomeFile As String, _
                               Optional strEstensione As String = "txt") _
                               As String

' Gestione errore.
On Error GoTo GesErr

' La Var conterrà il percorso e il nome del file.
Dim strPercorsoNomeFile As String
' La Var conterrà il numero del file che stiamo andando a creare.
Dim intNumFile As Integer
    
    ' Se la Var passata alla Funzione, contenente il nome del file, è vuota, allora.
    If strNomeFile = "" Then
        ' Crea il nome del file. L'estensione se non è passata dalla Var, viene usata quella di default.
        strNomeFile = Format(Date, "ddmmmyyyy") & "_" & Format(Time, "hhmmss") & "." & strEstensione
    End If
    ' Se la Var passata alla Funzione, contenente il percorso del file, è vuota, allora.
    If strPercorso = "" Then
        ' Crea il percorso alla cartella temporanea.
        strPercorso = Environ("TMP") & Application.PathSeparator
    End If
    ' Poi concatena le due stringe per ottenere il file.
    strPercorsoNomeFile = strPercorso & strNomeFile
    
    ' Il numero del file temporareo è il prossimo numero disponibile.
    intNumFile = FreeFile()
    Open strPercorsoNomeFile For Output As intNumFile
    Print #intNumFile, strTesto;
    Close #intNumFile
    ' Apre il file creato con Notepad massimizzato.
    Shell "Notepad.exe " & strPercorsoNomeFile, vbMaximizedFocus
    ' La Funzione restituisce il percorso e il nome del file creato.
    ScriviFileTemp = strPercorsoNomeFile

' Esce dalla Funzione, dopo aver svuotato la/e variabile/i.
Uscita: strTesto = Empty
        strPercorso = Empty
        strNomeFile = Empty
        strEstensione = Empty
        strPercorsoNomeFile = Empty
        intNumFile = Empty
        Exit Function
' Questa riga di uscita viene raggiunta in caso di errore.
GesErr: MsgBox "Errore nella Function" & vbCrLf & "'ScriviFileTemp'" & vbCrLf & vbCrLf & "Errore Numero: " & Err.Number & vbCrLf & "Descrizione dell'errore:" & vbCrLf & Err.Description, vbCritical, "C'è stato un errore!"
        Resume Uscita
' Fine della Funzione.
End Function

非常感谢大家。


2
我会创建一个数组的数组(不规则数组)。主数组将有26个项目。内部数组将是每个字母。然后,您可以循环每个想要的单词中的字母,并按其字母位置调用每个项目,然后循环该结果数组。 - Scott Craner
请不要编辑您的帖子以展示您的解决方案,而是将其作为自我回答发布。 - undefined
3个回答

4
这是一种解决方法。
你需要一个构建字符串的函数.. (如果你想用字母构成字母,取消注释Replace行,如果你想用#构成字母,保持原样)
Function asciitext(txt As String)
    Dim output(1 To 7), rw As Long, pos As Long, segment As String, charac As String
    For rw = 1 To 7
        For pos = 1 To Len(txt)
            charac = Mid(txt, pos, 1)
            If charac = " " Then
                output(rw) = output(rw) & "        "
            Else
                segment = letter(Asc(charac) - 64, rw)
                'segment = Replace(segment, "#", charac)
                output(rw) = output(rw) & segment
            End If
        Next
        asciitext = asciitext & output(rw) & Chr(13)
    Next
End Function

你还需要一个过程来测试它.. (运行这个)

Sub output_to_immediate_window_test()
    Debug.Print asciitext("JUST A TEST")
End Sub

最后,您需要一个包含字母字符串的函数。
Function letter(lRow, lCode)
    Dim lString(1 To 26, 1 To 7)
    
    lString(1, 1) = "   ###    "
    lString(1, 2) = "  ## ##   "
    lString(1, 3) = " ##   ##  "
    lString(1, 4) = "##     ## "
    lString(1, 5) = "######### "
    lString(1, 6) = "##     ## "
    lString(1, 7) = "##     ## "
    
    lString(2, 1) = "########  "
    lString(2, 2) = "##     ## "
    lString(2, 3) = "##     ## "
    lString(2, 4) = "########  "
    lString(2, 5) = "##     ## "
    lString(2, 6) = "##     ## "
    lString(2, 7) = "########  "
    
    lString(3, 1) = " ######  "
    lString(3, 2) = "##    ## "
    lString(3, 3) = "##       "
    lString(3, 4) = "##       "
    lString(3, 5) = "##       "
    lString(3, 6) = "##    ## "
    lString(3, 7) = " ######  "
    
    lString(4, 1) = "########  "
    lString(4, 2) = "##     ## "
    lString(4, 3) = "##     ## "
    lString(4, 4) = "##     ## "
    lString(4, 5) = "##     ## "
    lString(4, 6) = "##     ## "
    lString(4, 7) = "########  "
    
    lString(5, 1) = "######## "
    lString(5, 2) = "##       "
    lString(5, 3) = "##       "
    lString(5, 4) = "######   "
    lString(5, 5) = "##       "
    lString(5, 6) = "##       "
    lString(5, 7) = "######## "
    
    lString(6, 1) = "######## "
    lString(6, 2) = "##       "
    lString(6, 3) = "##       "
    lString(6, 4) = "######   "
    lString(6, 5) = "##       "
    lString(6, 6) = "##       "
    lString(6, 7) = "##       "
    
    lString(7, 1) = " ######   "
    lString(7, 2) = "##    ##  "
    lString(7, 3) = "##        "
    lString(7, 4) = "##   #### "
    lString(7, 5) = "##    ##  "
    lString(7, 6) = "##    ##  "
    lString(7, 7) = " ######   "
    
    lString(8, 1) = "##     ## "
    lString(8, 2) = "##     ## "
    lString(8, 3) = "##     ## "
    lString(8, 4) = "######### "
    lString(8, 5) = "##     ## "
    lString(8, 6) = "##     ## "
    lString(8, 7) = "##     ## "
    
    lString(9, 1) = "#### "
    lString(9, 2) = " ##  "
    lString(9, 3) = " ##  "
    lString(9, 4) = " ##  "
    lString(9, 5) = " ##  "
    lString(9, 6) = " ##  "
    lString(9, 7) = "#### "
    
    lString(10, 1) = "      ## "
    lString(10, 2) = "      ## "
    lString(10, 3) = "      ## "
    lString(10, 4) = "      ## "
    lString(10, 5) = "##    ## "
    lString(10, 6) = "##    ## "
    lString(10, 7) = " ######  "
            
    lString(11, 1) = "##    ## "
    lString(11, 2) = "##   ##  "
    lString(11, 3) = "##  ##   "
    lString(11, 4) = "#####    "
    lString(11, 5) = "##  ##   "
    lString(11, 6) = "##   ##  "
    lString(11, 7) = "##    ## "
    
    lString(12, 1) = "##       "
    lString(12, 2) = "##       "
    lString(12, 3) = "##       "
    lString(12, 4) = "##       "
    lString(12, 5) = "##       "
    lString(12, 6) = "##       "
    lString(12, 7) = "######## "
    
    lString(13, 1) = "##     ## "
    lString(13, 2) = "###   ### "
    lString(13, 3) = "#### #### "
    lString(13, 4) = "## ### ## "
    lString(13, 5) = "##     ## "
    lString(13, 6) = "##     ## "
    lString(13, 7) = "##     ## "
    
    lString(14, 1) = "##    ## "
    lString(14, 2) = "###   ## "
    lString(14, 3) = "####  ## "
    lString(14, 4) = "## ## ## "
    lString(14, 5) = "##  #### "
    lString(14, 6) = "##   ### "
    lString(14, 7) = "##    ## "
            
    lString(15, 1) = " #######  "
    lString(15, 2) = "##     ## "
    lString(15, 3) = "##     ## "
    lString(15, 4) = "##     ## "
    lString(15, 5) = "##     ## "
    lString(15, 6) = "##     ## "
    lString(15, 7) = " #######  "
    
    lString(16, 1) = "########  "
    lString(16, 2) = "##     ## "
    lString(16, 3) = "##     ## "
    lString(16, 4) = "########  "
    lString(16, 5) = "##        "
    lString(16, 6) = "##        "
    lString(16, 7) = "##        "
    
    lString(17, 1) = " #######  "
    lString(17, 2) = "##     ## "
    lString(17, 3) = "##     ## "
    lString(17, 4) = "##     ## "
    lString(17, 5) = "##  ## ## "
    lString(17, 6) = "##    ##  "
    lString(17, 7) = " ##### ## "
    
    lString(18, 1) = "########  "
    lString(18, 2) = "##     ## "
    lString(18, 3) = "##     ## "
    lString(18, 4) = "########  "
    lString(18, 5) = "##   ##   "
    lString(18, 6) = "##    ##  "
    lString(18, 7) = "##     ## "
           
    lString(19, 1) = " ######  "
    lString(19, 2) = "##    ## "
    lString(19, 3) = "##       "
    lString(19, 4) = " ######  "
    lString(19, 5) = "      ## "
    lString(19, 6) = "##    ## "
    lString(19, 7) = " ######  "
    
    lString(20, 1) = "######## "
    lString(20, 2) = "   ##    "
    lString(20, 3) = "   ##    "
    lString(20, 4) = "   ##    "
    lString(20, 5) = "   ##    "
    lString(20, 6) = "   ##    "
    lString(20, 7) = "   ##    "
    
    lString(21, 1) = "##     ## "
    lString(21, 2) = "##     ## "
    lString(21, 3) = "##     ## "
    lString(21, 4) = "##     ## "
    lString(21, 5) = "##     ## "
    lString(21, 6) = "##     ## "
    lString(21, 7) = " #######  "
    
    lString(22, 1) = "##     ## "
    lString(22, 2) = "##     ## "
    lString(22, 3) = "##     ## "
    lString(22, 4) = "##     ## "
    lString(22, 5) = " ##   ##  "
    lString(22, 6) = "  ## ##   "
    lString(22, 7) = "   ###    "
            
    lString(23, 1) = "##      ##"
    lString(23, 2) = "##  ##  ##"
    lString(23, 3) = "##  ##  ##"
    lString(23, 4) = "##  ##  ##"
    lString(23, 5) = "##  ##  ##"
    lString(23, 6) = "##  ##  ##"
    lString(23, 7) = " ###  ### "
    
    lString(24, 1) = " ##     ##"
    lString(24, 2) = "  ##   ## "
    lString(24, 3) = "   ## ##  "
    lString(24, 4) = "    ###   "
    lString(24, 5) = "   ## ##  "
    lString(24, 6) = "  ##   ## "
    lString(24, 7) = " ##     ##"
    
    lString(25, 1) = " ##    ## "
    lString(25, 2) = "  ##  ##  "
    lString(25, 3) = "   ####   "
    lString(25, 4) = "    ##    "
    lString(25, 5) = "    ##    "
    lString(25, 6) = "    ##    "
    lString(25, 7) = "    ##    "
    
    lString(26, 1) = "######## "
    lString(26, 2) = "     ##  "
    lString(26, 3) = "    ##   "
    lString(26, 4) = "   ##    "
    lString(26, 5) = "  ##     "
    lString(26, 6) = " ##      "
    lString(26, 7) = "######## "
        
    letter = lString(lRow, lCode)

End Function

以下是输出结果:

      ## ##     ##  ######  ########            ###            ######## ########  ######  ########
      ## ##     ## ##    ##    ##              ## ##              ##    ##       ##    ##    ##
      ## ##     ## ##          ##             ##   ##             ##    ##       ##          ##
      ## ##     ##  ######     ##            ##     ##            ##    ######    ######     ##
##    ## ##     ##       ##    ##            #########            ##    ##             ##    ##
##    ## ##     ## ##    ##    ##            ##     ##            ##    ##       ##    ##    ##
 ######   #######   ######     ##            ##     ##            ##    ########  ######     ##

或者去掉注释:

      JJ UU     UU  SSSSSS  TTTTTTTT            AAA            TTTTTTTT EEEEEEEE  SSSSSS  TTTTTTTT
      JJ UU     UU SS    SS    TT              AA AA              TT    EE       SS    SS    TT
      JJ UU     UU SS          TT             AA   AA             TT    EE       SS          TT
      JJ UU     UU  SSSSSS     TT            AA     AA            TT    EEEEEE    SSSSSS     TT
JJ    JJ UU     UU       SS    TT            AAAAAAAAA            TT    EE             SS    TT
JJ    JJ UU     UU SS    SS    TT            AA     AA            TT    EE       SS    SS    TT
 JJJJJJ   UUUUUUU   SSSSSS     TT            AA     AA            TT    EEEEEEEE  SSSSSS     TT

2

你可以使用二维数组来实现这个功能。其中一个维度代表字母,另一个维度代表行数(因为一个字母可能由多行组成,就像上面的例子一样)。 例如:

Sub BuildAsciiWrite(strInput As String)
Dim Ascii(1 To 26, 1 To 7) As String

'Filling this array will take a lot of code, only showing H and I for demo purposes
'Ascii(8, x) is H, because H is the 8th letter
Ascii(8, 1) = "HHH    HHH  "
Ascii(8, 2) = "HHH    HHH  "
Ascii(8, 3) = "HHH    HHH  "
Ascii(8, 4) = "HHHHHHHHHH  "
Ascii(8, 5) = "HHH    HHH  "
Ascii(8, 6) = "HHH    HHH  "
Ascii(8, 7) = "HHH    HHH  "

'Ascii i, 9th letter
Ascii(9, 1) = "IIIIIIIIIII  "
Ascii(9, 2) = "    III      "
Ascii(9, 3) = "    III      "
Ascii(9, 4) = "    III      "
Ascii(9, 5) = "    III      "
Ascii(9, 6) = "    III      "
Ascii(9, 7) = "IIIIIIIIIII  "

'etc
'notice I added some space to keep letters a bit separate visually

'Now you need some loops to put together your output string
Dim strOutput As String, charNum As Long
For y = 1 To 7 'height
    For x = 1 To Len(strInput)
        'Getting the 1-26 number
        charNum = InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(strInput, x, 1)))
        'Alternatively you could use the Asc() function
            'and make your input array line up with ascii character codes
            'and so have both uppercase and lowercase, plus punctuation and things
            'depends how much effort you want to put into this ;)
        strOutput = strOutput & Ascii(charNum, y)
    Next
    strOutput = strOutput & Chr(13) 'new line
Next 'Height

Debug.Print strOutput
End Sub

Sub Test()
Dim MyInput As String
'MyInput = Inputbox("Input HI")
MyInput = "HI"

BuildAsciiWrite MyInput

End Sub

CLR比我更快,使用相同的方法但更好地分解了(尽管为每个输出字符重新声明letter数组可能是不必要的) - Spencer Barnes

0
Option Explicit
' with options for: BOLD, ITALICS, UNDERLINE, DOUBLE-HEIGHT
Type CHARACTER_MAP
   r As Variant   'array of character definition
   w As Integer   'width of character
End Type

Enum CHARACTER_OPTIONS
   lo_normal = 0
   lo_italics = 1
   lo_bold = 2    'setting this, is equal duplicating parameter symb
   lo_hx2 = 4     'double height
   lo_underline = 8
End Enum

Const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ! "  'array of simple characters to define in array alphabet
Const CHARACTERS_COUNT = 28
    
Dim alphabet(1 To CHARACTERS_COUNT) As CHARACTER_MAP  'array with data of all defined characters


Sub initAlphabet()   'DEFINITION character faces from top to bottom (7 lines)
   alphabet(1).r = Array(56, 108, 198, 387, 511, 387, 387): alphabet(1).w = 10      'A
   alphabet(2).r = Array(255, 387, 387, 255, 387, 387, 255): alphabet(2).w = 10     'B
   alphabet(3).r = Array(126, 195, 3, 3, 3, 195, 126): alphabet(3).w = 9            'C
   alphabet(4).r = Array(255, 387, 387, 387, 387, 387, 255): alphabet(4).w = 10     'D
   alphabet(5).r = Array(255, 3, 3, 63, 3, 3, 255): alphabet(5).w = 9               'E
   alphabet(6).r = Array(255, 3, 3, 63, 3, 3, 3): alphabet(6).w = 9                 'F
   alphabet(7).r = Array(126, 195, 3, 483, 195, 195, 126): alphabet(7).w = 10       'G
   alphabet(8).r = Array(387, 387, 387, 511, 387, 387, 387): alphabet(8).w = 10     'H
   alphabet(9).r = Array(15, 6, 6, 6, 6, 6, 15): alphabet(9).w = 5                  'I
   alphabet(10).r = Array(192, 192, 192, 192, 195, 195, 126): alphabet(10).w = 9    'J
   alphabet(11).r = Array(195, 99, 51, 31, 51, 99, 195): alphabet(11).w = 10        'K
   alphabet(12).r = Array(3, 3, 3, 3, 3, 3, 255): alphabet(12).w = 10               'L
   alphabet(13).r = Array(387, 455, 495, 443, 387, 387, 387): alphabet(13).w = 11   'M
   alphabet(14).r = Array(195, 199, 207, 219, 243, 227, 195): alphabet(14).w = 9    'N
   alphabet(15).r = Array(254, 387, 387, 387, 387, 387, 254): alphabet(15).w = 10   'O
   alphabet(16).r = Array(255, 387, 387, 255, 3, 3, 3): alphabet(16).w = 10         'P
   alphabet(17).r = Array(254, 387, 387, 387, 435, 195, 446): alphabet(17).w = 10   'Q
   alphabet(18).r = Array(255, 387, 387, 255, 99, 195, 387): alphabet(18).w = 10    'R
   alphabet(19).r = Array(126, 195, 3, 126, 192, 195, 126): alphabet(19).w = 9      'S
   alphabet(20).r = Array(255, 24, 24, 24, 24, 24, 24): alphabet(20).w = 9         'T
   alphabet(21).r = Array(387, 387, 387, 387, 387, 387, 254): alphabet(21).w = 10   'U
   alphabet(22).r = Array(387, 387, 387, 387, 198, 108, 56): alphabet(22).w = 10    'V
   alphabet(23).r = Array(771, 819, 819, 819, 819, 819, 462): alphabet(23).w = 10   'W
   alphabet(24).r = Array(774, 396, 216, 112, 216, 396, 774): alphabet(24).w = 10   'X
   alphabet(25).r = Array(390, 204, 120, 48, 48, 48, 48): alphabet(25).w = 10       'Y
   alphabet(26).r = Array(255, 96, 48, 24, 12, 6, 255): alphabet(26).w = 9          'Z
   alphabet(27).r = Array(6, 6, 6, 6, 6, 0, 6): alphabet(27).w = 4                  '!
   alphabet(28).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(28).w = 5                  'SPACE
'   TODO
'   alphabet(29).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(29).w = 8     '0
'   alphabet(30).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(30).w = 8     '1
'   alphabet(31).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(31).w = 8     '2
'   alphabet(32).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(32).w = 8     '3
'   alphabet(33).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(33).w = 8     '4
'   alphabet(34).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(34).w = 8     '5
'   alphabet(35).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(35).w = 8     '6
'   alphabet(36).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(36).w = 8     '7
'   alphabet(37).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(37).w = 8     '8
'   alphabet(38).r = Array(0, 0, 0, 0, 0, 0, 0): alphabet(38).w = 8     '9
   
End Sub


Public Function drawText(txt As String, Optional symb As String = "#", Optional options As CHARACTER_OPTIONS = lo_normal, Optional underlineChar = "=", Optional gap As String = "") As String
'txt > the text to draw
'Optional symb > think it as the pixel (unit of drawing) with any length
'Optional options > as Enum CHARACTER_OPTIONS
'Optional underlineChar > character for underline if option exist
'Optional gap > anything between the characters
   Dim c As Integer, p As Integer, ln As Integer, ww As Integer, hh As Integer, s As String, tmp As String
   Dim i() As Integer, maxh As Integer, isItl As Boolean, isBld As Boolean, isHx2 As Boolean, isUndrln As Boolean, symblen As Integer, totw As Integer
   
   ln = Len(txt)
   txt = UCase(txt)
   maxh = 6
   isItl = options And lo_italics
   isHx2 = options And lo_hx2
   isUndrln = options And lo_underline And underlineChar <> ""
   isBld = options And lo_bold
   If isBld Then symb = symb & symb
   symblen = Len(symb)
   
   If ln <= 0 Then Exit Function
   ReDim i(1 To ln)
   For c = 1 To ln
      i(c) = InStr(1, characters, Mid(txt, c, 1))
   Next
   For hh = 0 To maxh
      s = IIf(isItl, Space(maxh - hh), "")
      For c = 1 To ln
         With alphabet(i(c))
            For ww = 1 To .w
               s = s & IIf(.r(hh) And Application.WorksheetFunction.Bitlshift(1, ww - 1), symb, String(symblen, " "))
            Next
            If (gap <> "" and c < ln) Then s = s & gap
         End With
      Next
      drawText = drawText & s & IIf(isHx2, vbCrLf & s, "") & IIf(hh < maxh, vbCrLf, "")
   Next
   If isUndrln Then drawText = drawText & vbCrLf & String(Len(s), underlineChar)
End Function

Sub example()
   Dim s As String
   initAlphabet
   s = drawText("yes!", "//", lo_italics + lo_underline)
   Debug.Print s
End Sub

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接