精华内容
下载资源
问答
  • VBA中经常会有一个这样的需求,在执行运算之前需要先判断指定工作表名称是否存在,否则程序将会报“下标越界”的错误,给用户不好的体验 我所知晓的查询工作表名称通常有3种方法: 一、遍历所有工作表,如果工作表...

    VBA中经常会有一个这样的需求,在执行运算之前需要先判断指定工作表名称是否存在,否则程序将会报“下标越界”的错误,给用户不好的体验

    我所知晓的查询工作表名称通常有3种方法:

    一、遍历所有工作表,如果工作表的名称等于指定名称,则反馈存在

    for each ws in worksheets
        if ws.name = "指定名称"  then
            msgbox "存在"
            exit for
        else
            msgbox "不存在"
        end if
    next ws    
    

      

    二、利用“下表越界”错误来判断

    Function SheetIsExist(strExcleName As String, strSheetName As String) As Boolean
        '//判断名称的工作表是否已经在指定的Excel文件中存在
    
        Dim shtSheet As Worksheet
        
        SheetIsExist = False
        On Error GoTo lab1
        Set shtSheet = Workbooks(strExcleName).Sheets(strSheetName)
        If shtSheet Is Nothing Then
            SheetIsExist = False
        Else
            SheetIsExist = True
        End If
        
        Set shtSheet = Nothing
        Exit Function
    
    lab1:
        SheetIsExist = False
    End Function
    

      

    在学习过程中记录一下,以备查阅

    转载于:https://www.cnblogs.com/Violet918/p/6653736.html

    展开全文
  • vba工作表重命名Renaming of worksheets can be done manually, but it will look like a massive task if there are a lot of worksheets to be renamed and when we need to rename it based on some conditions ...

    vba工作表重命名

    Renaming of worksheets can be done manually, but it will look like a massive task if there are a lot of worksheets to be renamed and when we need to rename it based on some conditions or values.

    重命名工作表可以手动完成,但是如果要重命名许多工作表以及需要根据某些条件或值重命名工作表,这将是一项艰巨的任务。

    Today, I'm going to show how we can actually rename the worksheets based on conditions by writing some VBA codes.

    今天,我将展示如何通过编写一些VBA代码,根据条件实际重命名工作表。

    Create the fundamental

    创造基础

    Renaming the worksheet can be as easy as looking for a fixed value and then renaming it accordingly with another fixed value.

    重命名工作表就像查找一个固定值,然后相应地使用另一个固定值重命名一样容易。

    Worksheets("Sheet1").Name = "New Sheet Name" 
    

    When we are trying to rename many worksheets, we can repeat to call the codes with minimal changes:

    当我们尝试重命名许多工作表时,我们可以重复执行以最少的更改来调用代码:

    Worksheets("Sheet1").Name = "New Sheet Name"
    Worksheets("Sheet2").Name = "New Sheet Name (2)" 
    

    Note: You must be careful when you trying to rename your worksheet:

    注意:尝试重命名工作表时,必须小心:

    1) Make sure the worksheet name you're referring to exists in the current worksheet

    1 )确保您要引用的工作表名称在当前工作表中存在

    2) Make sure the new worksheet name doesn't exist in the current worksheet

    2 )确保新工作表名称在当前工作表中不存在

    To better handle this, you can create a function like this:

    为了更好地处理此问题,您可以创建如下函数:

    Function getWorkSheet(ByVal WorkSheetName As String) As Worksheet
        On Error GoTo EH
        Set getWorkSheet = Worksheets(WorkSheetName)
        Exit Function
    EH:
        Set getWorkSheet = Nothing
    End Function
    
    Function renameWorkSheet(ByRef ws As Worksheet, ByVal NewWsName As String) As Boolean
        On Error GoTo EH
        Debug.Print ws.Name & " to " & NewWsName
        If getWorkSheet(NewName) Is Nothing Then
            ws.Name = NewWsName
            renameWorkSheet = True
        Else
            'New Worksheet Name already exists
            renameWorkSheet = False
        End If
        Exit Function
    EH:
        renameWorkSheet = False
    End Function 
    

    Function getWorkSheet will be used to look up if there's an existing worksheet with the name that passes to that function. In this case, this function will return a worksheet object. Of course, you can customize your own codes to return as a Boolean instead, for example, but it's all up to you how you design your code.

    函数getWorkSheet将用于查找是否存在名称传递给该函数的现有工作表。 在这种情况下,此函数将返回一个工作表对象。 当然,例如,您可以自定义自己的代码以返回为布尔值,但是这完全取决于您如何设计代码。

    Function renameWorkSheet will be used to handle the rename worksheet process. First, it checks if the worksheet exists before the code proceeds to rename it.

    函数renameWorkSheet将用于处理重命名工作表过程。 首先,它在代码进行重命名之前检查工作表是否存在。

    So, we can directly implement the rename process by using code like this:

    因此,我们可以使用以下代码直接实现重命名过程:

    renameWorkSheet Worksheets("Sheet1"), "New Sheet Name"
    renameWorkSheet Worksheets("Sheet2"), "New Sheet Name (2)" 
    

    which should rename the worksheets accordingly

    应该相应地重命名工作表

    So far so good, right?

    到目前为止一切顺利,对吗?

    Now, what we can do is to rename the worksheets based on a set of rules, by knowing or without knowing how many of worksheets in total we should be renaming.

    现在,我们可以做的是基于一组规则来重命名工作表,方法是知道(或不知道)总共应该重命名多少个工作表。

    To do that, I would think we can list out the requirements we needed, such as:

    为此,我认为我们可以列出所需的要求,例如:

    1) What to be compared, such as fixed values, or cell values, etc.

    1 )要比较的内容,例如固定值或单元格值等。

    2) The comparison method, such as exact match, partial match, or match by using start with, etc.

    2 )比较方法,例如精确匹配,部分匹配或通过使用start with进行匹配等。

    To address the above, I have come out with a  data structure like this:

    为了解决上述问题,我提出了这样的数据结构:

    Private Enum CompareMethod
        Exact = 1
        Within = 2
        StartWith = 3
    End Enum
    
    Private Enum ReadFrom
        Cell = 1
        FixedValue = 2
    End Enum
    
    Private Type WorkSheetSettings
        CompareKey As String
        CompareMethod As CompareMethod
        ReadFrom As ReadFrom
        ReplaceWith As String
        ReadFrom2 As ReadFrom
        ReplaceWith2 As String
        JoinString As String
    End Type 
    

    Enum is being used to define the possible values for our own defined type.

    枚举被用来为我们自己定义的类型定义可能的值。

    Type is being used to declare a user-defined type.

    类型用于声明用户定义的类型。

    In addition, we would need a function getNewWsName to generate the new worksheet name based on the options given above:

    另外,我们将需要一个函数getNewWsName来基于上面给出的选项生成新的工作表名称:

    Function getNewWsName(ByRef ws As Worksheet, ByRef config As WorkSheetSettings) As String
        If config.ReplaceWith <> "" Then
            If config.ReadFrom = Cell Then
                getNewWsName = ws.Range(config.ReplaceWith).Value
            Else
                getNewWsName = config.ReplaceWith
            End If
        End If
        If config.ReplaceWith2 <> "" Then
            If config.ReadFrom2 = Cell Then
                getNewWsName = getNewWsName & config.JoinString & ws.Range(config.ReplaceWith2).Value
            Else
                getNewWsName = getNewWsName & config.JoinString & config.ReplaceWith2
            End If
        End If
    End Function 
    

    Now, to rename "Sheet1" to "New Sheet Name", we can implement this:

    现在,要将“ Sheet1 ”重命名为“ New Sheet Name ”,我们可以实现以下功能:

        Dim NewWsName As String
        Dim ws As Worksheet
        Dim myConfig As WorkSheetSettings
        
        myConfig.CompareKey = "Sheet1"
        myConfig.CompareMethod = Exact
        myConfig.ReadFrom = FixedValue
        myConfig.ReplaceWith = "New Sheet Name"
        
        Set ws = getWorkSheet(myConfig.CompareKey)
        If Not ws Is Nothing Then
            NewWsName = getNewWsName(ws, myConfig)
            renameWorkSheet ws, NewWsName
        End If 
    

    In the event that we would like to rename Sheet1 based on the cell value of Sheet1's cell, A1, we can implement the same by using code:

    如果我们想基于Sheet1的单元格A1的单元格值重命名Sheet1,我们可以使用以下代码来实现:

        Dim NewWsName As String
        Dim ws As Worksheet
        Dim myConfig As WorkSheetSettings
        
        myConfig.CompareKey = "Sheet1"
        myConfig.CompareMethod = Exact
        myConfig.ReadFrom = Cell
        myConfig.ReplaceWith = "A1"
       
        Set ws = getWorkSheet(myConfig.CompareKey)
        If Not ws Is Nothing Then
            NewWsName = getNewWsName(ws, myConfig)
            renameWorkSheet ws, NewWsName
        End If 
    

    To replace multiple worksheets all together, we can simply declare the WorkSheetSettings as an array with the settings, like:

    要一起替换多个工作表,我们可以简单地将WorkSheetSettings声明为具有设置的数组,例如:

        Dim NewWsName As String
        Dim ws As Worksheet
        Dim myConfig(1) As WorkSheetSettings
        
        myConfig(0).CompareKey = "Sheet1"
        myConfig(0).CompareMethod = Exact
        myConfig(0).ReadFrom = Cell
        myConfig(0).ReplaceWith = "A1"
        
        myConfig(1).CompareKey = "Sheet2"
        myConfig(1).CompareMethod = Exact
        myConfig(1).ReadFrom = FixedValue
        myConfig(1).ReplaceWith = "New Sheet Name (2)"
        
        For i = 0 To UBound(myConfig)
            Set ws = getWorkSheet(myConfig(i).CompareKey)
            If Not ws Is Nothing Then
                NewWsName = getNewWsName(ws, myConfig(i))
                renameWorkSheet ws, NewWsName
            End If
        Next 
    

    You can try exploring the possible combinations by revisiting the rules:

    您可以尝试通过重新访问规则来探索可能的组合:

    1) What to be compared, such as fixed values, or cell values, etc.

    1 )要比较的内容,例如固定值或单元格值等。

    2) The comparison method, such as exact match, partial match, or match by using start with, etc.

    2 )比较方法,例如精确匹配,部分匹配或通过使用start with进行匹配等。

    Real Scenario Implementation

    实际方案实施

    Based on the question posted recently: Needs Help With Conditional Renaming for Several Tabs

    基于最近发布的问题: 需要几个标签的条件重命名的帮助

    For the 1st tab, the "Allocation" tab, I need to rename it as “Master_CellD28Value” which means that if Cell D28’s value is A123 - FIFO, the tab should be renamed to “Master_A123 - FIFO”.

    对于第一个选项卡,即“分配”选项卡,我需要将其重命名为“ Master_CellD28Value”,这意味着如果单元格D28的值为A123-FIFO,则该选项卡应重命名为“ Master_A123-FIFO”。

    For the 6th and 7th tab, the "ESD Trf Qty" and "EVNL Trf Qty" tabs, I need it to be renamed like this: The part before “ Trf Qty”_Cell C28’s value. For example, if EVNL Trf Qty tab’s cell C28 value is A123 - LIFO then the tab should be renamed to “EVNL_A123 - LIFO”

    对于第6和第7个选项卡,“ ESD Trf数量”和“ EVNL Trf数量”选项卡,我需要像这样重命名:“ Trf数量” _Cell C28值之前的部分。 例如,如果“ EVNL Trf数量”选项卡的单元格C28值为A123-LIFO,则该选项卡应重命名为“ EVNL_A123-LIFO”



    The tabs which are named "By Ctrn-EIN", "By Ctrn-EMSB", "By Ctrn-ETH", "By Ctrn-EPC" and "By Ctry-IDC", these need to be renamed to “CellE25Value_CellC28Value”. If Cell E25 Value’s is Canada and Cell C28’s Value is B987 -123 then the tab should be renamed to “Canada_B987 - 123”

    名为“按Ctrn-EIN”,“按Ctrn-EMSB”,“按Ctrn-ETH”,“按Ctrn-EPC”和“按Ctry-IDC”的选项卡,这些选项卡需要重命名为“ CellE25Value_CellC28Value”。 如果单元格E25的值是加拿大,单元格C28的值是B987 -123,则该选项卡应重命名为“ Canada_B987-123”



    As an error proofing method, the last tab, the subset list should be left alone.

    作为防错方法,最后一个选项卡,子集列表应保留为空。

    This actually can be done by simply changing the rules and apply the code below:

    实际上,只需更改规则并应用以下代码即可完成此操作:

    Sub RenameWorkSheets()
        Dim NewWsName As String
        Dim ArrPrefix(3) As WorkSheetSettings
        
        ArrPrefix(0).CompareKey = "Allocation"
        ArrPrefix(0).CompareMethod = Exact
        ArrPrefix(0).ReadFrom = FixedValue
        ArrPrefix(0).ReplaceWith = "Master_"
        ArrPrefix(0).ReadFrom2 = Cell
        ArrPrefix(0).ReplaceWith2 = "D28"
        
        ArrPrefix(1).CompareKey = "ESD "
        ArrPrefix(1).CompareMethod = StartWith
        ArrPrefix(1).ReadFrom = FixedValue
        ArrPrefix(1).ReplaceWith = "ESD_"
        ArrPrefix(1).ReadFrom2 = Cell
        ArrPrefix(1).ReplaceWith2 = "C28"
        
        ArrPrefix(2).CompareKey = "EVNL "
        ArrPrefix(2).CompareMethod = StartWith
        ArrPrefix(2).ReadFrom = FixedValue
        ArrPrefix(2).ReplaceWith = "EVNL_"
        ArrPrefix(2).ReadFrom2 = Cell
        ArrPrefix(2).ReplaceWith2 = "C28"
        
        ArrPrefix(3).CompareKey = "By "
        ArrPrefix(3).CompareMethod = StartWith
        ArrPrefix(3).ReadFrom = Cell
        ArrPrefix(3).ReplaceWith = "E25"
        ArrPrefix(3).ReadFrom2 = Cell
        ArrPrefix(3).ReplaceWith2 = "C28"
        ArrPrefix(3).JoinString = "_"
        
        Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Worksheets
            For i = 0 To UBound(ArrPrefix)
                Select Case ArrPrefix(i).CompareMethod
                Case CompareMethod.Exact
                    If ws.Name = ArrPrefix(i).CompareKey Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                Case CompareMethod.StartWith
                    If Left(ws.Name, Len(ArrPrefix(i).CompareKey)) = ArrPrefix(i).CompareKey Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                Case CompareMethod.Within
                    If InStr(ws.Name, ArrPrefix(i).CompareKey) > 0 Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                End Select
            Next
        Next
    End Sub 
    

    As you can see, the main code remains the same. Hence, minimal code changes are needed.

    如您所见,主要代码保持不变。 因此,需要最少的代码更改。

    Note:

    注意:

    In the case you see the worksheet name is not being renamed, there could be some possibilities:

    如果您看到工作表名称未重命名,则可能存在一些可能性:

    1) You provided an invalid sheet name that needs to be renamed

    1 )您提供了一个无效的工作表名称,需要重命名

    2) You provided an empty new sheet name

    2 )您提供了一个空白的新工作表名称

    3) You provided a new sheet name that is over the limited allowed in Excel (max allowed is 31 chars)

    3 )您提供的新工作表名称超出了Excel中允许的限制(允许的最大字符数为31个字符)

    4) You provided a new sheet name that already exists

    4 )您提供了一个已经存在的新工作表名称

    Well, we are done! And here is the complete code to share with you all:

    好了,我们完成了! 以下是与大家共享的完整代码:

    Private Enum CompareMethod
        Exact = 1
        Within = 2
        StartWith = 3
    End Enum
    
    Private Enum ReadFrom
        Cell = 1
        FixedValue = 2
    End Enum
    
    Private Type WorkSheetSettings
        CompareKey As String
        CompareMethod As CompareMethod
        ReadFrom As ReadFrom
        ReplaceWith As String
        ReadFrom2 As ReadFrom
        ReplaceWith2 As String
        JoinString As String
    End Type
    
    Sub RenameWorkSheets()
        Dim NewWsName As String
        Dim ArrPrefix(3) As WorkSheetSettings
       
        ArrPrefix(0).CompareKey = "Allocation"
        ArrPrefix(0).CompareMethod = Exact
        ArrPrefix(0).ReadFrom = FixedValue
        ArrPrefix(0).ReplaceWith = "Master_"
        ArrPrefix(0).ReadFrom2 = Cell
        ArrPrefix(0).ReplaceWith2 = "D28"
       
        ArrPrefix(1).CompareKey = "ESD "
        ArrPrefix(1).CompareMethod = StartWith
        ArrPrefix(1).ReadFrom = FixedValue
        ArrPrefix(1).ReplaceWith = "ESD_"
        ArrPrefix(1).ReadFrom2 = Cell
        ArrPrefix(1).ReplaceWith2 = "C28"
       
        ArrPrefix(2).CompareKey = "EVNL "
        ArrPrefix(2).CompareMethod = StartWith
        ArrPrefix(2).ReadFrom = FixedValue
        ArrPrefix(2).ReplaceWith = "EVNL_"
        ArrPrefix(2).ReadFrom2 = Cell
        ArrPrefix(2).ReplaceWith2 = "C28"
       
        ArrPrefix(3).CompareKey = "By "
        ArrPrefix(3).CompareMethod = StartWith
        ArrPrefix(3).ReadFrom = Cell
        ArrPrefix(3).ReplaceWith = "E25"
        ArrPrefix(3).ReadFrom2 = Cell
        ArrPrefix(3).ReplaceWith2 = "C28"
        ArrPrefix(3).JoinString = "_"
       
        Dim ws As Worksheet
       
        For Each ws In ActiveWorkbook.Worksheets
            For i = 0 To UBound(ArrPrefix)
                Select Case ArrPrefix(i).CompareMethod
                Case CompareMethod.Exact
                    If ws.Name = ArrPrefix(i).CompareKey Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                Case CompareMethod.StartWith
                    If Left(ws.Name, Len(ArrPrefix(i).CompareKey)) = ArrPrefix(i).CompareKey Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                Case CompareMethod.Within
                    If InStr(ws.Name, ArrPrefix(i).CompareKey) > 0 Then
                        NewWsName = getNewWsName(ws, ArrPrefix(i))
                        renameWorkSheet ws, NewWsName
                        Exit For
                    End If
                End Select
            Next
        Next
    End Sub
    
    Function getNewWsName(ByRef ws As Worksheet, ByRef config As WorkSheetSettings) As String
        If config.ReplaceWith <> "" Then
            If config.ReadFrom = Cell Then
                getNewWsName = ws.Range(config.ReplaceWith).Value
            Else
                getNewWsName = config.ReplaceWith
            End If
        End If
        If config.ReplaceWith2 <> "" Then
            If config.ReadFrom2 = Cell Then
                getNewWsName = getNewWsName & config.JoinString & ws.Range(config.ReplaceWith2).Value
            Else
                getNewWsName = getNewWsName & config.JoinString & config.ReplaceWith2
            End If
        End If
    End Function
    
    Function getWorkSheet(ByVal WorkSheetName As String) As Worksheet
        On Error GoTo EH
        Set getWorkSheet = Worksheets(WorkSheetName)
        Exit Function
    EH:
        Set getWorkSheet = Nothing
    End Function
    
    Function renameWorkSheet(ByRef ws As Worksheet, ByVal NewWsName As String) As Boolean
        On Error GoTo EH
        Debug.Print ws.Name & " to " & NewWsName
        If getWorkSheet(NewName) Is Nothing Then
            ws.Name = NewWsName
            renameWorkSheet = True
        Else
            'New Worksheet Name already exists
            renameWorkSheet = False
        End If
        Exit Function
    EH:
        renameWorkSheet = False
    End Function 
    

    What's more?

    更重要的是?

    Last but not least, we can further enhance our code to include features such as:

    最后但并非最不重要的一点是,我们可以进一步增强代码以包括以下功能:

    • Put the color options to worksheet's tab

      将颜色选项放入工作表的选项卡
    • Sort the worksheets according to certain orders

      根据某些顺序对工作表进行排序
    • Split the content of a worksheet into multiple worksheets

      将工作表的内容拆分为多个工作表
    • Merge the content of worksheets into a single worksheet

      将工作表的内容合并到一个工作表中

    This I think we can discuss that in future tutorials.

    我想我们可以在以后的教程中对此进行讨论。

    I hope you found this article useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.

    希望本文对您有所帮助。 鼓励您在下面提出问题,报告任何错误或对此作出任何其他评论。

    Note: If you need further "Support" about this topic, please consider using the Ask a Question feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts...

    注意 :如果您需要有关此主题的更多“支持”,请考虑使用Experts Exchange 的“提问”功能。 我会监督提出的问题,并很高兴与其他电子工程师一起提供以这种方式提出的问题所需的任何其他支持...

    Please do not forget to press the "Thumb's Up" button if you think this article was helpful and valuable for EE members.  It also provides me with positive feedback. Thank you!

    如果您认为本文对EE成员有用且有价值,请不要忘记按“ Thumb's Up”按钮。 它还为我提供了积极的反馈。 谢谢!

    翻译自: https://www.experts-exchange.com/articles/33507/Rename-a-list-of-worksheets-using-VBA-codes-with-conditions.html

    vba工作表重命名

    展开全文
  • 通过在主输入框模糊查询表明,显示包含相关名的Sheet,除了主外不匹配的都会被隐藏
  • vba工作表重命名By default, worksheet tabs in Excel are given generic names, such as Sheet1, Sheet2, and so on. If you have a lot of worksheets in your workbook, finding specific sheets can get tricky....
    vba工作表重命名

    vba工作表重命名

    00_lead_image_renaming_tabs

    By default, worksheet tabs in Excel are given generic names, such as Sheet1, Sheet2, and so on. If you have a lot of worksheets in your workbook, finding specific sheets can get tricky.

    默认情况下,Excel中的工作表选项卡具有通用名称,例如Sheet1,Sheet2等。 如果您的工作簿中有很多工作表,则查找特定工作表可能会很棘手。

    However, you can assign a name to each tab in your workbook so you can easily find the tab you want. It’s quite simple, and we’ll show you how.

    但是,您可以为工作簿中的每个选项卡分配一个名称,以便轻松找到所需的选项卡。 这很简单,我们将向您展示如何操作。

    To rename a tab, right-click on the tab and select “Rename” from the popup menu. You can also double-click on a tab.

    要重命名选项卡,请右键单击该选项卡,然后从弹出菜单中选择“重命名”。 您也可以双击选项卡。

    01_selecting_rename

    Type a new name for the tab and press Enter. Tab names can contain up to 30 characters.

    输入选项卡的新名称,然后按Enter。 标签名称最多可以包含30个字符。

    02_typing_tab_name

    Repeat this procedure for each tab you want to rename.

    对要重命名的每个选项卡重复此过程。

    03_sheets_renamed

    If your custom tab names are longer than the default names, they may not all fit on the worksheet tab bar. If they don’t, you will see three dots on the right end, initially. Click the dots to go to the first hidden tab to the right. Every time you click the three dots, the next hidden tab displays and is selected until you reach the last tab. As you click the three dots on the right, you will notice that three dots appear on the left end of the worksheet tab bar also. Click the three dots on the let to move to and select the next hidden tab on the left side of the worksheet tab bar, until you reach the first tab.

    如果您的自定义选项卡名称长于默认名称,则它们可能无法全部适合工作表选项卡栏。 如果没有,最初您会在右端看到三个点。 单击这些点以转到右侧的第一个隐藏选项卡。 每次单击三个点时,都会显示并选择下一个隐藏的选项卡,直到到达最后一个选项卡。 单击右侧的三个点时,您会注意到三个点也出现在工作表选项卡栏的左端。 单击让其移动到的三个点,然后选择工作表选项卡栏左侧的下一个隐藏选项卡,直到到达第一个选项卡。

    04_additional_tabs_indicator

    Try to be concise with your tab names. The longer your tab names, the fewer tabs will fit on the visible part of the worksheet tab bar.

    尽量简明扼要的标签名称。 选项卡名称越长,工作表选项卡栏的可见部分将容纳的选项卡就越少。

    You can also change the color of your worksheet tabs to distinguish among them.

    您还可以更改工作表选项卡的颜色以区分它们。

    翻译自: https://www.howtogeek.com/276285/how-to-rename-worksheet-tabs-in-excel/

    vba工作表重命名

    展开全文
  • 2、需要合并的工作表名称相同(如: “sheet1”),且数据字段一样(如:A列表示序号,B列表示姓名,C列表示月工资等); 3、需要合并的数据所在区域起始行列一致(如:有相同的表头) ALL IN ONE Option Explicit ...

    学习日志

    批量合并excel工作簿中同名工作表,适用条件:
    1、所有要汇总的工作簿在同一个文件夹中,这里以后缀为.xlsx为例;
    2、需要合并的工作表名称相同(如: “sheet1”),且数据字段一样(如:A列表示序号,B列表示姓名,C列表示月工资等,本例中指定数据位于a-c列);
    3、需要合并的数据所在区域起始行列一致(如:有相同的表头)

    ALL IN ONE

    Sub allinone()
        Dim path As String, filename As String
        Dim ws As Workbook, w As Workbook
        Dim starrow As Long, n As Long, r As Long, titlerow As Integer
        
        path = "C:\Users\Lee\Desktop\新建文件夹\全民一起VBA 提高篇\12"
        filename = Dir(path & "\*.xlsx")
        
        Set ws = Workbooks.Add
        '每次复制时开始的行数
        starrow = 1: n = 0: titlerow = 1
        Application.DisplayAlerts = False
        Do While filename <> ""
            Set w = Workbooks.Open(path & "\" & filename)
            n = n + 1
            '以下复制分表数据,第一张含表头,其他表格只复制数据区
            With w.Worksheets("sheet1")
                'xlCellTypeLastCell 可用11代替
                'Cells.SpecialCells(11).Row 包含字符的最后一个单元格所在行号
                
                r = Cells.SpecialCells(xlCellTypeLastCell).Row
                
                If n = 1 Then
                    .Range("a1", "c" & r).Select
                Else
                    .Range("a" & (titlerow + 1), "c" & r).Select
                End If
            End With
            Selection.Copy
            w.Close
            
            With ws.Worksheets("sheet1")
                .Range("b" & starrow).Select
                .Paste
                .Range("a" & starrow, "a" & (starrow + r - titlerow)) = Mid(filename, 1, Len(filename) - 5)
            End With
            
            '复制完后,根据B列中最后数据所在行号,重定义下次复制数据开始行号
            '.End(xlUp).Row指数据区域最后一行行号
            starrow = Range("b" & Rows.Count).End(xlUp).Row + 1
            
            filename = Dir
        Loop
        With ws.Worksheets("sheet1")
            .Range("a1", "a" & titlerow) = ""
            .Range("a" & Rows.Count).End(xlUp).value = ""
        End With
        
        Application.DisplayAlerts = True
        
        ws.SaveAs path & "\合并2.xlsx"
        
    End Sub
    
    展开全文
  • Excel技巧分享1-利用VBA定位工作表

    千次阅读 2018-10-31 14:45:41
    如果你的Excel 工作簿内有成百上千个工作表, 要查找其中的某一个工作表会很麻烦。使用下面的VBA 代码进行查询会极大的缩短查询时间。 Sub GoToSheet() Application.ScreenUpdating = False SheetName = ...
  • VBA-遍历工作表的方法

    千次阅读 2020-02-24 15:50:13
    遍历工作表的方法 在工作中,经常需要遍历工作表,这里展示最基本的功能,查询工作表的个数,其余功能大同小异: #方法一 Sub test() Dim i As Integer Dim s As Integer For i = Worksheets.Count To 1 Step -1 ...
  • 学习资源:《Excel VBA从入门到进阶》第10集 by蓝色幻想一、Excel工作表的分类Excel工作表有两大类,一类是我们平常用的工作表(worksheet),另一类是图表、宏表等。这两类的统称是sheets。在工作表的标签页上右击—...
  • 使用VBA验证工作表是否存在 ============================================================= 代码区域 ============================================================= Sub 判断工作表是否存在() Dim wk As...
  • 如图所示,该工作簿中建立“数据”和“数据查询”分别用于存储客户信息以及客户信息的查询。现希望在数据查询的关键字栏中输入某个关键字,并在“数据”中的各列对该关键字进行查找,然后希望能随着按钮...
  • 在新建工作表的这条路上踩过太多的坑了,虽然仅有个别的项目才需要用到新建工作表,但是很久不用,难免习惯性的baidu,然后就是一大堆乱七八糟的搜索结果:1、代码独立,获取不到新建工作表对象,无法在后续调用中...
  • VBA学习笔记本(二)——两个表格数据匹配_DEMO 将excel中两个工作表中,数据进行一键同步的通用方法
  • '------ 取当前,或活跃的工作表名字名字方法 Debug.Print ThisWorkbook.Name Debug.Print ActiveWorkbook.Name '------ 取其他工作表名字名字方法 '这下面的算个特例 'workbooks("") 路径是相对路径,居然不是...
  • 如图所示,在同一个目录下有多个工作表名称相同的工资汇总月报。现希望通过VBA建立该多个月份的月报总表,将多个工作簿中的“工资汇总”工作表导入当前工作簿,并以月份命名。该如何实现呢? ...
  • 工作表中的列数比较多,而且列数、列的位置经常变动时,如果在VBA代码中使用的静态指定的列名,将不得不经常改动VBA代码,非常不方便。 解决方法: 使用自定义列号、列名查找函数。 代码如下: 1. 列号查找...
  • 因为工作原因,经常需要替换各种公式,但是各种公式的名称,数量各不相同,做了一个VBA公式,供参阅~ 变换前(都是男内): 总行数 工作表名 公式替换 61 男内 NOT(ISNA(VLOOKUP(C2,男内!$A$1:$AB$...
  • 使用VBA代码操作工作表时,如果工作表并不存在,那么将产生运行时错误。为了避免这种异常的发生,使用代码操作任何表(不止局限于工作表,也适用于宏表和图表)之前应检测该工作表是否存在。在代码中这样用到此功能...
  • '1、工作簿 ' Workbooks 代表工作簿集合,所有的工作簿,Workbooks(N),表示已打开的第N个工作簿 ' Workbooks ("工作簿名称") ' ActiveWorkbook 正在操作的工作簿 ... ' 'Sheets("工作表名称") ...
  • excel宏 拆分工作表 在Excel宏代码中更改工作表名称 (Change Sheet Names in Excel Macro Code) If you download one of the sample Excel workbooks from my Contextures website, you might want to customize it,...
  • vba中的查找匹配函数

    万次阅读 2015-09-29 10:42:52
    最朴树的find例子:Range("G4") = Range("B:B").Find(Range("G3")).Row解释: Range(“B:B”)表示要查找的...Row返回查找到的位置的行数工作表函数Match例子Range("A1") = application.WorksheetFunction.Match(Ran
  • VBA中,是否可以设置一个工作表名称的列表(如图所示),当工作表属于该列表中时则不允许改变。该如何实现? 实现代码 编写一个备份工作表的程序,将当前活动工作表备份,然后在工作簿的Workbook_SheetChange...
  • vba 电子表格 宏代码 编程 用VBA代码实现遍历某文件夹下所有*.xls*文件,有两种实现方法
  • 案例:根据工作表名称查找标题栏中的对应标题的单元格 源表:   VBA语句:   Option Explicit Option Compare Text Sub 根据内容查找单元格() Dim sSheet As Worksheet Dim eachSheet As ...
  • 数据:N张工作簿,每张工作簿里又有M张工作表,每张工作表的同一位置Cells(2,16)有需要的数据,想讲这些数据汇总到一张新的表格。 新建一张“汇总.xlsm”,并打开,新建sub()过程; 依次打开工作簿; 源码如下: ...
  • vba 交叉查询 总览 从本质上讲,Visual Basic类型语言和c类型语言是完全不同的。 在Visual Basic类型语言中,处理数据的步骤已删除,而c类型语言使您不知所措,难以理解数据及其引用方式。 让我尝试解释一下我的...
  • 每天一篇Excel技术图文微信公众号:Excel星球NO.145-I ...虽然没有自古以来,但依然众所周知的是,在Excel中有一个很常用的功能:查找与替换。而在VBA编程中也有与之对应的两个方法:Find和Replace。我们先来聊Fi...
  • 在我们启动excel的时候,excel就自动创建了许多对象, 其中之一就是worksheets, 这个对像可以用来表示我们的工作表 中所有的表格 在工作中, 我们会经常遇到, 我们打开的一个excel文件,不止有一个表,而是很非常多个 ...
  • 当单个excel工作簿有很多工作表时,使用起来会有麻烦,虽然excel提供隐藏工作表功能,但点击及查找并不方便。如果能够在一个工作表中实现目录索引功能,点击按钮即可隐藏/显示对应工作表,会非常方便操作和查看。 ...
  • 2.名称表示法 ListColumn对象 向ListObject中添加新数据 使用VBA添加ListObject对象 Excel中数据存储的结构 (1)用输入界面的表格或者汇总形式的表格直接代替数据库存储。 (2)使用多维(交叉)记录数据。 ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,125
精华内容 850
关键字:

vba查找工作表名称