ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索

一些常用的资料总结(遍历文件,最大行列,选择整行或者整列)

已有 1155 次阅读2013-11-21 16:48 |个人分类:自我学习总结| 文件夹, 针对性, 资料

QQ:1302084032,可以加我或者QQ群:281250721


感觉平时先对比较常用,但是不容易记住的内容整理了一小部分,以后会有针对性的整理,希望与大家分享

一.自动搜索文件夹下的文件名并保存

1.搜索当前文件夹下的文件名

Sub test() '读取当前文件夹下的文件名,并保存在表格内

    a = 1

    Set fso = CreateObject("scripting.filesystemobject")

    Set ff = fso.getfolder(ThisWorkbook.Path)

    For Each f In ff.Files

        If f.Name <> ThisWorkbook.Name Then

            'Workbooks.Open f '打开搜索到的文件

            Sheets(1).Cells(a, 1) = f.Name

            a = a + 1

        End If

    Next f

End Sub

2.搜索当前文件夹的下级文件夹里的文件名

Sub bbb() '修改之后的,读取下一级文件夹下的文件名

    a = 1

    Dim arr, MyPath$, MyName$, Fol

    MyPath = ThisWorkbook.Path

    For Each Fol In CreateObject("scripting.filesystemobject").getfolder(MyPath).subfolders

        For Each f In Fol.Files

            Sheets(2).Cells(a, 1) = f.Name

            a = a + 1

        Next f

    Next Fol

End Sub

二.选择对应区域的行或者列

1、选择对应区域的行

    Range("a1:a10").EntireRow.Select

    Cells(11, 1).EntireRow.Select

2、选择对应区域的列

    Range("a1:a10").EntireColumn.Select

    Cells(11, 1).EntireColumn.Select

三.返回非空行的最大行号和返回最大非空列的列号

1.返回非空行的最大行号

Cells(Rows.Count, 1).End(3).Row

Cells(Rows.Count, 1).End(xlUp).Row

2. 返回非空列的最大列号

Cells(1, Columns.Count).End(1).Column

Cells(1, Columns.Count).End(xlToLeft).Column

四.Checkboxes的返回对应的单元格地址,使用频率不是很大,但是关于checkboxes用处还是比较多的

Checkboxesvalue属性是返回是否被选中,text属性是指checkboxes的名称。以下代码是返回被选中的checkboxes对应的单元格地址。

Sub Macro1()

    Dim i As Byte

    With ActiveSheet

        For i = 1 To 4

            If .CheckBoxes(i).Value = 1 Then MsgBox .CheckBoxes(i).TopLeftCell.Address

        Next

    End With

End Sub

 

表格事件处理,第二列单元格有变化,在其他表内查找数据


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count <> 1 Then Exit Sub
If TypeName(Target) <> "Range" Then Exit Sub
Dim rng As Range
If Len(Target.Value) = 0 Then Exit Sub
If Target.Column = 2 And Target.Row > 1 Then
Application.ScreenUpdating = False
l1:
Set rng = Sheets("送货库存").Columns(2).Find(Target.Value, lookat:=xlWhole)
If Not rng Is Nothing Then
rng.EntireRow.Delete
GoTo l1
End If
Application.ScreenUpdating = True
End If
End Sub
 
 
用这个Sheet1.ScrollArea = "$B$1:$IV$65536",让光标选不中A列
 
批量修改属性值
Private Sub CheckBox1_Change()
    For i = 2 To 5
        Me.OLEObjects("CheckBox" & i).Object.Value = CheckBox1.Value
    Next
End Sub
Private Sub CheckBox6_Change()
    For i = 7 To 10
        Me.OLEObjects("CheckBox" & i).Object.Value = CheckBox6.Value
    Next
End Sub
 
 
遍历表格内的控件并获取属性内容。
'引用TypeLib Information
Sub listcontrols()
    Cells(1, 1) = "控件"
    Cells(1, 2) = "属性,方法,事件"
    Cells(1, 3) = "值"
    [a1:c1].Interior.ColorIndex = 3
    On Error Resume Next
    Dim ctl As Object, n As Long
        n = 1
        For Each ctl In Sheet1.OLEObjects
        Dim iInf As InterfaceInfo
        Set iInf = InterfaceInfoFromObject(ctl)
        If Not (iInf Is Nothing) Then
        Dim mem As MemberInfo
        For Each mem In iInf.Members
        If mem.InvokeKind And INVOKE_PROPERTYGET Then
            n = n + 1
            Cells(n, 1) = ctl.Name
            Cells(n, 2) = mem.Name
            Cells(n, 3) = CallByName(ctl, mem.Name, VbGet)
        End If
        Next
        End If
        Next
    [a1:c65536].Columns.AutoFit
End Sub

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 免费注册

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-28 18:51 , Processed in 0.032242 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

返回顶部