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)
 
I thought I did that other than assign it to a button on that sheet. Should I not do that?
You can - just replace Set ShList = Me with Set ShList = Sheets("ImportExport")
 
Last edited:
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
But my suggestion in the post #7 does not require any button, code will be calculated autonatically after selection of ImportExport sheet, read this in the post #3 - "Change sheet selection to see the result."
 
Upvote 0
Ok so misunderstanding I didn't see that comment. Grid becomes an inventory of data. sheet 2 or importsheet is used to just update the grid. Used to remove those X's under groups or add for new ones. but never modify others I don't define in that import sheet. Problem is that actual data is 200+ accounts and over 500 groups. so a mechanism is what i am looking for.

I tried to run the button it clears the importsheet but didnt add any X's in the grid
 
Last edited:
Upvote 0
I tried to run the button it clears the importsheet but didnt add any X's in the grid
The posted code was only about import data from the Grid sheet to ImportExport sheet, as the 1st part of 2 tasks was - "Looking for a way in another tab to just list group #, Account #". May be it was my misunderstanding :)

Ok, let's begin one more time.
Value "A" in ImportExport sheet is for setting Account-Group cell in the Grid sheet.
But what value is for removing X's? May be it's empty value in A-column, please confirm.
 
Upvote 0
Don't use any previous code and try this one:
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

  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(4).Value
    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(, 3).Value
  For i = 2 To UBound(a)
    Grp = a(i, 2) ' group
    Acc = a(i, 3) ' account
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not DicGrp.Exists(Grp) Then
        ShImpExp.Cells(i, 2).Select
        MsgBox "Group '" & Grp & "' not found in the Grig", vbExclamation, "Exit"
        Exit Sub
      End If
      If Not DicAcc.Exists(Acc) Then
        ShImpExp.Cells(i, 3).Select
        MsgBox "Account '" & Acc & "' not found in the Grig", vbExclamation, "Exit"
        Exit Sub
      End If
      ' Update the intersected Account-Group value in the Data()
      Data(DicAcc.Item(Acc), DicGrp.Item(Grp)) = IIf(UCase(Trim(a(i, 1))) = "A", "X", "")
    End If
  Next

  ' Update Grig
  Rng.Value = Data()

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

End Sub
 
Last edited:
Upvote 0
Question in Grid where does it look for the account column A or column D. should be column A but couldnt decipher, i shouldve changed column D name to avoid confusion :oops:

I put a 1 and says Account '1' not found in grid
 
Upvote 0
According to your post #5 the Account# values are like XX1, XX2 and so on.
Such values are in D-column of Grid according to your post #1.
There is no value 1 in the sheet ImportExport in your posted examples.
 
Last edited:
Upvote 0
I put a 1 and says Account '1' not found in grid
Why do you put value 1 into ImportExport sheet, instead of value "XX1"?
There is no 1 at all in your examples of ImportExport data, please be more specific in the posted examples.
 
Last edited:
Upvote 0
If accounts should be get from column A of the Grig then replace this code line:
a() = Rng.EntireRow.Columns(4).Value
by that one:
a() = Rng.EntireRow.Columns(1).Value
 
Upvote 0
I tried XX1 as well and it just cleared but didnt add the X's. so i changed as you described above. and it is doing the same clearing but not updating the Xs in the grid. Am i doing something wrong?
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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