当前位置:网站首页>VBA simple macro programming of Excel
VBA simple macro programming of Excel
2022-07-01 18:55:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Excel And VBA Simple macro programming
excel It is a classic data analysis tool , It contains many built-in functions , But the actual situation is sometimes much more complicated , and excel Macro programming of provides the function of user-defined functions , There is just a teacher who needs help to do some data analysis , I just learned , Here are my study notes . I use excel2013. If there is any discrepancy, please refer to .
List of articles
1、 preparation
- Open the development tools panel (excel By default, the main tab of does not have a development tool tab )
file -> Options -> Custom ribbon ->
At this time, the main tab has a development tool item
- Get into vba edit
Click on visual basic Get into
stay VBAProject Right click -> Insert -> modular An edit file appears, as shown in the following figure
Then you can write vba Code .
2、VBA Programming
2.1 Module declaration
Similar to a program , A module should have a main program entry, that is, module declaration , Such as Sub sname() ··· ··· End Sub
2.2 Variable declaration and assignment
The declaration format is : Dim ‘ Variable name ’ As ‘ Variable type ’ Such as Dim i As Integer Dim str As String The assignment format is : i = 3 str = “hello world!” Direct equal sign assignment
data type | Defining symbols | remarks |
|---|---|---|
character string | String | |
integer | Integer | -32768~32767 |
Long integer numbers | Long | -2147483648~2147483647 |
Single precision floating point numbers | Single | Accuracy of 6 |
Double precision floating point numbers | Double | Accuracy of 14 |
Date type | Date | |
Boolean type | Boolean | |
Variant | Variant | Universal type , Avoid using |
Object type | Object | Not in depth |
enum |
Enumeration type data definition format : Public|Private Enum Variable name member 1[= Constant expression 1] member 1[= Constant expression 1] ··· ··· End Enum for example : Public Enum WorkDays Sunday Monday Tuesday Wednesday Thursday Friday Saturday End Enum
2.3if-else structure
If ······ Then
  ······
ElseIf ······ Then
  ······
Else
  ······
End If2.4 Loop structure
Do While ······
  ······
Loop
For i = 0 To 100 Step 1
  ······
Next iJump out of for The loop can be used Exit For
2.5 Comparison operator
name | Operator |
|---|---|
be equal to | = |
It's not equal to | <> |
Less than | < |
Less than or equal to | <= |
Greater than | > |
Greater than or equal to | >= |
string matching | Like |
2.6 notes
VBA Single line comments in are in single quotation marks ' identification
3、 Common functions
3.1 Get table data
- For the first i That's ok j Column cell data
Sheets(‘ Table name ’).Cells(i,j).Value - For the first i Row data
Sheets(' Table name ').Rows(i) - For the first j Column
Sheets(' Table name ').Columns(j) - Get a piece
Range("a1:b60")
3.2 Copy cells and keep the original cell column width
Sheets(name_1).Cells(i, k).copy
Sheets(name_result).Cells(i,k).PasteSpecial xlPasteColumnWidths3.3 Set cell 、 Color of a row or column
Sheets(name_1).Rows(i).Interior.ColorIndex = 3'
Sheets(name_1).Rows(i).Font.ColorIndex = 1
Sheets(name_1).Cells(i,j).Interior.ColorIndex = 3'
Sheets(name_1).Cells(i,j).Font.ColorIndex = 13.4 Get the number of useful columns and rows of the table
irows1 = Sheets(name_1).UsedRange.Rows.Count
icolumns1 = Sheets(name_1).UsedRange.Columns.Count4、 Debugging operation
Shortcut key | function |
|---|---|
F8 | Run sentence by statement |
Shift+F8 | Run process by process |
Ctrl+F8 | Run to the cursor |
F9 | Add a breakpoint at the cursor |
debugging -> Add monitoring | Add monitoring |
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/130843.html Link to the original text :https://javaforall.cn
边栏推荐
- 力扣每日一题-第32天-1232. 缀点成线
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- Regular expression
- 1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
- R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
- Example explanation: move graph explorer to jupyterlab
- 2020, the regular expression for mobile phone verification of the latest mobile phone number is continuously updated
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- JS find the next adjacent element of the number in the array
- R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、不设置by参数则计算数据框指定数据列范围的基础描述性统计信息
猜你喜欢

关于企业中台规划和 IT 架构微服务转型

Lumiprobe biomolecular quantification - qudye Protein Quantification Kit

How to use the low code platform of the Internet of things for personal settings?

Li Kou daily question - Day 32 -589 N × Preorder traversal of tree

Relational database management system of easyclick

Halcon image calibration enables subsequent image processing to become the same as the template image

实例讲解将Graph Explorer搬上JupyterLab

Mysql database of easyclick

Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)

OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
随机推荐
Create your own NFT collections and publish a Web3 application to show them (Introduction)
数据库基础:select基本查询语句
Viewing technological changes through Huawei Corps (VI): smart highway
Openai video pre training (VPT): action learning based on watching unmarked online videos
Leetcode203 移除链表元素
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
R language uses the aggregate function of epidisplay package to divide numerical variables into different subsets based on factor variables, and calculate the summary statistics of each subset
实例讲解将Graph Explorer搬上JupyterLab
Lumiprobe 生物分子定量丨QuDye 蛋白定量试剂盒
Three.js学习-相机Camera的基本操作(了解向)
Stanford, salesforce|maskvit: masked vision pre training for video prediction
每周推薦短視頻:警惕“現象”與“問題”相互混淆
Must see, time series analysis
Three. JS learning - basic operation of camera (learn from)
How to realize the applet in its own app to realize continuous live broadcast
Regular expression
R language uses the transmute function of dplyr package to calculate the moving window mean value of the specified data column in dataframe data, and uses ggplot2 package to visualize the line graph b
golang 错误处理
Leetcode-141 circular linked list
Excel之VBA简单宏编程