Opening and Modifying Excel with Access VBA

eaddi

New Member
Joined
Apr 2, 2012
Messages
24
Hello

I am trying to open and modify an Excel 2016 file using VBA in Access 2016. I pieced together the code below, but I keep getting "Compile Error: Sub or Function not defined" at the bolded section of the code below. Does anyone know how to correct this error or if there is a better code to use?

Thanks in advance




Sub OpenFileWithShell()
Dim Shex As Variant
Dim strPath As String
Dim strFileName As String
Dim strFileType As String
Dim strApplication As String
Dim xRow As Integer
Dim strSearch As String


strPath = "D:\Source Files" & ""
strFileName = "Inventory.xls"
strFileType = Mid(strFileName, InStrRev(strFileName, "."))

Select Case strFileType 'Identify type of file and set application to use
Case ".xls"
strApplication = "Excel.exe " 'Note the trailing space
Case ".docx"
strApplication = "Winword.exe " 'Note the trailing space
End Select

'Enclose path and filename in double quotes in case of spaces (previously omitted)
VarMyFile = Shell(strApplication & Chr(34) & strPath & strFileName & Chr(34), vbNormalFocus)

' UserForm1.Show vbModeless 'Open as modeless if access to workheet is required.

strSearch = "Provision Date"
' Assuming Total is in column C as your picture shows, but you can configure to search anywhere

xRow = Range("A" & Rows.Count).End(xlUp).Row
Range("$A1:A" & xRow).Select

Selection.Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select

Range("A1:A" & ActiveCell.Row - 1).EntireRow.Delete

Cells.Select
Selection.UnMerge
Range("D:E,H:H,K:K,L:L").Select
Range("L1").Activate
Selection.Delete Shift:=xlToLeft
Range("A1").Select
ActiveWorkbook.Save
ActiveWindow.Close

End Sub


<tbody>
</tbody>
 
Last edited:
I hope this explains what I am trying to do.





Delete ROWS
My Excel sheet contains a Summary and a Detailed section within the same sheet. I want to delete the Summary section in Red. I was trying to do this by saying find "Provision Date" and then delete all rows above the row that contain "Provision Date"



Header1Header2Header3Header4Header5
5/14/20185000 Magic TvWestN/a
5/14/201810000 Magic TvEastN/a
5/14/20182000 Magic TvSouthN/a
5/14/2018600 Magic TvNorthN/a
5/13/20181500 Magic TvWestN/a
5/13/2018900 Magic TvEastN/a
5/13/20181260 Magic TvSouthN/a
5/13/201815000 Magic TvNorthN/a
5/13/20180 Magic TvWestN/a
SHeader1 SHeader2SHeader3SHeader4SHeader5SHeader6SHeader7SHeader8SHeader9
5/14/2018200JohnsonPack001WestIncomingTaxable30 daysN
5/14/2018300SmithPack03NorthReferralTaxable30 daysN
5/14/2018500LongbottomPack03EastIncomingTaxable30 daysN
5/14/2018150ClarkPack001SouthIncomingTaxable30 daysN
5/14/2018-890FoxPack001WestIncomingNotaxable30 daysY
5/14/20182000JamesPack03NorthIncomingTaxable90 daysN
5/14/2018500BlackPack001NorthIncomingTaxable30 daysN
5/14/2018600WhilkenPack03EastReferralTaxable30 daysN

<tbody>
</tbody>




Delete columns after Summary section has been deleted
Once my header data has been deleted and my detail data has shifted up so that the row that contains "Provision Date" is now in row 1. I need to unmerge my data. Assume SHeader 3 consist of the column C and D and SHeader6 consist of column H and I. What I need to do is unmerge the columns and then delete column D and column I, since both of them are blank.


SHeader1 SHeader2 SHeader3SHeader4SHeader5SHeader6SHeader7SHeader8SHeader9
5/14/2018200 Johnson Pack001 West Incoming Taxable 30 days N
5/14/2018300SmithPack03NorthReferralTaxable30 daysN
5/14/2018500LongbottomPack03EastIncomingTaxable30 daysN
5/14/2018150ClarkPack001SouthIncomingTaxable30 daysN
5/14/2018-890FoxPack001WestIncomingNotaxable30 daysY
5/14/20182000JamesPack03NorthIncomingTaxable90 daysN
5/14/2018500BlackPack001NorthIncomingTaxable30 daysN
5/14/2018600WhilkenPack03EastReferralTaxable30 daysN

<tbody>
</tbody><colgroup><col span="9"></colgroup>
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
There are no rows that contain the text 'Provision Date'.:eek:
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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