Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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 visual Basic中调整宏代码_Excel_Vba - Fatal编程技术网

在excel visual Basic中调整宏代码

在excel visual Basic中调整宏代码,excel,vba,Excel,Vba,各位下午好, 目前,我正在处理一个宏的问题,该宏始终引用我在其中重新编写宏的一个excel工作表。我有大约300个工作表,我想在上面使用宏。我的问题是如何调整代码以引用这些工作表中的任何一个,而不仅仅是我在其中记录的工作表 案例 在下文中,我列出了部分代码,而关键部分由定义“Sheets”(“6110”).Select显示,其中宏始终引用工作表“6110”,而不是我正在使用的工作表。此外,所有工作表都使用相同的密码进行保护,这意味着我希望实现宏在代码开头取消对工作表的保护,并在操作结束时再次对其

各位下午好,

目前,我正在处理一个宏的问题,该宏始终引用我在其中重新编写宏的一个excel工作表。我有大约300个工作表,我想在上面使用宏。我的问题是如何调整代码以引用这些工作表中的任何一个,而不仅仅是我在其中记录的工作表

案例

在下文中,我列出了部分代码,而关键部分由定义“Sheets”(“6110”).Select显示,其中宏始终引用工作表“6110”,而不是我正在使用的工作表。此外,所有工作表都使用相同的密码进行保护,这意味着我希望实现宏在代码开头取消对工作表的保护,并在操作结束时再次对其进行保护的过程

(宏代码visual Basic)


希望下面的代码能帮助您理解要点:

Option Explicit

Sub LoopAllSheets()

    Dim ws As Worksheet
    Dim rng As Range

    'You loop all the worksheets in the workbook
    For Each ws In ThisWorkbook.Worksheets

        'Create a with statement referring the sheet you are currenlty loop
        With ws

            'Unprotect the loop sheet may need to add your password
            .Unprotect

            'Create a range you want to use
            Set rng = .Range("C5:C13,D4:P13")

            'You refer to the range fonts
            With rng.Font
                .Name = "Calibri"
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
            End With

        End With

    Next ws

End Sub

据我所知,您需要格式化每个工作表的字体等:

Option explicit
public Sub alignFont()
    Dim ws As Worksheet
    For each ws in ThisWorkbook.Worksheets
        ws.Unprotect
        with ws.Cells
            .Name = "Calibri"
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor  
        end with
    next ws
end Sub
一些旁注:

- Always use Option explicit at the beginning of your modules
- Do take the advise from the comments to not use select, this is not needed, see above
- Recording macros can be usefull to find certain properties etc - but you should usually not just try to Twist the recorded macro but start from scratch and check if you really want to do what the macro did. Eg do you really want to Change the strikethrough? This was added by the macro, but if all you care about is font etc, then this may lead to some very wrong Information in your EXcel. 

你可能会从阅读中受益。你可能需要扩展一点你想要达到的目标<代码>为了在没有任何条件的情况下引用这些工作表中的任何一个,很难猜测。。。是否要同时在所有宏上运行相同的宏?或者你想在什么时候单独穿?跟随@Pᴇʜ建议,尽管我认为在尝试此操作之前,您应该阅读数不清的VBA教程之一。我同意您应该学习@Pᴇʜ也提供建议。但要实现所需的功能,只需删除第一行
工作表(“6110”)。选择
将使其在当前工作表上运行,而不是转到该工作表。如果你想一次完成所有工作,一个简单的循环就可以了。如果你可以观看一个教程,为什么还要阅读它:@JelleManne你正在尝试循环工作簿中的所有工作表,并在
范围(“C5:C13,D4:P13”)
- Always use Option explicit at the beginning of your modules
- Do take the advise from the comments to not use select, this is not needed, see above
- Recording macros can be usefull to find certain properties etc - but you should usually not just try to Twist the recorded macro but start from scratch and check if you really want to do what the macro did. Eg do you really want to Change the strikethrough? This was added by the macro, but if all you care about is font etc, then this may lead to some very wrong Information in your EXcel.