Я начал свое путешествие по VBA два месяца назад и столкнулся с проблемой, которую не могу решить.У меня есть таблица в определенном формате, которую я получил помощь, чтобы переформатировать с помощью словаря сценариев.
Я попытался добавить еще одну переменную, называемую временем, и разделил ее на "/". Еще одна проблема, с которой я столкнулся, заключается в том, что столбец времени имеет формат "чч: мм", что может быть переформатировано после цикла, хотя яПоверь.Это оригинальный код:
Dim lastrow As Long
Dim iter As Long
Dim diter As Long
Dim countrydict As Object
Dim country As String
Dim data As String
Dim key As Variant
Set countrydict = CreateObject("Scripting.Dictionary")
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).row
For iter = 1 To lastrow
country = Trim(.Cells(iter, 1).value)
data = Trim(.Cells(iter, 2).value)
If countrydict.Exists(country) Then
If Not InStr(1, countrydict(country), data) > 0 Then
' Remove Dupes
countrydict(country) = countrydict(country) & "|" & data
End If
Else
countrydict.Add country, data
End If
Next
iter = 1
For Each key In countrydict
.Cells(iter, 1).value = key & ":"
.cells(iter, 1).font.bold = True
.cells(iter, 1).font.colorindex = 30
iter = iter + 1
For diter = 0 To UBound(Split(countrydict(key), "|"))
.Cells(iter, 1).value = Split(countrydict(key), "|")(diter)
iter = iter + 1
Next
Next
.Columns("B").Clear
End With
Это преобразует мою таблицу из этого формата
"A" "B"
India Sales
France Sales
France Tax
Spain Sales
Spain Tax
В
India:
Sales
France:
Tax
Spain:
Sales
Tax
Это отлично работает, но я хочу знать, какчтобы добавить еще один столбец, поэтому, если у меня есть такая таблица
"A" "B" "C"
India Sales 12:00
France Sales 09:00
France Tax 11:00
Spain Sales 11:00
Spain Tax 05:00
Я хочу, чтобы она выглядела так
"A" "B"
India:
Sales 12:00
France:
Sales 09:00
Tax 11:00
Spain:
Sales 11:00
Tax 05:00
Я попытался добавить
dim diter2 as Long
For iter = 1 To lastrow
country = Trim(.Cells(iter, 1).value)
data = Trim(.Cells(iter, 2).value)
time = Trim(.Cells(iter, 3).value)
If countrydict.Exists(country) Then
If Not InStr(1, countrydict(country), data) > 0 Then
countrydict(country) = countrydict(country) & "|" & data & "/" & time
End If
Else
countrydict.Add country, data, time
End If
Next
iter = 1
For Each key In countrydict
.Cells(iter, 1).value = key & ":"
.cells(iter, 1).font.bold = True
.cells(iter, 1).font.colorindex = 30
iter = iter + 1
For diter = 0 To UBound(Split(countrydict(key), "|"))
.Cells(iter, 1).value = Split(countrydict(key), "|")(diter)
iter = iter + 1
For diter2 = 0 To UBound(Split(countrydict(key), "|"))
.Cells(iter, 2).value = Split(countrydict(key), "/")(diter2)
iter = iter + 1
Next
Next
Next
Большое спасибо за любую помощь