Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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
Excel 如何阻止页面生成器随机递增?_Excel_Vba - Fatal编程技术网

Excel 如何阻止页面生成器随机递增?

Excel 如何阻止页面生成器随机递增?,excel,vba,Excel,Vba,好的,我想在右边的页脚中放一个excel报告的页码 为此,我使用VBA宏生成页面,并将信息从标题详细信息表复制到模板表的副本,然后将模板表用作报告的实际页面 问题是,简介页面按预期的初始值打印,但当转到下一页时,它会随机增加8,因此变成“第9页,共x页”。我怎样才能阻止它做这种随机跳转呢 报告页面 Sub ReportPages() Dim areas As Integer Dim pageNumberTotal As Integer areas = 1 ' Unhides the Templ

好的,我想在右边的页脚中放一个excel报告的页码

为此,我使用VBA宏生成页面,并将信息从标题详细信息表复制到模板表的副本,然后将模板表用作报告的实际页面

问题是,简介页面按预期的初始值打印,但当转到下一页时,它会随机增加8,因此变成“第9页,共x页”。我怎样才能阻止它做这种随机跳转呢

报告页面

Sub ReportPages()

Dim areas As Integer
Dim pageNumberTotal As Integer
areas = 1
' Unhides the Template sheet so it is ready to be used.
Worksheets("Template").Visible = True

' Add new pages based on the header details sheet.
Sheets("Header Details").Select

' Select cell A14 as the basis to fill out the template with data.
Range("A14").Select

Do While IsEmpty(ActiveCell) = False
    ActiveCell.Offset(1, 0).Select
    areas = areas + 1
Loop

pageNumberTotal = areas + 5

' Matches the amount of areas tested that have been specified in the Header Details sheet
Do While areas > 1
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = areas - 1 Then
        exists = True
        End If
    Next i
    If exists = True Then
        areas = areas - 1
        exists = False
    Else
        ' Decrement by 1 and copy the relevant data to the template.
        areas = areas - 1
        Sheets("Template").Select
        Sheets("Template").Copy After:=Worksheets("Template")
        Sheets("Template (2)").Select
        Sheets("Template (2)").Name = areas
        Range("I6").Select
        ActiveCell = areas
        ' Call the WetDry function and then protect the sheet.
        Call WetDry

    End If
Loop

' Closes the template sheet when it is done.
Worksheets("Template").Visible = False

'If ActiveSheet.Name = 1 Then
    'Dim pageNumberSetting As String
    'Dim pageNumber As Integer
    'pageNumber = 1
    'Sheets("Front Page").Select
    ' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
    'pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
    'With ActiveSheet.PageSetup
        '.RightFooter = pageNumberSetting
    'End With


    'pageNumber = pageNumber + 1
    'ActiveSheet.Next.Activate 
'End If
    ' Calls the next function and passes the value of the page number setting.
    Call FrontBackPages       
 End Sub
Sub FrontBackPages()

' Sets the preliminary features for the start of the report.
' Declarations of variables.
If ActiveSheet.Name = 1 Then
    Dim pageNumberSetting As String
    Dim pageNumber As Integer
    pageNumber = 1
    Sheets("Front Page").Select

    ' Debug message - please ignore.
    ' MsgBox " The Name of the active sheet is " & ActiveSheet.Name


    ' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
    pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
    With ActiveSheet.PageSetup
        .RightFooter = pageNumberSetting

    End With

    pageNumber = pageNumber + 1
    ActiveSheet.Next.Activate

    ' Selects the "Appx Summary" sheet and propegates it with information from other parts of the workbook,
    ' generates a page number for this part of the report.
    Do While ActiveSheet.Name <> "Appx Summary"
        pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."

        ' If the active sheet condition is met then the "Slip Resistance Testing" sheet is selected and is
        ' given a page number that will be placed in the lower right hand corner of the page.
        If ActiveSheet.Name = "Slip Resistance Testing" Then
            With ActiveSheet.PageSetup
                .FirstPage.RightFooter.Text = pageNumberParameter
            End With
            pageNumber = pageNumber + 1
            pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."
        End If

        ' The "Template" sheet is selected and the page number is decremented by 1.
        If ActiveSheet.Name = "Template" Then
            pageNumber = pageNumber - 1
        End If

        ' The active sheet is selected and in the right - hand footer is given a page number.
        ' After this the next sheet is activated.
        With ActiveSheet.PageSetup
            .RightFooter = pageNumberParameter
        End With
        pageNumber = pageNumber + 1
        ActiveSheet.Next.Activate
    Loop

    ' The page number is then added to the page and also gives the total page number as well.
    ' This will place the page number in the bottom right hand corner of the page..
        pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."
    With ActiveSheet.PageSetup
        .FirstPage.RightFooter.Text = pageNumberParameter
    End With
End If
' Selectes the "Header Details" sheet and the prompts the user that the pages have been successfully added.
Sheets("Header Details").Select
MsgBox "Pages Added!"

End Sub
首页和背面

Sub ReportPages()

Dim areas As Integer
Dim pageNumberTotal As Integer
areas = 1
' Unhides the Template sheet so it is ready to be used.
Worksheets("Template").Visible = True

' Add new pages based on the header details sheet.
Sheets("Header Details").Select

' Select cell A14 as the basis to fill out the template with data.
Range("A14").Select

Do While IsEmpty(ActiveCell) = False
    ActiveCell.Offset(1, 0).Select
    areas = areas + 1
Loop

pageNumberTotal = areas + 5

' Matches the amount of areas tested that have been specified in the Header Details sheet
Do While areas > 1
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = areas - 1 Then
        exists = True
        End If
    Next i
    If exists = True Then
        areas = areas - 1
        exists = False
    Else
        ' Decrement by 1 and copy the relevant data to the template.
        areas = areas - 1
        Sheets("Template").Select
        Sheets("Template").Copy After:=Worksheets("Template")
        Sheets("Template (2)").Select
        Sheets("Template (2)").Name = areas
        Range("I6").Select
        ActiveCell = areas
        ' Call the WetDry function and then protect the sheet.
        Call WetDry

    End If
Loop

' Closes the template sheet when it is done.
Worksheets("Template").Visible = False

'If ActiveSheet.Name = 1 Then
    'Dim pageNumberSetting As String
    'Dim pageNumber As Integer
    'pageNumber = 1
    'Sheets("Front Page").Select
    ' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
    'pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
    'With ActiveSheet.PageSetup
        '.RightFooter = pageNumberSetting
    'End With


    'pageNumber = pageNumber + 1
    'ActiveSheet.Next.Activate 
'End If
    ' Calls the next function and passes the value of the page number setting.
    Call FrontBackPages       
 End Sub
Sub FrontBackPages()

' Sets the preliminary features for the start of the report.
' Declarations of variables.
If ActiveSheet.Name = 1 Then
    Dim pageNumberSetting As String
    Dim pageNumber As Integer
    pageNumber = 1
    Sheets("Front Page").Select

    ' Debug message - please ignore.
    ' MsgBox " The Name of the active sheet is " & ActiveSheet.Name


    ' Sets the font type and size of the page number and page total in the bottom right hand corner of the page.
    pageNumberSetting = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000." & Chr(10) & "" & Chr(10) & "" & Chr(10) & ""
    With ActiveSheet.PageSetup
        .RightFooter = pageNumberSetting

    End With

    pageNumber = pageNumber + 1
    ActiveSheet.Next.Activate

    ' Selects the "Appx Summary" sheet and propegates it with information from other parts of the workbook,
    ' generates a page number for this part of the report.
    Do While ActiveSheet.Name <> "Appx Summary"
        pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."

        ' If the active sheet condition is met then the "Slip Resistance Testing" sheet is selected and is
        ' given a page number that will be placed in the lower right hand corner of the page.
        If ActiveSheet.Name = "Slip Resistance Testing" Then
            With ActiveSheet.PageSetup
                .FirstPage.RightFooter.Text = pageNumberParameter
            End With
            pageNumber = pageNumber + 1
            pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."
        End If

        ' The "Template" sheet is selected and the page number is decremented by 1.
        If ActiveSheet.Name = "Template" Then
            pageNumber = pageNumber - 1
        End If

        ' The active sheet is selected and in the right - hand footer is given a page number.
        ' After this the next sheet is activated.
        With ActiveSheet.PageSetup
            .RightFooter = pageNumberParameter
        End With
        pageNumber = pageNumber + 1
        ActiveSheet.Next.Activate
    Loop

    ' The page number is then added to the page and also gives the total page number as well.
    ' This will place the page number in the bottom right hand corner of the page..
        pageNumberParameter = "&B&9Page " & pageNumber & " of " & pageNumberTotal & "    &K00+000."
    With ActiveSheet.PageSetup
        .FirstPage.RightFooter.Text = pageNumberParameter
    End With
End If
' Selectes the "Header Details" sheet and the prompts the user that the pages have been successfully added.
Sheets("Header Details").Select
MsgBox "Pages Added!"

End Sub
子FrontBackPages()
'设置报告开头的初步功能。
'变量声明。
如果ActiveSheet.Name=1,则
Dim PageNumber设置为字符串
将页码设置为整数
页码=1
工作表(“首页”)。选择
'调试消息-请忽略。
“MsgBox”活动工作表的名称为“&ActiveSheet.Name”
'设置页面右下角页码和总页数的字体类型和大小。
pageNumberSetting=“&B&9Page”&pageNumber&“of”&pageNumberTotal&“&K00+000”&Chr(10)和“&Chr(10)和”&Chr(10)和”
使用ActiveSheet.PageSetup
.RightFooter=页码设置
以
页码=页码+1
ActiveSheet.Next.Activate
'选择“Appx摘要”工作表,并使用工作簿其他部分的信息对其进行编辑,
'为报告的此部分生成页码。
在ActiveSheet中执行。名称为“Appx摘要”
pageNumberParameter=“&B&9Page”&pageNumber&“of”&pageNumberTotal&“&K00+000。”
'如果满足激活板材条件,则选择“抗滑性测试”板材,并且
'给定将放置在页面右下角的页码。
如果ActiveSheet.Name=“防滑测试”,则
使用ActiveSheet.PageSetup
.FirstPage.RightFooter.Text=pageNumberParameter
以
页码=页码+1
pageNumberParameter=“&B&9Page”&pageNumber&“of”&pageNumberTotal&“&K00+000。”
如果结束
'选择“模板”页,页码递减1。
如果是ActiveSheet.Name=“Template”,则
页码=页码-1
如果结束
'选中活动工作表,并在右侧页脚中指定页码。
'在此之后,将激活下一张图纸。
使用ActiveSheet.PageSetup
.RightFooter=pageNumberParameter
以
页码=页码+1
ActiveSheet.Next.Activate
环
'然后将页码添加到页面中,并给出总页码。
'这将把页码放在页面的右下角。。
pageNumberParameter=“&B&9Page”&pageNumber&“of”&pageNumberTotal&“&K00+000。”
使用ActiveSheet.PageSetup
.FirstPage.RightFooter.Text=pageNumberParameter
以
如果结束
'选择“页眉详细信息”页,并提示用户页面已成功添加。
图纸(“标题详细信息”)。选择
MsgBox“已添加页面!”
端接头

我已经大大减少了代码量。我还删除了资源密集型
。激活
/
。选择
。我已经评论了很多,所以不需要很多解释,但我会说,如果你需要澄清,请随时回复这个答案


名人
  • 我不知道对
    WetDry
    的调用有什么作用,所以我把它添加到了我认为应该添加的地方

代码
编辑
  • 添加了
    Shtpw
    ,作为代码内多个调用的单个引用。
    • 这假定在每个受保护的工作表上使用相同的密码
  • 添加了检查,以查看工作表是否首先受到保护。如果是这样,它将使用定义的密码(
    Shtpw
    )取消保护
  • 添加了第二个检查,以查看最初是否有密码,并根据需要重新应用
什么是
活动页。下一步。激活
?我认为在每个工作表中使用
for
循环可能会更好。也,最好是@BruceWayne好的,我想我知道你是从哪里来的,但是我还是VBA新手。你能举一个你所说的
for
循环的例子吗?还有关于你对.Select和.Activate的使用的评论。我同意你发送的链接,但是我不认为从工作表中选择几个单元格是不合适的之所以可行,主要是因为我需要使用整张工作表。另一件需要记住的事情是您对
的使用。你把一行代码变成了三行,这并不一定是坏的,因为它会做同样的事情,但它不是那么干净/高效。酷!现在我遇到的一个问题是,代码从
If Application.property(Sht.name)
跳转到
End If
语句,我忘记了工作表是受保护的,这些代码行应该放在哪里?我已经更新并注释了代码,以帮助您实现这一点。一切正常吗?好的,我一直在调试模式下运行这个程序,现在它卡在if语句的循环中,如果Sht.ProtectsContents=True,那么
,仍然跳行到if
结尾,而不执行复制粘贴到新工作表。这是一个未经测试的添加。我将重新定位检查,以便
if
正确触发。(编辑完成)。我觉得我们越来越近了,但它仍然跳过了
If
语句