Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过固定vba代码移动表格_Vba_Excel - Fatal编程技术网

通过固定vba代码移动表格

通过固定vba代码移动表格,vba,excel,Vba,Excel,我有一个现有的VBA代码,如下所示: Sub fillDL() Dim cn As Object, lr As Integer, soct As Integer, i As Integer lr = Range("P31").End(xlDown).Row If lr > 32 Then Rows("32:" & lr - 1).Delete Shift:=xlUp Range("AO1").Formula = "=COUNTA(AN:AN)"

我有一个现有的VBA代码,如下所示:

Sub fillDL()
    Dim cn As Object, lr As Integer, soct As Integer, i As Integer
    lr = Range("P31").End(xlDown).Row
    If lr > 32 Then Rows("32:" & lr - 1).Delete Shift:=xlUp
    Range("AO1").Formula = "=COUNTA(AN:AN)"
    soct = Range("AO1").Value + 31
    If soct > 31 Then
        Rows("32:" & soct).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Set cn = CreateObject("ADODB.Connection")
        cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";")
        Range("A32").CopyFromRecordset cn.Execute("select b.f4, b.f5, b.f6, b.f7, '','','','','','','', '','','','','','', b.f9, b.f10,'','','','', b.f12,'','','','', b.f13,'','','','',b.f14 " & _
                                                    "from [BILL$AN1:AN] a inner join [pivot$A6:N] b on a.f1 = b.f1 where a.f1 is not null")"

因为它是从第31行开始运行的,但是现在我需要将表从第31行移动到第11行。你们能帮我将来修复这个VBA吗?为了让自己的代码更灵活,你们应该添加一个
常量RowNum,只要Long=11
,并且在
中使用对
RowNum
的引用。这样,如果将来要修改为第150行(或其他任何行),可以在单个位置进行修改

代码

Option Explicit

Const RowNum    As Long = 11

Sub fillDL()

    Dim cn As Object, lr As Long, soct As Long, i As Long

    lr = Range("P" & RowNum).End(xlDown).Row

    If lr > RowNum + 1 Then Rows(RowNum + 1 & ":" & lr - 1).Delete Shift:=xlUp

    Range("AO1").Formula = "=COUNTA(AN:AN)"
    soct = Range("AO1").Value + RowNum
    If soct > RowNum Then
        Rows(RowNum + 1 & ":" & soct).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        ' rest of your code...

只需将所有出现的“31”更改为“11”,并且对“32”的引用不能更改为“12”,直到单元格12到单元格32,列AN中的数字才会出现