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)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
... Looking for a way in another tab to just list group #, Account # ...
Hi,

As the Account-Group data layout in another tab is not posted, it is not clear the destination data are just numbers or strings from the ranges.
Your code attempt, if exists, will be helpful to see what and where the problem is.
If code does not exist then start playing with the below suggestions.
Assuming that the accounts and groups are incremented by +1 as shown in the post #1, to auto-create Accounts-Groups list in another tab put the below code into code module of that another sheet. Change sheet selection to see the result.

VBA Code:
Option Explicit

' Put this code into VBA module of the sheet with Accoubt-Croup list
' It is assumed that Accounts in the A7:A... are incremented by +1, as well as Groups in the row #5 of the main table

Private Sub Worksheet_Activate()

  Const DataCell = "O7"
  Dim ShTable As Worksheet, ShList As Worksheet
  Dim Rng As Range
  Dim Data() As Variant, Dest() As Variant
  Dim i As Long, j As Long, k As Long

  Set ShTable = Sheets("Sheet1")  ' Sheet with data table
  Set ShList = Me                 ' This sheet for Accounts-Groups list

  With ShTable.Range("A1").CurrentRegion
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Data() = Rng.Value
  End With

  ReDim Dest(1 To UBound(Data) * UBound(Data, 2), 1 To 2)
  For i = 1 To UBound(Data)
    For j = 1 To UBound(Data, 2)
      If Data(i, j) = "X" Then
        k = k + 1
        Dest(k, 1) = i  ' Account #
        Dest(k, 2) = j  ' Group #
      End If
    Next
  Next

  With ShList.UsedRange.Resize(, 2)
    Application.ScreenUpdating = False
    .ClearContents
    .Rows(1) = Array("Account", "Group")
    If k > 0 Then
      .Offset(1).Resize(k).Value = Dest()
    End If
    Application.ScreenUpdating = True
  End With

End Sub

The result looks as follows:
Wb1.xlsb
AB
1AccountGroup
211
312
415
516
617
718
819
921
1022
1125
1226
1327
1428
1529
1631
1733
1835
1936
2038
2139
2241
2343
2445
2546
2648
2749
2851
2954
3056
3158
3259
3361
3464
3566
3668
3769
3871
3974
4076
4178
4281
4384
4486
4588
4691
4794
4896
4998
50101
51104
52106
53108
Sheet2
 
Last edited:
Upvote 0
Just for the case - below is the version for getting Account# from the D-column and Group# from the 5th row:
VBA Code:
Option Explicit

' Put this code into VBA module of the sheet with Accoubt-Croup list
' It is assumed that Accounts in the A7:A... are incremented by +1, as well as Groups in the row #5 of the main table

Private Sub Worksheet_Activate()

  Const DataCell = "O7"
  Dim ShTable As Worksheet, ShList As Worksheet
  Dim Rng As Range
  Dim Acc() As Variant, Grp() As Variant, Data() As Variant, Dest() As Variant
  Dim i As Long, j As Long, k As Long

  Set ShTable = Sheets("Sheet1")
  Set ShList = Me

  With ShTable.Range("A1").CurrentRegion
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Acc() = Rng.EntireRow.Columns(4).Value
    Grp() = Rng.Offset(-2).Value
    Data() = Rng.Value
  End With

  ReDim Dest(1 To UBound(Data) * UBound(Data, 2), 1 To 2)
  For i = 1 To UBound(Data)
    For j = 1 To UBound(Data, 2)
      If Data(i, j) = "X" Then
        k = k + 1
        Dest(k, 1) = Acc(i, 1)
        Dest(k, 2) = Grp(1, j)
      End If
    Next
  Next

  With ShList.UsedRange.Resize(, 2)
    .ClearContents
    .Rows(1) = Array("Account", "Group")
    If k > 0 Then
      .Offset(1).Resize(k).Value = Dest()
    End If
  End With

End Sub

The result:
Wb1.xlsb
AB
1AccountGroup
2XX1Group 1
3XX1Group 2
4XX1Group 5
5XX1Group 6
6XX1Group 7
7XX1Group 8
8XX1Group 9
9XX2Group 1
10XX2Group 2
11XX2Group 5
12XX2Group 6
13XX2Group 7
14XX2Group 8
15XX2Group 9
16XX3Group 1
17XX3Group 3
18XX3Group 5
19XX3Group 6
20XX3Group 8
21XX3Group 9
22XX4Group 1
23XX4Group 3
24XX4Group 5
25XX4Group 6
26XX4Group 8
27XX4Group 9
28XX5Group 1
29XX5Group 4
30XX5Group 6
31XX5Group 8
32XX5Group 9
33XX6Group 1
34XX6Group 4
35XX6Group 6
36XX6Group 8
37XX6Group 9
38XX7Group 1
39XX7Group 4
40XX7Group 6
41XX7Group 8
42XX8Group 1
43XX8Group 4
44XX8Group 6
45XX8Group 8
46XX9Group 1
47XX9Group 4
48XX9Group 6
49XX9Group 8
50XX10Group 1
51XX10Group 4
52XX10Group 6
53XX10Group 8
Sheet2
 
Upvote 0
apologies here is my 2nd tab how I have it. I will try the above as well. Apologize again if it requires tweaking after seeing my below 2nd sheet

Book1
ABCDEFGH
1Add or RemveGroup #Account #
2A10000XX1
3A10000XX2
4A10000XX3
5A10000XX4
6A10000XX5
7A10000XX6
8A10000XX7
9A10000XX8
10A10000XX9
11A10000XX10
ImportExport
 
Upvote 0
Column A ends up being unique #s that define the account for my example I just had them increase by +1 but they are actually 11 digit #s that are all unique and no correlation between each other
 
Upvote 0
Well, try this code:
VBA Code:
Option Explicit
' Put this code into VBA module of the ImportExport sheet

Private Sub Worksheet_Activate()
  
  Const DataCell = "O7"
  Dim ShTable As Worksheet, ShList As Worksheet
  Dim Rng As Range
  Dim Acc() As Variant, Grp() As Variant, Data() As Variant, Dest() As Variant
  Dim i As Long, j As Long, k As Long
  
  Set ShTable = Sheets("Grid")
  Set ShList = Me  ' = Sheets("ImportExport")
  
  With ShTable.Range("A1").CurrentRegion
    Set Rng = .Range(DataCell).Resize(.Rows.Count - .Range(DataCell).Row + 1, .Columns.Count - .Range(DataCell).Column + 1)
    Acc() = Rng.EntireRow.Columns(4).Value
    Grp() = Rng.Offset(-1).Value
    Data() = Rng.Value
  End With
  
  ReDim Dest(1 To UBound(Data) * UBound(Data, 2), 1 To 2)
  For i = 1 To UBound(Data, 2)
    For j = 1 To UBound(Data)
      If Data(j, i) = "X" Then
        k = k + 1
        Dest(k, 1) = Grp(1, i)
        Dest(k, 2) = Acc(j, 1)
      End If
    Next
  Next
  
  With ShList.UsedRange.Resize(, 3)
    Application.ScreenUpdating = False
    .ClearContents
    .Rows(1) = Array("Add or Remove", "Group #", "Account #")
    If k > 0 Then
      .Resize(k, 1).Offset(1).Value = "A"
      .Resize(k, 2).Offset(1, 1).Value = Dest()
    End If
    Application.ScreenUpdating = True
  End With

End Sub
 
Upvote 0
got this error Vlad.

1609794789586.png
 
Upvote 0
got this error Vlad.
Code should not go to standard module (VBE-Insert-Module).
It was highlighted in the comment - "Put this code into VBA module of the ImportExport sheet".
That means - copy the code, do right click on ImportExport sheet tab, choose "View code" and paste the code.
 

Attachments

  • 2021-01-05 00_36_53-Clipboard.png
    2021-01-05 00_36_53-Clipboard.png
    29.6 KB · Views: 3
Upvote 0
I thought I did that other than assign it to a button on that sheet. Should I not do that?

1609796777453.png
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

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