Excel VBA常用代码总结1
改变背景
Range("A1").Interior.ColorIndex = xlNone
 ColorIndex一览
改变文字颜
Range("A1").Font.ColorIndex = 1
获取单元格
Cells(1, 2)
Range("H7")
获取范围
Range(Cells(2, 3), Cells(4, 5))
Range("a1:c3")
'用快捷记号引用单元格
Worksheets("Sheet1").[A1:B5]
选中某sheet
SetNewSheet = Sheets("sheet1")
NewSheet.Select
选中或激活某单元格
'“Range”对象的的Select方法可以选择一个或多个单元格,而Activate方法可以指定某一个单元格为活动单元格。
'下面的代码首先选择A1:E10区域,同时激活D4单元格:
Range("a1:e10").Select
Range("d4:e5").Activate
'而对于下面的代码:
Range("a1:e10").Select
Range("f11:g15").Activate
'由于区域A1:E10和F11:G15没有公共区域,将最终选择F11:G15,并激活F11单元格。
获得文档的路径和文件名
ActiveWorkbook.Path    '路徑
ActiveWorkbook.Name   '名稱
ActiveWorkbook.FullName  '路徑+名稱
'或将ActiveWorkbook换成thisworkbook
隐藏文档
Application.Visible = False
禁止屏幕更新
Application.ScreenUpdating = False
禁止显示提示和警告消息
Application.DisplayAlerts = False
文件夹做成
strPath = "C:\temp\"
MkDirstrPath
状态栏文字表示
Application.StatusBar = "计算中"
双击单元格内容变换
PrivateSubWorksheet_BeforeDoubleClick(ByVal Target As Range, Cancel AsBoolean)
If  (Target.Cells.Row>= 5AndTarget.Cells.Row<= 8) Then
IfTarget.Cells.Value = "●"Then
Target.Cells.Value = ""
Else
Target.Cells.Value = "●"
EndIf
        Cancel = True
EndIf
End Sub
文件夹选择框方法1
SetobjShell = CreateObject("Shell.Application")
SetobjFolder = objShell.BrowseForFolder(0, "文件", 0, 0)
IfNotobjFolderIsNothing
Then path= objFolder.self.Path&"\"
endif
SetobjFolder = Nothing
SetobjShell = Nothing
文件夹选择框方法2(推荐)
PublicFunctionChooseFolder() AsString
DimdlgOpenAsFileDialog
SetdlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
WithdlgOpen
    .InitialFileName = ThisWorkbook.path&"\"
If .Show = -1Then
ChooseFolder = .SelectedItems(1)
EndIf
EndWith
SetdlgOpen = Nothing
End Function
'使用方法例:
Dim path AsString
path = ChooseFolder()
If path <>""Then
MsgBox"open folder"
EndIf
文件选择框方法
PublicFunctionChooseOneFile(OptionalTitleStrAsString = "Please choose a file", OptionalTypesDecAsString = "*.*", OptionalExtenAsString = "*.*") AsString
DimdlgOpenAsFileDialog
SetdlgOpen = Application.FileDialog(msoFileDialogFilePicker)
WithdlgOpen
          .Title = TitleStr
          .Filters.Clear
          .Filters.AddTypesDec, Exten
          .AllowMultiSelect = False
          .InitialFileName = ThisWorkbook.Path
If .Show = -1Then
'  .AllowMultiSelect  =  True             
'  For  Each  vrtSelectedItemIn  .SelectedItems
'  MsgBox  "Path  name:  "  &vrtSelectedItem
'  Next  vrtSelectedItem
ChooseOneFile = .SelectedItems(1)           
EndIf
EndWith
SetdlgOpen = Nothing
End Function
某列到关键字为止循环方法1(假设关键字是end)
SetCurrentCell = Range("A1")
DoWhileCurrentCell.Value<>"end"
……
vba 字符串函数SetCurrentCell = CurrentCell.Offset(1, 0)
Loop
某列到关键字为止循环方法2(假设关键字是空字符串)
i = StartRow
DoWhileCells(i, 1) <>""
……
i = i + 1
Loop
"Next 循环(知道确切边界)
ForEach c InWorksheets("Sheet1").Range("A1:D10").Cells
  IfAbs(c.Value) <0.01Thenc.Value = 0
Next
"Next 循环(不知道确切边界),在活动单元格周围的区域内循环
ForEach c InActiveCell.CurrentRegion.Cells
IfAbs(c.Value) <0.01Thenc.Value = 0
Next

 
某列有数据的最末行的行数的取得(中间不能有空行)
lonRow=1
DoWhileTrim(Cells(lonRow, ).Value) <>""
lonRow = lonRow + 1
Loop
lonRow11 = lonRow11 - 1
A列有数据的最末行的行数的取得另一种方法
Range("A65536").End(xlUp).Row
将文字复制到剪贴板
DimMyDataAsDataObject
SetMyData = NewDataObject
MyData.SetTextRange("H7").Value
MyData.PutInClipboard
取得路径中的文件名
PrivateFunctionGetFileName(ByVal s AsString)
Dimsname() AsString
sname = Split(s, "\")
GetFileName = sname(UBound(sname))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。