VBA 学习笔记之– Excel 对象篇(一)

来源:互联网 发布:中美网络大战华为 编辑:程序博客网 时间:2024/05/23 00:12

VBA 学习笔记之– Excel 对象篇(一)

Excel 2013 版之后的本地帮助文档不见啦! 选中对象浏览器中内容按下F1会直接跳转到msdn的页面, 然后你会发现自己不停的各种页面直接跳来跳去,慢的蜗牛一样的微软服务器真心让人happy不起来.
在受够了卡成狗的Chrome和蹩脚的Excel 2013 Developer Documentation

https://www.microsoft.com/en-us/download/details.aspx?id=40326

本着自己动手丰衣足食的原则, 还是做笔记来的靠谱些.
それて、手帳を記入して始めましょう!


1. AddIn (AddIns,AddIns2)

第一个亮相的就是自动化的小能手, VBA开发的常用功能–加载项!
AddIn 是AddIns和AddIns2集合的成员, AddIns代表当前所有可用AddIn,无论是否载人Excel. AddIns2 还包含未在列表中,但已被Excel打开的加载宏.

属性:
- Name 加载项名称
- FullName 全名,即磁盘上的完整路径
- Path 路径, 所在位置.
- Installed 显示/设置AddIn是否装载入Excel程序
- IsOpen 显示Add-in是否已打开, 只读哟
- ProgID automation add-in用的 ,前面部分是通用名 最后的数字是版本号,布吉岛怎么用…
- CLSID 好吧 这个也不知道是做啥的 就是个唯一标识符

方法:
- expression .Add(Filename, CopyFile)
- Addins 和 Addins2 的Add方法有大同小异,只不过不支持Automation Add-in.

Name Required/Optional Data Type Description Filename Required String The name of the file that contains the add-in or the ProgID of the automation add-in that you want to add to the list in the add-in manager. CopyFile Optional Variant Ignored if the add-in file is on a hard disk. True to copy the add-in to your hard disk, if the add-in is on a removable medium (a floppy disk or compact disc). False to have the add-in remain on the removable medium. If this argument is omitted, Microsoft Excel displays a dialog box and asks you to choose.

载入AddIn方法如下
AddIns.Add(addin_name).installed = True; 将AddIn加入可用列表.
Workbooks.open(AddIn.FullName)方法确保AddIn在当前程序中可用.

AddIns vs COMAddIns
COMAddIn 是 COM add-in的集合 两个加载项是分开哒, 引用的时候得区别.COM add-in 没有第二个集合, 因为不能用Excel直接打开, 只有注册了的组件才能使用. COM add-in 木有name属性,通过ProgID或者Guid引用. Connect属性显示是否当前被加载.


2. Adjustments

这是一个坑爹的设计!专门为自选图像设置调整值,其中每个调整值代表调整柄的调整方向,可是调整柄根本没有规律好么, 一个调整柄可能操纵两个调整值, 版本不同调整值的顺序也会不同,官方给出的结论是先试验一下. 画特殊图形的时候还是手动靠谱点儿.

调整柄类型如下: 一共仨,取值在0-1之间, 默认0.5

Type of adjustment Valid values Linear (horizontal or vertical) Generally the value 0.0 represents the left or top edge of the shape and the value 1.0 represents the right or bottom edge of the shape. Valid values correspond to valid adjustments you can make to the shape manually. For example, if you can only pull an adjustment handle half way across the shape manually, the maximum value for the corresponding adjustment will be 0.5. For shapes such as connectors and callouts, where the values 0.0 and 1.0 represent the limits of the rectangle defined by the starting and ending points of the connector or callout line, negative numbers and numbers greater than 1.0 are valid values. Radial An adjustment value of 1.0 corresponds to the width of the shape. The maximum value is 0.5, or half way across the shape. Angle Values are expressed in degrees. If you specify a value outside the range ? 180 to 180, it will be normalized to be within that range.

3. AllowEditRange( AllowEditRanges)

这个设置可编辑区域哒,是protection对象的属性,代表受保护工作表上可编辑的单元格区域.依稀记得有用可编辑区域设置反选已选内容的案列,说明这个东西是多么不常用.(明明是Word好不好…)

属性:
- Users 返回一个UserAccessList, 可以添加本地和域上的用户名哦.

方法:
- ChangePassword 更改可编辑区域的密码不需要原密码哦,不过只能在工作表未保护情况下,打开受保护工作表输入一次密码就可以编辑整个区域的单元格啦
- Unprotect(Password) 这个功能是解除保护 后面的password参数很莫名啊 需要密码么
- Title 可以给可编辑区域命名哦,不可以和其他可编辑区域重名.
- Add(Title, Range, Password)

Name Required/Optional Data Type Description Title Required String The title of range. Range Required Range Range object. The range allowed to be edited. Password Optional Variant The password for the range.

4. Application

这个太大了 要单列出来写.放在以后吧


5. AutoCorrect

自动更正选项,这是application的子对象,可以用VBA方便的更改设置,尤其是添加删除更正项目,简直不能好用更多

属性:
- AutoExpandListRange 自动扩展列表区域 会自动将粘贴数据加入列表.有时候自动扩展有点恼人
- AutoFillFormulasInLists 只对自动向下填充的列表列起作用
- CapitalizeNamesOfDays 只有星期木有月份哦
- CorrectCapsLock Excel如何判断accidental use呢?
- CorrectSentenceCap 句号结束就是新句子.
- TwoInitialCapitals 这个更正的是手指头粘在Shift键上了
- DisplayAutoCorrectOptions 关闭自动更正选项的显示,对整个Office起效,是有小秘密!
- ReplaceText 打开关闭自动更正选项
- ReplacementList 返回一个二维数组,第一列是被替换项,第二列是替换项

方法:
- AddReplacement(What, Replacement) 添加和更改是同一个方法

Name Required/Optional Data Type Description What Required String The text to be replaced. If this string already exists in the array of AutoCorrect replacements, the existing substitute text is replaced by the new text. Replacement Required String The replacement text.

- DeleteReplacement(What) 删除只需要提供被更正项,不存在会报错.


6. AutoFilter

工作表的自动筛选 不过是只读属性, 生成新筛选还得靠AutoFilter方法. 如果要复制删选需要建立个n行3列的二维数组从原先的AutoFilter对象中取出删选内容.

属性:
- FilterMode 只读, 现实当前自动筛选是否处于筛选状态,如果所有筛选项均关闭 值为- False,与工作表的AutoFilterMode不同, 后者显示/设置工作表中的自动筛选.
- Filters 只读属性 无法更改Filter的内容.
- Range 返回range对象代表当前自动筛选区域
- Sort 返回Sort对象 可以设置排序方法 使用sort.sortfieldes.add方法

方法:
- ApplyFilter 应用筛选 包括对筛选单元格以及排序选项等所做的修改,等于Ribbon上的reapply按钮
- ShowAllData 清除筛选


7. AutoRecover

设置工作表的自动恢复 这个用起来少少的吧

属性:
- Enabled 显示/设置工作簿自动恢复功能
- Path 显示/设置自动恢复文件保存路径
- Time 显示/设置自动恢复文件保存时间间隔,分钟单位

8. Axis(Axes)
坐标轴对象,位于图表中,跟图表有关的对象属性都好多的说.慢慢来吧

属性:
- AxisBetweenCategories 设置XY轴是否交叉,只对xlCategory类型的坐标轴有效,(不包含3D图表) 左侧为true,右侧为false

  • 点状图
    点状图1 点状图2
  • 直方图
    直方图1 直方图2
  • 面积图
    面积图1面积图2

    • AxisGroup 返回坐标轴分组 xlprimary xlSecondary
    • AxisTitle 返回坐标轴标题对象
    • BaseUnit 取值为xlDays xlMonths xlYears 只有在CategoryType为xlTimeScale时生效
    • BaseUnitIsAuto 如果手动设置BaseUnit 则此项变为False
    • Border 返回Border对象,调整坐标轴样式
    • CategoryNames 返回数组或者Range对象,设置类别名称, 更改后会改变引用区域范围
    • CategoryType 返回/设置类别坐标轴类型
Name Value Description xlAutomaticScale -4105 Excel controls the axis type. xlCategoryScale 2 Axis groups data by an arbitrary set of categories. xlTimeScale 3 Axis groups data on a time scale.

- Crosses 设置坐标轴交叉类型 对雷达图及3D图表无效

Constant Meaning xlAxisCrossesAutomatic Microsoft Excel sets the axis crossing point. xlMinimum The axis crosses at the minimum value. xlMaximum The axis crosses at the maximum value. xlAxisCrossesCustom The CrossesAt property specifies the axis crossing point.

- CrossesAt 只对xlValue坐标轴有效,设置其与其他坐标轴截距
- DisplayUnit xlNone xlCustom and below

Name Value Description xlHundredMillions -8 Hundreds of millions. xlHundreds -2 Hundreds. xlHundredThousands -5 Hundreds of thousands. xlMillionMillions -10 Millions of millions. xlMillions -6 Millions. xlTenMillions -7 Tens of millions. xlTenThousands -4 Tens of thousands. xlThousandMillions -9 Thousands of millions. xlThousands -3 Thousands.

- DisplayUnitCustom double类型 DisplayUnit 为xlCustom时指定显示单位
- DisplayUnitLabel 返回DisplayUnitLabel对象
- Format 返回ChartFormat对象
下面几个代表是否显示blabla
- HasDisplayUnitLabel
- HasMajorGridlines
- HasMinorGridlines
- HasTitle
- Height 只读 返回坐标轴高度
- Left 只读 返回坐标轴到绘图区左侧的距离
- LogBase 仅当 xlScaleType 属性设置为 xlScaleLogarithmic时,此属性有效果.
- MajorGridlines 返回主网格对象
- MajorTickMark Tick Mark 的位置

Name Value Description xlTickMarkCross 4 Crosses the axis xlTickMarkInside 2 Inside the axis xlTickMarkNone -4142 No mark xlTickMarkOutside 3 Outside the axis

- MajorUnit double
- MajorUnitIsAuto
- MajorUnitScale 在xlTimeScale取值
- MaximumScale 坐标轴最大值
- MaximumScaleIsAuto
- MinimumScale
- MinimumScaleIsAuto
- MinorGridlines
- MinorTickMark
- MinorUnit
- MinorUnitIsAuto
- MinorUnitScale
- ReversePlotOrder 是否反转绘图
- ScaleType 设置线性还是对数增长 xlScaleLinear xlScaleLogarithmic
- TickLabelPosition Tick label的位置不能随意拖拽

Name Value Description xlTickLabelPositionHigh -4127 Top or right side of the chart. xlTickLabelPositionLow -4134 Bottom or left side of the chart. xlTickLabelPositionNextToAxis 4 Next to axis (where axis is not at either side of the chart). xlTickLabelPositionNone -4142 No tick marks.

- TickLabels 返回TickLabels对象
- TickLabelSpacing 取值在1 to31999
- TickLabelSpacingIsAuto
- TickMarkSpacing
- Top
- Type xlSeries变成了xlSeriesAxis

Name Value Description xlCategory 1 Axis displays categories. xlSeriesAxis 3 Axis displays data series. xlValue 2 Axis displays values.

- Width

方法:
- Delete
- Select


9. AxisTitle

表示坐标轴标题,此对象只有在坐标轴的HasTitle属性为真是才存在. Legend ChartTitle 什么的都差不多啊 设置起来好麻烦的说

属性:
- Caption 坐标轴标题文本
- Characters(Start, Length) 返回标题内指定字符串
- Format 返回ChartFormat对象
- Formula 设置公式,引用必须在当前工作表之外
- FormulaLocal
- FormulaR1C1
- FormulaR1C1Local
- Height 返回高度值 (只读) 高度和宽度大小只能通过textframe2来了
- HorizontalAlignment 取值为xlCenter xlDistributed xlJustify xlLeft xlRight
- IncludeInLayout 决定坐标轴标题是否占用图表布局空间,默认为真 false 的时候图表会变大 改回true又变小
- Left 返回坐标轴标题距离图表左缘的距离
-Name 返回内部名词 (只读)
-Orientation 返回标题角度-90到90 或者xlDownward = -90 xlHorizontal xlUpward = 90 xlVertical 之一

Position Name Value Description xlChartElementPositionAutomatic -4105 Automatically sets the position of the chart element. xlChartElementPositionCustom -4114 Specifies a specific position for the chart element.

- ReadingOrder 取值xlRTL xlLTR xlContext 试验了下 没发现有啥区别
- Shadow 设置是否显示阴影, 阴影就是右边和下面变成黑色,这是为了兼容哪个古老版本留下来的属性啊
- Text 和Caption一样的啦
- Top 返回坐标轴标题距离图表上缘的距离
- VerticalAlignment 取值 xlBottom xlCenter xlDistributed xlJustify xlTop
- Width 返回坐标轴标题宽度 (只读)

方法:
- Delete
- Select


10. Border(Borders)

Border和Borders 返回对象的边框对象,边框统一样式的使用Border对象,边框区分上下左右的使用Borders对象.

属性:
- Color 使用RGB函数设定
- ColorIndex 色表中1到56的颜色,不同系统中设置可能不完全匹配 或者xlColorIndexAutomatic xlColorIndexNone
- LineStyle xlGray25, xlGray50, xlGray75, or xlAutomatic (xlDouble andxlSlantDashDot are not usable with chart)

Name Value Description xlContinuous 1 Continuous line. xlDash -4115 Dashed line. xlDashDot 4 Alternating dashes and dots. xlDashDotDot 5 Dash followed by two dots. xlDot -4118 Dotted line. xlDouble -4119 Double line. xlLineStyleNone -4142 No line. xlSlantDashDot 13 Slanted dashes.

- ThemeColor 设置Color Scheme中制定对象的颜色
- TintAndShade single value调整颜色的明度, 取值在-1至1 默认为0
- Weight 返回border粗细,不能指定啊 残念だよ

Name Value Description xlHairline 1 Hairline (thinnest border). xlMedium -4138 Medium. xlThick 4 Thick (widest border). xlThin 2 Thin.

only for Borders
- Value another name for LineStyle

第一篇到此完成 写两天 挨个属性和方法在VBA里试一遍, 主要参考StackOverflow和MSDN.
排版好丑啊 ╮( ̄▽ ̄”)╭

0 0
原创粉丝点击