Solved Convert An Excel Macro To Run In Access VBA (1 Viewer)

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
I am altering a spreadsheet from access vba. I have this Excel macro that makes a portion of the required changes, but this is excel vba not the access vba.

Can someone provide me with what this code would look like if it were access vba to control excel?

Code:
 Columns("H:I").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Sheets("Sheet1").Select
    Sheets("Sheet1").Name = "TestData"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("$A$1:$B$1728").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    Columns("A:B").Select
    ActiveWorkbook.Worksheets("TestData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("TestData").Sort.SortFields.Add2 Key:=Range("A2:A1728"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("TestData").Sort
        .SetRange Range("A1:B1728")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    Columns("A:A").EntireColumn.AutoFit
 

Ranman256

Well-known member
Local time
Yesterday, 19:38
Joined
Apr 9, 2015
Messages
4,337
you can do this with 1 query. No code needed.
the query can remove duplicates.


I am altering a spreadsheet from access vba. I have this Excel macro that makes a portion of the required changes, but this is excel vba not the access vba. Can someone provide me with what this code would look like if it were access vba to control excel?
Code:
 Columns("H:I").Select Selection.Copy Sheets.Add After:=ActiveSheet Sheets("Sheet1").Select Sheets("Sheet1").Name = "TestData" ActiveSheet.Paste Application.CutCopyMode = False ActiveSheet.Range("$A$1:$B$1728").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes Columns("A:B").Select ActiveWorkbook.Worksheets("TestData").Sort.SortFields.Clear ActiveWorkbook.Worksheets("TestData").Sort.SortFields.Add2 Key:=Range("A2:A1728"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("TestData").Sort .SetRange Range("A1:B1728") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A1").Select Columns("A:A").EntireColumn.AutoFit
 

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
you can do this with 1 query. No code needed.
the query can remove duplicates.

With 1 query...meaning an access query?

I'm needing to keep it all done in Excel - but let access vba do the leg work
 

Micron

AWF VIP
Local time
Yesterday, 19:38
Joined
Oct 20, 2018
Messages
3,478
I'm needing to keep it all done in Excel - but let access vba do the leg work
That sounds contradictory but I think I understand. In that case, what you need to do is learn Automation. Maybe this will give you a starting point:

or research automating Excel from Access
 

Isaac

Lifelong Learner
Local time
Yesterday, 16:38
Joined
Mar 14, 2017
Messages
8,777
something like this should get you started. you should never use Activate, Active, Select, in Excel vba. I've tried to correct it as best as I can, I recommend you go with this route even if you have to make some small tweaks to my code. I've created it based on what I think seemed to be the intentions of the Select's and Activate's.
Code:
dim excelapp as object, wb as object, ws as object, ws2 as object
set excelapp=createobject("excel.application")
set wb = excelapp.workbooks.open("path to file") 'EDIT ************'
set ws = wb.sheets("name") 'EDIT **********'
ws.Columns("H:I").Copy
    set ws2 = wb.Sheets.Add After:=wb.worksheets("name of first sheet")
    ws2.Name = "TestData"
    ws2.paste
    Application.CutCopyMode = False
    ws2.Range("$A$1:$B$1728").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    ws2.Sort.SortFields.Clear
    ws2.Sort.SortFields.Add2 Key:=Range("A2:A1728"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ws2.Sort
        .SetRange Range("A1:B1728")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    'Range("A1").Select '** see below line instead
    application.goto ws2.range("A1"),true
    ws2.Columns("A:A").EntireColumn.AutoFit
 

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
something like this should get you started.

I am getting an error of:
Compile error:
Expected: end of statement

On this line

Code:
Set ws2 = wb.Sheets.Add After:=wb.worksheets("Data")

and it highlights this
Code:
After
 

Isaac

Lifelong Learner
Local time
Yesterday, 16:38
Joined
Mar 14, 2017
Messages
8,777
Sorry, try
Code:
Set ws2 = wb.Sheets.Add(After:=wb.worksheets("Data"))
 

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
When I try to run the function, I get this error

Compile error:
Method or data member not found

On this
Code:
.CutCopyMode

full line is

Code:
Application.CutCopyMode = False
 

Isaac

Lifelong Learner
Local time
Yesterday, 16:38
Joined
Mar 14, 2017
Messages
8,777
sorry, change Application to my excelapp object.
excelapp.cutcopymode=false
 

Isaac

Lifelong Learner
Local time
Yesterday, 16:38
Joined
Mar 14, 2017
Messages
8,777
This is what I get for doing everything from memory
 

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
This is what I get for doing everything from memory

FROM MEMORY!!!??? that is amazing! I hope I am one day this good

Now I am getting the error
Compile error:
Method or data member not found

on this
Code:
.Goto

and this is full line

Code:
Application.Goto ws2.Range("A1"), True
 

Isaac

Lifelong Learner
Local time
Yesterday, 16:38
Joined
Mar 14, 2017
Messages
8,777
darn it, it's the using Application. rather than Excelapp. again

Basically as you can see I have declared an Excel application instance and that variable needs to be used as the handle replacing Application. That handle can now do anything that Application can do in Excel vba, just like wb and ws can do anything that ThisWorkbook and a Worksheet variable could do in Excel vba.
 

jo15765

Registered User.
Local time
Yesterday, 16:38
Joined
Jun 24, 2011
Messages
130
oh wait. same thing.
should be
Code:
excelapp
 

Users who are viewing this thread

Top Bottom