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)
 
The code just puts or deletes symbol "X" in the Grid table.

1. Test1, use these testing values in the ImportExport sheet:
Wb1.xlsb
ABC
1Add or RemoveGroup #Account #
210000XX1
ImportExport

Run the macro - value of Grid!O7 becomes empty.

2. Test2, testing values are as follows:
Wb1.xlsb
ABC
1Add or RemoveGroup #Account #
2A10000XX1
ImportExport

Run the macro - value of Grid!O7 becomes "X"

I have no idea what you expect in case the value in ImportExport!B2 or ImportExport!C2 is out of the existing values in column D or in the 5th row of the Grid.
Now the code just stops with a warning.
If you expect that code should extend size of the Grid table then how code knows what should be in the columns A:C, E:N of the inserted row for new Account#, and in rows 2:4 of the inserted column for new Group#?
 
Last edited:
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Yup your pics above clarified my issue I had "add" in the column A. but when doing A it works. should there be a warning message if I used the wrong one similiar to how you have the missing group or account?
 
Upvote 0
Yup your pics above clarified my issue I had "add" in the column A. but when doing A it works. should there be a warning message if I used the wrong one similiar to how you have the missing group or account?
Sure, "add" was not in your posted examples/
It's ok, this code accepts "A" or "Add" to set "X" into Grid:
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

  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 ' Replace 4 by 1 to use A-column in Grid instead of D-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(, 3).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))
    Grp = a(i, 2) ' group
    Acc = a(i, 3) ' account
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not (s = "A" Or s = "ADD" Or s = "") Then
        ShImpExp.Cells(i, 1).Select
        MsgBox "Wrong code - '" & s & "'" & vbLf & "Use 'A' or 'Add' to set 'X' in Grig," & vbLf & "Clear cell to remove 'X' in Grid", vbExclamation, "Exit"
        Exit Sub
      End If
      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(s = "A" Or s = "ADD", "X", "")
    End If
  Next

  ' Update Grig
  Rng.Value = Data()

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

End Sub
 
Upvote 0
I am actually ok with A its better and more streamlined. I was more asking if a message could be alerted if it DIDNT equal "A" or "R". to say only A or R is accepted. or something along those lines
 
Upvote 0
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

  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 ' Replace 4 by 1 to use A-column in Grid instead of D-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(, 3).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))
    Grp = a(i, 2) ' group
    Acc = a(i, 3) ' account
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not (s = "A" Or s = "R") Then
        ShImpExp.Cells(i, 1).Select
        MsgBox "Wrong code - '" & s & "'" & vbLf & "Use 'A' to add 'X' to Grig" & vbLf & "Use 'R' to remove 'X' from Grid", vbExclamation, "Exit"
        Exit Sub
      End If
      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(s = "A", "X", "")
    End If
  Next

  ' Update Grig
  Rng.Value = Data()

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

End Sub
 
Upvote 0
Ok Vlad working really well a few things noticed in testing:
  1. the data in the importexport could it clear after successfully adding the Xs or removing the Xs? Only on success
  2. Only if possible --
    1. Adding - if Xs already exist on the grid could a message say these were not added as Xs already exist
    2. Removing - if Xs dont exist on the grid could a message say these were not removed as Xs dont already exist
 
Upvote 0
There could be other questions like:
  • Does clearing according to the p.1 mean a gap (empty) row in ImportExport?
  • What does "successfully adding the Xs or removing the Xs" mean? It is always succesfully as for me.
  • Should code stop & not continue after warning in case of the p.2.1 or 2.2, or should ask to continue?
  • Why the p.2.1 and 2.2 are required, as according to the p.1 such rows can be just cleaned?
...and so on.

Please let me know if you able to twick the code by yourself for those minor actions?
As only you know all details about how it will be according to your needs.
 
Last edited:
Upvote 0
Hi Vlad agreed on your points am worried to tweak it as i don't want to adverse impact what is working very well. to touch on your points:

  1. is more as it was sucessfully loaded how I envisioned it or a sign to the user these were imported into the matrix
  2. As an audit to the user. Say they are trying to add something already on the matrix then it should alert "hey this is already here dont do your other process." in short they are adding it in this vba as an inventory and then are adding those into our system. So if already on the list it will alert them on the next process could be erroneous same goes for removals just opposite effect
  3. could can continue but just alert the user at end these already had Xs or these didnt have Xs to remove
  4. I hope your 4th point is answered above?
 
Upvote 0
Well, first of all see if the below alternative proposal suits:
  • For the cases of the p.2.1 and 2.2 the " - already here" is added to the A-column of ImportExport
  • To exclude gap (empty) rows in ImportExport the sorting by A-column is applied
VBA Code:
Option Explicit

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(4).Value ' Replace 4 by 1 to use A-column in Grid instead of D-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(, 3).Value
  For i = 2 To UBound(a)
    s = UCase(Trim(a(i, 1)))
    Grp = a(i, 2) ' group
    Acc = a(i, 3) ' account
    If Len(Trim(Grp)) > 0 And Len(Trim(Acc)) > 0 Then
      If Not (s = "A" Or s = "R") Then
        ShImpExp.Cells(i, 1).Select
        MsgBox "Wrong code - '" & s & "'" & vbLf & "Use 'A' to add 'X' to Grig" & vbLf & "Use 'R' to remove 'X' from Grid", vbExclamation, "Exit"
        Exit Sub
      End If
      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()
      If s = "A" Then s = "X" Else s = ""
      s1 = UCase(Data(DicAcc.Item(Acc), DicGrp.Item(Grp)))
      If s = s1 Then
        a(i, 1) = a(i, 1) & " -  already here"
      Else
        Data(DicAcc.Item(Acc), DicGrp.Item(Grp)) = s
        a(i, 1) = Empty
        a(i, 2) = Empty
        a(i, 3) = Empty
      End If
    End If
  Next

  ' Update ImportExport
  With ShImpExp.UsedRange.Resize(, 3)
    .Value = a()
    .Sort .Cells(1), xlAscending, Header:=xlYes
  End With

  ' Update Grig
  Rng.Value = Data()

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

End Sub
 
Upvote 0
Vlad tested one scenario out I think it was an error how it is produced the result seeing it put it in A2 and not a dialog box; it but it actually makes me like how it worked. so looks like this:

1609890782232.png


Very simple could it work with just a result tab, that yields the messaging? if it was added - say in result column success, if removed success, then if not it says already exists or no X's exist to remove. i think that would accomplish really that audit control i was thinking about. for my post #26 then i dont care if it auto clears anymore if it tells the user it worked that is all i care about or didnt work
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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