Hi, Have a problem with Power Automate and decided to use Excel VBA

MarkMueller

New Member
Joined
Aug 16, 2018
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I'm, working on a workbook that has a Master sheet with about 26 columns and up to 5 rows. This spreadsheet is used to update 6 other worksheets of which are defined as tables. When the content matches the headings the cells below the Headings are updated (copied) from the content within the Master worksheet to the workbooks named below. In many cases there are not match's and in that case a Blank can be inserted.

So the Sheet names within the workbook are: INPUT (Master sheet), Sub, Cust, NewUser, StudentEnroll, Attributes, and Audit!
------------------------------------------------------------------------------------
Heading information from INPUT (Master) listed below: Table Name(INPUT)

First NameLast NameStudent EmailUser RoleCompany NameMine Site NameLocation - City/Province/RegionLocation - CountryManager EmailRequestor NameRequestor EmailRequest TypeCourse 1Course 2Course 3Course 4Course 5Course 6Course 7Course 8Creator CommentsDateProcessVerify1DateProcessVerify2Date ProcessedCourseDescCourse Code

Heading information from Sub: Table Name(Sub)

First NameLast NameJob TitleJob Typejob LevelLocal First NameLocal Last NameLocal Language NameStudent EmailPerson IdPhoneMobileOrganization Name*ActiveDisable Invite EmailManager Emailproducts(NewsArea)parts(NewsArea)training(NewsArea)service(NewsArea)brightspace user role(Inherited)organizationdistributor.User(Inherited)organization2general Role(Inherited)organization3kac General(Inherited)organization4subsidiary General Role(Inherited)organization5subsidiary.User(Inherited)organization6user(Inherited)organization7user(Inherited)8organization9

Heading information from Cust: Table Name(Cust)

First NameLast NameJob TitleJob Typejob LevelLocal First NameLocal Last NameLocal Language NameStudent EmailPerson IdPhoneMobileActiveDisable Invite EmailCurrent Organization – Account# - Data SourceNew Organization – Account# - Data Source(Mandatory for New Users)*Manager Emailproducts(NewsArea)parts(NewsArea)training(NewsArea)service(NewsArea)brightspace user role(Inherited)organizationdistributor.User(Inherited)organization2general Role(Inherited)organization3kac General(Inherited)organization4user(Inherited)organization5user(Inherited)6organization7

Heading Information from NewUser: Table Name(

Action - IMPORTStudent EmailBlankFirst NameLast NamePasswordRoleActive (1 or 0)Email Address / User NameCourse Code

Heading Information from StudentEnroll: Table Name(Enroll)

Action - ENROLLStudent EmailBlankRoleCourse Code

Heading information from Attributes: Table Name(Attributes)

LMS UsernameAffiliated Org UnitsCompany Employee IdCompany NameCost CenterDepartmentDivisionEmployee RoleHired DateLanguageLocationManagerRegionRole CategoryTeam

Heading Information from Audit: Table Name(Audit)

Action - CREATEManager EmailStudent Email

I hope that the above information will help. It has been some time since using VBA. Thank you.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Kind of hard to understand exactly what you're looking for here. Are you just looking to go through all the rows of INPUT, and insert them all into Sub, Cust, NewUser, etc. whenever you run the VBA macro -- but only when/where the column headers match, and with blanks where they don't?

IMO this does sound like the sort of thing that might be better suited for Power Query, which lets you manipulate tables of data in a more powerful, less "manual" way. If you're looking to eschew that and use VBA though... maybe something like this?

VBA Code:
Sub InsertRowsInAllTables()
    Dim Master As ListObject
    Set Master = Sheets("INPUT").ListObjects("INPUT")
    
    ' Calls InsertRowsInTable for each of the Target tables below
    InsertRowsInTable Master, Sheets("Sub").ListObjects("Sub")
    InsertRowsInTable Master, Sheets("Cust").ListObjects("Cust")
    InsertRowsInTable Master, Sheets("NewUser").ListObjects("NewUser")
    InsertRowsInTable Master, Sheets("StudentEnroll").ListObjects("Enroll")
    InsertRowsInTable Master, Sheets("Attributes").ListObjects("Attributes")
    InsertRowsInTable Master, Sheets("Audit").ListObjects("Audit")
End Sub

Private Sub InsertRowsInTable(Master As ListObject, Target As ListObject)
    Dim Indices() As Long
    Dim oSource As ListColumn
    Dim oColumn As ListColumn
    Dim oRow As ListRow
    Dim vData As Variant
    Dim i As Long
    ReDim Indices(1 To Target.ListColumns.Count)
    
    ' For each column in the Target table, get its column # in the Master table
    On Error Resume Next
    For Each oColumn In Target.ListColumns
        Set oSource = Master.ListColumns(oColumn.Name)
        If Err Then
            Indices(oColumn.Index) = 0
            Err.Clear
        Else
            Indices(oColumn.Index) = oSource.Index
        End If
    Next oColumn
    On Error GoTo 0
    
    ' Add rows from Master table to Target table
    For Each oRow In Master.ListRows
        ReDim vData(1 To Target.ListColumns.Count)
        For i = 1 To Target.ListColumns.Count
            If Indices(i) > 0 Then vData(i) = oRow.Range(Indices(i)).Value
        Next i
        With Target.ListRows.Add
            .Range.Value = vData
        End With
    Next oRow
End Sub

If what you're trying to do is look up rows from INPUT which may or may not already be in Sub, Cust, etc., based on the fields they have in common, then that's a different matter. That case would be even more strongly suited for Power Query, or some other tool more intended for structured data manipulation.

You also referred to the "updated" tables as "worksheets" in one instance / "workbooks" in another. For the VBA code, it makes a difference whether they are in fact just separate worksheets within the same workbook, or entirely separate workbooks with their own files. I assumed it was the former; if not, then you would need to include references to those workbooks in the code, e.g. replace InsertRowsInTable Master, Sheets("Sub").ListObjects("Sub") with something like InsertRowsInTable Master, Workbooks("Sub.xlsx").Sheets("Sub").ListObjects("Sub"). In a similar vein, if the macro won't live in the same workbook with the INPUT table, then the same idea applies to the references to that table as well.
 
Upvote 0
Thank you, yes I understand that generic explanation can be difficult. To clarify:

The content, all of it, is within a workbook called: ImportTest.xlsx

The other worksheets are: Input, Sub, Cust, NewUser, StudentEnroll, Attributes, Audit.

Each of the worksheets have the Tables defined in my previous Post.

So Input provides the data to populate the other worksheets, under the appropriate heading match.

I hope that helps? And Thank you again.
 
Upvote 0
In that case: the VBA code in my previous post should do what you're looking for, as those were the assumptions I was operating under.

All you have to do is copy the code & paste it into a normal Module. Then run the InsertRowsInAllTabes macro. It will insert all the rows from your Input table into all of the other tables, under the appropriate heading match, with blanks where no match can be found.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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