Coen
Coen

Reputation: 31

Running VBA from Access to alter Excelfile

I'm trying to run a script from my access database to alter a excelfile that was just produced by the same script. I want to make a module that loops through each created sheet and workbook, while preforming some basic tasks. I'm very new to vba excel so I can't seem to find what is going wrong. In excel I wrote the first script using the macro recorder. This works fine.

I now want to make a script which replicates this excel macro in access, for which I already tried to write a script (shown below) but this seems to halt at the following actions: Cut and AutoFill. Also when leaving out these commands, a pop-up shows in which I'm asked to overwrite the existing file for each loop, which I want to avoid.

Many thanks in advance for any help!

Excel Code:

Sub Macro1()
    Range("A1:A200").Select
    Selection.Copy
    Range("A201").Select
    ActiveSheet.Paste
    Range("A401").Select
    ActiveSheet.Paste
    Range("A601").Select
    ActiveSheet.Paste
    Range("V1:AO200").Select
    Application.CutCopyMode = False
    Selection.Cut
    Range("B201").Select
    ActiveSheet.Paste
    Range("AP1:BI200").Select
    Selection.Cut
    Range("B401").Select
    ActiveSheet.Paste
    Range("BJ1:CC200").Select
    Selection.Cut
    Range("B601").Select
    ActiveSheet.Paste
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("V1").Select
    ActiveCell.FormulaR1C1 = "=IF(OR(AND(RC[-6]=FALSE,RC[-11]=FALSE,RC[-16]=FALSE,RC[-1]=FALSE),AND(RC[-6]="""",RC[-11]="""",RC[-16]="""",RC[-1]="""")),0,1)"
    Range("V1").Select
    Selection.AutoFill Destination:=Range("V1:V800")
    Range("V1:V800").Select
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$V$800").AutoFilter Field:=22, Criteria1:="0"
    Rows("2:1000").Select
    Selection.Delete Shift:=xlUp
    Selection.AutoFilter
    Columns("V:V").Select
    Selection.Delete Shift:=xlToLeft
    Range("A1").Select
End Sub  `

Acces Code:

For i = 1 To 7
   For j = 1 To 4
         DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, sCoun(i) & sQuer(j), myPath & sCoun(i) & ".xlsx"
                Set xlApp = New Excel.Application
                Set xlWB = xlApp.Workbooks.Open(myPath & sCoun(i) & ".xlsx")
                Set xlSh = xlWB.Sheets(sCoun(i) & sTab(j))

            xlSh.Range("A1:A200").Copy
            xlSh.Range("A201").PasteSpecial
            xlSh.Range("A401").PasteSpecial
            xlSh.Range("A601").PasteSpecial
            xlApp.CutCopyMode = False
            xlSh.Range("V1:AO200").Cut Destination:=Range("B201")
            xlSh.Range("AP1:BI200").Cut Destination:=Range("B401")
            xlSh.Range("BJ1:CC200").Cut Destination:=Range("B601")
            xlSh.Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            xlSh.Range("V1").FormulaR1C1 = "=IF(OR(AND(RC[-6]=FALSE,RC[-11]=FALSE,RC[-16]=FALSE,RC[-1]=FALSE),AND(RC[-6]="""",RC[-11]="""",RC[-16]="""",RC[-1]="""")),0,1)"
            xlSh.Range("V1").AutoFill Destination:=Range("V1:V800")
            xlSh.Range("A1").AutoFilter
            xlSh.Range("$A$1:$V$800").AutoFilter Field:=22, Criteria1:="0"
            xlSh.Rows("2:1000").Delete Shift:=xlUp
            xlSh.Range("A1").AutoFilter
            xlSh.Columns("V:V").Delete Shift:=xlToLeft
            xlSh.Range("A1").Select

                xlWB.Save
                xlWB.Close
                xlApp.Quit
                Set xlApp = Nothing
         Next j
        Next i`

Upvotes: 2

Views: 110

Answers (1)

Coen
Coen

Reputation: 31

Please try replacing Range with xlSh.Range in Destination parameter. – user3964075 5 mins ago

Upvotes: 1

Related Questions