VBA help to import/export list of data to populate a grid

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I couldn't find anything to uniquely accomplish what I am ultimately trying to do.
  1. I have a grid of data where column A7 down is my account #
  2. Starting at O6 to no end zz6 say for sake of argument where the accounts are grouped by a group #
  3. Currently I manually update the grid to place an X if the account is in the group #
    1. my file is going to grow exponentially so I can no longer manually support
Looking for a way in another tab to just list group #, Account #, then Add or remove then it will update the grid to clear X's if removed or add X if adding. For example below is a mocked up grid.

Book1
ABCDEFGHIJKLMNOPQRSTUVW
1Count102264102106
2PurposeTrackinglabelshipNeedsXXXXXXXXXXXXXXX
3OwnerTeam 1Team 1Team 1Team 1Team 1Team 1Team 1Team 1Team 1
4UniqueUniqueBasicUniqueBasicUniqueBasicUniqueBasicBasic
5AccountDesc 1Desc 2Account #NameFillerFillerFillerFillerFillerFillerFillerFillerFillerGroup 1Group 2Group 3Group 4Group 5Group 6Group 7Group 8Group 9
6100002000030000400005000060000700008000090000
71LargeL1XX1JNANANANANANANANANAXXXXXXX
82LargeL2XX2MNANANANANANANANANAXXXXXXX
93LargeL3XX3LNANANANANANANANANAXXXXXX
104LargeL4XX4ONANANANANANANANANAXXXXXX
115MediumL5XX5PNANANANANANANANANAXXXXX
126MediumL6XX6QNANANANANANANANANAXXXXX
137MediumL7XX7RNANANANANANANANANAXXXX
148SmallL8XX8SNANANANANANANANANAXXXX
159SmallL9XX9TNANANANANANANANANAXXXX
1610SmallL10XX10UNANANANANANANANANAXXXX
Grid
Cell Formulas
RangeFormula
O1:W1O1=COUNTA(O7:O10000)
 
Still same issue tried it on my smaller test subject. filter is set on row 6. and the filter criteria could change depending on user.
It does not help, please be more specific, as for the debugging I need to reproduce the problem on testing data of the post #1.

Use Grid data of the post #1, and VBA-code of the post #35 without any modifications.
Example of the tests description:
1. Filter in Grid does not hide rows (Filter criteria in A6 - Select All)
2. Data of ImportExport after macro running:
Wb3.xlsb
ABCD
1Add or RemoveGroup #Account #Result
2R100001Successfully removed
3A100001Successfully addeed
4A100001Already exists
ImportExport

3. Filter in Grid hides rows, except of rows 8,9,10 (Filter criteria in A6 - select Accounts 2,3,4)
4. Data of ImportExport after macro running:
Wb3.xlsb
ABCD
1Add or RemoveGroup #Account #Result
2R100001Successfully removed
3A100001Successfully addeed
4A100001Already exists
ImportExport

The results are the same.
Testing with Account # = 10 in the ImportExport gives the same results too.
Describe (like the above) your testing scenario in which results are not the same.
 
Last edited:
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
See below for a test for ease i put X's in every cell. filtered to just show me account 10. and want to remove X on 9 for 10000. As you can see marked as success but didn't change the X on #9 in last picture

1609971701185.png


1609971733276.png


1609971761126.png


1609971780745.png
 
Upvote 0
Thank you for the details, it helped.
And yes, ShGrid.ShowAllData have to be applied via the code, like this:
VBA Code:
Sub UpdateGrid()

  Const DataCell = "O7"
  Dim ShGrid As Worksheet, ShImpExp As Worksheet
  Dim Rng As Range
  Dim DicAcc As Object, DicGrp As Object
  Dim Acc As Variant, Grp As Variant
  Dim a() As Variant, Data() As Variant
  Dim i As Long
  Dim s As String, s1 As String

  Set ShGrid = Sheets("Grid")
  Set ShImpExp = Sheets("ImportExport")

  ' Create dictionaries for accounts and groups
  Set DicAcc = CreateObject("Scripting.Dictionary")
  Set DicGrp = CreateObject("Scripting.Dictionary")
  DicAcc.CompareMode = 1
  DicGrp.CompareMode = 1

  With ShGrid.Range("A1").CurrentRegion

    ' Copy Grid X's data to the Data()
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Data() = Rng.Value

    ' Put accounts and their rows to the dictionary
    a() = Rng.EntireRow.Columns(1).Value ' Replace 1 by 4 to use D-column in Grid instead of A-column
    For i = 1 To UBound(a)
      DicAcc.Item(a(i, 1)) = i
    Next

    ' Put groups and their columns to the dictionary
    a() = Rng.Offset(-1).Value
    For i = 1 To UBound(a, 2)
      DicGrp.Item(a(1, i)) = i
    Next

  End With

  ' Main
  a() = ShImpExp.UsedRange.Resize(, 4).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))  ' "A" to add or "R" to remove
    Grp = a(i, 2)             ' Group
    Acc = a(i, 3)             ' Account
    a(i, 4) = Empty           ' Result
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not (s = "A" Or s = "R") Then a(i, 4) = "Wrong 'A'/'R' code"
      If Not DicGrp.Exists(Grp) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Group doesn't exist"
      If Not DicAcc.Exists(Acc) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Account doesn't exist"
      ' Update the intersected Account-Group value in the Data()
      If s = "A" Then s = "X" Else s = ""
      If Len(a(i, 4)) = 0 Then
        s1 = UCase(Data(DicAcc.Item(Acc), DicGrp.Item(Grp)))
        If s = s1 Then
          If a(i, 1) = "A" Then
            a(i, 4) = "Already exists"
          ElseIf a(i, 1) = "R" Then
            a(i, 4) = "No X's exist to remove"
          End If
        Else
          Data(DicAcc.Item(Acc), DicGrp.Item(Grp)) = s
          a(i, 4) = "Successfully " & IIf(s = "X", "addeed", "removed")
        End If
      End If
    End If
  Next

  ' Update ImportExport
  ShImpExp.UsedRange.Resize(, 4).Value = a()
  
  ' Update Grig
  If ShGrid.FilterMode Then ShGrid.ShowAllData
  Rng.Value = Data()

  ' Release memory of the object variables
  Set DicAcc = Nothing
  Set DicGrp = Nothing

End Sub
 
Upvote 0
This code restores autofilter in Grid if it was applied:
VBA Code:
Sub UpdateGrid()

  Const DataCell = "O7", FreezeCell = "B7"
  Dim ShGrid As Worksheet, ShImpExp As Worksheet
  Dim Rng As Range
  Dim DicAcc As Object, DicGrp As Object
  Dim Acc As Variant, Grp As Variant
  Dim a() As Variant, Data() As Variant
  Dim i As Long
  Dim s As String, s1 As String

  Set ShGrid = Sheets("Grid")
  Set ShImpExp = Sheets("ImportExport")

  ' Create dictionaries for accounts and groups
  Set DicAcc = CreateObject("Scripting.Dictionary")
  Set DicGrp = CreateObject("Scripting.Dictionary")
  DicAcc.CompareMode = 1
  DicGrp.CompareMode = 1

  With ShGrid.Range("A1").CurrentRegion

    ' Copy Grid X's data to the Data()
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Data() = Rng.Value

    ' Put accounts and their rows to the dictionary
    a() = Rng.EntireRow.Columns(1).Value ' Replace 1 by 4 to use D-column in Grid instead of A-column
    For i = 1 To UBound(a)
      DicAcc.Item(a(i, 1)) = i
    Next

    ' Put groups and their columns to the dictionary
    a() = Rng.Offset(-1).Value
    For i = 1 To UBound(a, 2)
      DicGrp.Item(a(1, i)) = i
    Next

  End With

  ' Main
  a() = ShImpExp.UsedRange.Resize(, 4).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))  ' "A" to add or "R" to remove
    Grp = a(i, 2)             ' Group
    Acc = a(i, 3)             ' Account
    a(i, 4) = Empty           ' Result
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not (s = "A" Or s = "R") Then a(i, 4) = "Wrong 'A'/'R' code"
      If Not DicGrp.Exists(Grp) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Group doesn't exist"
      If Not DicAcc.Exists(Acc) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Account doesn't exist"
      ' Update the intersected Account-Group value in the Data()
      If s = "A" Then s = "X" Else s = ""
      If Len(a(i, 4)) = 0 Then
        s1 = UCase(Data(DicAcc.Item(Acc), DicGrp.Item(Grp)))
        If s = s1 Then
          If a(i, 1) = "A" Then
            a(i, 4) = "Already exists"
          ElseIf a(i, 1) = "R" Then
            a(i, 4) = "No X's exist to remove"
          End If
        Else
          Data(DicAcc.Item(Acc), DicGrp.Item(Grp)) = s
          a(i, 4) = "Successfully " & IIf(s = "X", "addeed", "removed")
        End If
      End If
    End If
  Next

  ' Update ImportExport
  ShImpExp.UsedRange.Resize(, 4).Value = a()
  
  ' Update Grig
  If ShGrid.FilterMode Then
    Application.ScreenUpdating = False
    With ThisWorkbook.CustomViews.Add("GridCustomViewName", False, True)
      ShGrid.ShowAllData
      Rng.Value = Data()
      .Show
      .Delete
    End With
    With ShGrid
      Set Rng = .Range(FreezeCell)
      If Rng.EntireRow.Hidden Then
        .Activate
        With Selection
          Rng.EntireRow.Hidden = False
          Rng.Select
          ActiveWindow.FreezePanes = False
          ActiveWindow.FreezePanes = True
          Rng.EntireRow.Hidden = True
          .Select
        End With
        ShImpExp.Activate
      End If
    End With
    Application.ScreenUpdating = True
  Else
    Rng.Value = Data()
  End If
  
  ' Release memory of the object variables
  Set DicAcc = Nothing
  Set DicGrp = Nothing

End Sub
 
Last edited:
Upvote 0
No, thanks is to you Vlad for doing all this. Looks good I will retest in the morning on my production data :)
 
Upvote 0
Hey Vlad --- so noticing something on my production data that is hard to replicate and occurs pretty often. at times it may say account, group or account and group doesnt exist. but if i just rerun the process it works.
 
Upvote 0
Hi,
Try this:
VBA Code:
Sub UpdateGrid()

  Const DataCell = "O7", FreezeCellRow = 7
  Dim ShGrid As Worksheet, ShImpExp As Worksheet
  Dim Rng As Range
  Dim DicAcc As Object, DicGrp As Object
  Dim Acc As String, Grp As String
  Dim a() As Variant, Data() As Variant
  Dim i As Long
  Dim s As String, s1 As String
  Dim IsHidden As Boolean

  Set ShGrid = Sheets("Grid")
  Set ShImpExp = Sheets("ImportExport")

  ' Create dictionaries for accounts and groups
  Set DicAcc = CreateObject("Scripting.Dictionary")
  Set DicGrp = CreateObject("Scripting.Dictionary")
  DicAcc.CompareMode = 1
  DicGrp.CompareMode = 1

  With ShGrid.Range("A1").CurrentRegion

    ' Copy Grid X's data to the Data()
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Data() = Rng.Value

    ' Put accounts and their rows to the dictionary
    a() = Rng.EntireRow.Columns(1).Value ' Replace 1 by 4 to use D-column in Grid instead of A-column
    For i = 1 To UBound(a)
      DicAcc.Item(Trim(a(i, 1))) = i
    Next

    ' Put groups and their columns to the dictionary
    a() = Rng.Offset(-1).Value
    For i = 1 To UBound(a, 2)
      DicGrp.Item(Trim(a(1, i))) = i
    Next

  End With

  ' Main
  a() = ShImpExp.UsedRange.Resize(, 4).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))  ' "A" to add or "R" to remove
    Grp = Trim(a(i, 2))       ' Group
    Acc = Trim(a(i, 3))       ' Account
    a(i, 4) = Empty           ' Result
    If Len(Grp) > 0 And Len(Acc) > 0 Then
      If Not (s = "A" Or s = "R") Then a(i, 4) = "Wrong 'A'/'R' code"
      If Not DicGrp.Exists(Grp) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Group doesn't exist"
      If Not DicAcc.Exists(Acc) Then a(i, 4) = IIf(Len(a(i, 4)) > 0, a(i, 4) & ", ", "") & "Account doesn't exist"
      ' Update the intersected Account-Group value in the Data()
      If s = "A" Then s = "X" Else s = ""
      If Len(a(i, 4)) = 0 Then
        s1 = UCase(Data(DicAcc.Item(Acc), DicGrp.Item(Grp)))
        If s = s1 Then
          If a(i, 1) = "A" Then
            a(i, 4) = "Already exists"
          ElseIf a(i, 1) = "R" Then
            a(i, 4) = "No X's exist to remove"
          End If
        Else
          Data(DicAcc.Item(Acc), DicGrp.Item(Grp)) = s
          a(i, 4) = "Successfully " & IIf(s = "X", "addeed", "removed")
        End If
      End If
    End If
  Next

  ' Update ImportExport
  ShImpExp.UsedRange.Resize(, 4).Value = a()
  
  ' Update Grig
  If ShGrid.FilterMode Then
    With ShGrid.Rows(FreezeCellRow).EntireRow
      IsHidden = .Hidden
      .Hidden = False
      With ThisWorkbook.CustomViews.Add("GridCustomViewName", False, True)
        ShGrid.ShowAllData
        Rng.Value = Data()
        .Show
        .Delete
      End With
      If IsHidden Then .Hidden = True
    End With
  Else
    Rng.Value = Data()
  End If
  
  ' Release memory of the object variables
  Set DicAcc = Nothing
  Set DicGrp = Nothing

End Sub
 
Upvote 0
i will restest on my real data. may not get back to you until next week of findings :)
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top