|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
刚刚开始学vsto,可以编一些小程序(虽然类啊,对象啊.class啊,继承啊什么的还不懂.而这些又是vsto强大之处...)
把这两天回复的关于vsto的程序整理到一个帖子,或许对其他人有一点点点点(此处省略无穷个点...)用,高手勿喷(所谓闻道有先后是也)
1: texttocolumns:
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=906742&pid=6481294
源码:
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim endrow As Integer
- Dim str As String
- endrow = Me.Range("a65536").End(Excel.XlDirection.xlUp).Row
- Range("a1:a" & endrow).TextToColumns(Range("c1"), Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote, , , , True)
- End Sub
复制代码 2: http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=910633&pid=6481278
源码:
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim endrow As Integer
- Dim str As String
- endrow = Me.Range("a65536").End(Excel.XlDirection.xlUp).Row
- For i As Integer = 1 To endrow
- For j As Integer = 1 To Len(Range("a" & i).Value)
- str &= "!" & Mid(Range("a" & i).Value, j, 1)
- Next
- Range("a" & i).Value = str
- str = ""
- Next
- End Sub
复制代码 3: 字典 http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=919100&pid=6481268
源码:
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim rng As Excel.Range = Me.Range("a1")
- Dim arr As Array
- arr = Split(rng.Value, " ")
- Dim rng2 As Excel.Range = Me.Range("a2")
- Dim brr() As String
- brr = Split(rng2.Value, " ")
- Array.Sort(arr)
- For i = 0 To UBound(brr)
- If Array.BinarySearch(arr, brr(i)) >= 0 Then
- rng2.Characters(InStr(rng2.Value, brr(i)), brr(i).Length).Font.ColorIndex = Int(Rnd() * 20)
- End If
- Next
- End Sub
复制代码 5: 依然是 array.binarysearch
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=901951&pid=6481127
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim rng As Excel.Range = Me.Range("a1")
- Dim arr As Array
- arr = Split(rng.Value, " ")
- Dim rng2 As Excel.Range = Me.Range("a2")
- Dim brr() As String
- brr = Split(rng2.Value, " ")
- Array.Sort(arr)
- For i = 0 To UBound(brr)
- If Array.BinarySearch(arr, brr(i)) >= 0 Then
- rng2.Characters(InStr(rng2.Value, brr(i)), brr(i).Length).Font.ColorIndex = Int(Rnd() * 20)
- End If
- Next
- End Sub
复制代码
6: 在vba中,为避免反复读取单元格值降低运行速度,会将单元格区域赋值给数值.
而在vsto中,同样是这样子的,赋值方式差不多一样
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=945411&pid=6480689
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim endrow As Integer
- Dim sht As Excel.Worksheet = Me.Application.ActiveWorkbook.Sheets(1)
- endrow = sht.Range("a65536").End(Excel.XlDirection.xlUp).Row
- Dim soucerng As Excel.Range = sht.Range("a1:c" & endrow)
- Dim arr
- arr = soucerng.Value
- Dim find As String = Me.Range("a2").Value
- For i As Integer = 1 To endrow
- If arr(i, 1) = find Then
- Me.Range("b2").Value = arr(i, 2)
- Me.Range("c2").Value = arr(i, 3)
- Exit For
- End If
- Next
- End Sub
复制代码
7: 删除工作表(array.binarysearch)
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=822255&pid=6480633
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim arr As Array = Array.CreateInstance(GetType(String), 2)
- Me.Application.DisplayAlerts = False
- arr.SetValue("Sheet1", 0)
- arr.SetValue("Sheet2", 1)
- For Each ws As Excel.Worksheet In Me.Application.ActiveWorkbook.Worksheets
- If Array.BinarySearch(arr, ws.Name) < 0 Then
- ws.Delete()
- End If
- Next
- Me.Application.DisplayAlerts = True
- End Sub
复制代码
8: 设置单元格背景色
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=945302&pid=6479748
- Private Sub Sheet1_SelectionChange(ByVal Target As Microsoft.Office.Interop.Excel.Range) Handles Me.SelectionChange
- If Left(Target.Address, 2) <> "$B" Or Len(Target.Value) > 0 Then
- Target.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone
- Else
- Target.Interior.ColorIndex = 12
- End If
- End Sub
复制代码
9: vsto操作文本文件(filesystem)
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=945274&pid=6479664
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim writer = My.Computer.FileSystem.OpenTextFileWriter("e:/123.txt", True)
- For i As Integer = 1 To 10
- For j As Integer = 1 To 5
- writer.WriteLine(Cells(i, j).value)
- Next
- Next
- writer.Close()
- End Sub
复制代码
10: 数据类型对运算速度的影响:
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=945074&pid=6478147
- 相同一程序:
- 以下为测试结果:
- 改为single(占4个字节):7秒
- 改为double(8个字节):17秒
- 改为dicimal(16个字节):37秒
- 改为integer(4个字节):7秒
- 改为short(2个字节):3秒
- 看来数据类型在vb.net中对运算速度有巨大影响!
复制代码
11:还是字典,刚编此程序时不知道如何将dim arr as array=array.createinstance(gettype(integer)),dic.count) , dic.keys.copyto (arr,0)此用法,用的是循环
http://club.excelhome.net/forum.php?mod=redirect&goto=findpost&ptid=943863&pid=6473273
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim dic As New Dictionary(Of Integer, Integer)
- Dim kk As Integer
- Dim brr()
- Dim crr()
- For i = 1 To 16
- If dic.ContainsKey(Me.Range("a" & i).Value) Then
- dic(Me.Range("a" & i).Value) = dic(Me.Range("a" & i).Value) + Range("b" & i).Value
- Else
- dic(Me.Range("a" & i).Value) = Me.Range("b" & i).Value
- End If
- Next
- Dim keys As Dictionary(Of Integer, Integer).KeyCollection = dic.Keys
- For Each s As Integer In keys
- kk = kk + 1
- ReDim Preserve brr(kk - 1)
- brr(kk - 1) = s
- Next
- MsgBox(Join(brr, ","))
- kk = 0
- Dim values As Dictionary(Of Integer, Integer).ValueCollection = dic.Values
- For Each s As Integer In values
- kk = kk + 1
- ReDim Preserve crr(kk - 1)
- crr(kk - 1) = s
- Next
- MsgBox(Join(crr, ","))
- End Sub
复制代码
12: 待续
|
|