Use vba to process Excel table data to achieve key-value conversion, which is suitable for converting encoding into corresponding text

Recently, I encountered a situation where Party A needed to provide a list of users who log in to the system and the corresponding role permissions. Helplessly, the data exported directly from the database corresponds to the code, and it has not been converted into Chinese. I was thinking about being lazy and can use Excel to directly convert it. I checked the cell format on the Internet, but it requires 2 to 3 codes. . Most of them use vba, so I will record my operation here, and also provide direct copy for other people who need it, haha, let’s not talk about the code.

1. Example of vba processing encoding to text

The following code is to convert the encoded data of the data in column D from 2 to 200 rows into corresponding text.

Sub 列表修改()
    Dim rRng As Range
    Set rRng = Range(" D2:D200")
    Dim rCell As Range
    For Each rCell In rRng
    Select Case rCell.Value
    Case 1
    rCell.Value = "超级管理员"
    Case 2
    rCell.Value = "普通用户"
    Case 3
    rCell.Value = "浏览用户"
    Case 4
    rCell.Value = "普通管理员"
    Case 5
    rCell.Value = "短信用户"
    End Select
    Next

End Sub

Before conversion:
insert image description here
After conversion:
insert image description here

2. VBA operation steps

Next, write a simple operation step:
1. Open the programming interface of Excel: Click File->Options->Customize Ribbon->Select Developer Tools. Click on the developer tools that appear, and there are tools for writing VBA.
2. Create a macro-enabled workbook: first create a new workbook, and save the workbook as a "macro-enabled workbook" type.
3. Open the VBA editor: Open the VBA editor through the ribbon "Development Tools → Code → Visual Basic" or the shortcut key Alt + F11.
4. Insert a new module: VBA code in an Excel workbook is usually saved in a worksheet object or module. In this example, we use modules to hold the VBA code. First select the workbook in the project list on the left, then right-click, and select "Insert" in the pop-up option list. In the secondary menu, select "Module" to complete inserting a new module.
5. Write VBA code in the newly inserted module, for example: Sub ClickTest().

3. Learning experience

In Excel, VBA is a powerful programming language that allows users to customize Excel functions to achieve various tasks such as automation, batch processing, and data analysis. Recently I started to learn VBA, and share my learning experience in this article.

First, I found an online VBA course on the Internet, which allowed me to understand the basics of VBA, such as basic syntax, variables, control structures, arrays, and functions. In the process of learning, I encountered some difficulties, such as understanding how to use objects, methods and properties, and how to handle errors and exceptions. But through repeated practice and reading relevant materials, I gradually mastered these knowledge points.

During the learning process, I realized that VBA programming is not only a skill, but also a way of thinking. VBA can help me better understand the concepts of programming and data structures, and make me pay more attention to the readability and maintainability of the code. At the same time, VBA also allows me to make better use of the functions of Excel and complete work tasks more efficiently.

In the process of learning VBA, I think the most important thing is practice and exploration. Only through continuous practice and trying can we truly grasp the essence of VBA. At the same time, I also recommend that beginners start learning from simple programs, gradually increase the difficulty, and constantly consult relevant information during the learning process to deepen their understanding of VBA.

In short, learning VBA has allowed me to better understand the concepts of programming and data structures, and at the same time, it has also enabled me to make better use of the functions of Excel and complete work tasks more efficiently. I believe that in the future work, VBA will become one of my indispensable skills.

Guess you like

Origin blog.csdn.net/weixin_43578304/article/details/132692394