如何:我用的是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' –