如何:我用的是ColorIndex
財產由1
遞增起設置單元格的顏色:
Dim dict As New Scripting.Dictionary
Sub ReadTextFile()
Dim fso As FileSystemObject, filePath As String, data As Variant, colorIndex As Integer
filePath = "C:\Users\Alex\Desktop\input.txt" //Change this
Set fso = New FileSystemObject
Set txtStream = fso.OpenTextFile(filePath, ForReading, False)
colorIndex = 1
Do While Not txtStream.AtEndOfStream
inputLine = txtStream.ReadLine
data = Split(inputLine, vbTab)
With Worksheets("Sheet1")
.Cells(CInt(data(0)), CInt(data(1))) = data(2)
.Cells(CInt(data(0)), CInt(data(1))).Interior.colorIndex = GetColor(CStr(data(2)), colorIndex)
End With
colorIndex = colorIndex + 1
Loop
txtStream.Close
End Sub
Function GetColor(label As String, colorIndex As Integer)
If Not dict.Exists(label) Then
dict.Add label, colorIndex
GetColor = colorIndex
Exit Function
Else
GetColor = dict(label)
End If
End Function
我沒有做過的唯一位添加圖例,但我相信你可以遍歷字典並寫到工作表上的任何你想要的地方。
可以顯示'.txt'文件的結構嗎? –
請看上面,謝謝。 – user429400
詞典在VBA中可用。早期綁定到'Microsoft Scripting Runtime'('scrrun.dll')或延遲綁定到'Scripting.Dictionary' –