Словарь сценариев для переформатирования таблицы Excel в формате времени - PullRequest
0 голосов
/ 04 июля 2019

Я начал свое путешествие по 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

Большое спасибо за любую помощь

1 Ответ

0 голосов
/ 05 июля 2019

Это должно быть ближе к тому, что вы хотите. Вы можете скопировать время как текст или как значение и соответствующим образом отформатировать принимающую ячейку.

   dim diter2 as Long, arr, arr2
    For iter = 1 To lastrow
        country = Trim(.Cells(iter, 1).value)
        data = Trim(.Cells(iter, 2).value)
        time = Trim(.Cells(iter, 3).Text) '<<<<<<
        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 '<<<edit
        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
        arr = Split(countrydict(key), "|")
        For diter = 0 To UBound(arr)
            arr2 = Split(arr(diter), "/") 
            .Cells(iter, 1).value = arr2(0)
            .Cells(iter, 2).value = arr2(1)      
        Next diter
    Next key
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...