ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: lsc900707

[分享] 见证成长历程---我的答疑解难代码汇总

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2017-1-21 18:20 | 显示全部楼层
  1. myCell.Interior.Color = Application.WorksheetFunction.RandBetween(0, 16777216)
复制代码

这样设置颜色不会出错吗?怎么我记得color颜色值并不是任何数值都可以的,即使在代码中随机数的范围之内?

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-25 10:03 | 显示全部楼层
xlsx文件批量转换为csv:
Sub lsc()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim wb As Workbook
    file = Application.GetOpenFilename(MultiSelect:=True)
    For i = LBound(file) To UBound(file)
        Workbooks.Open Filename:=file(i)
        Set wb = ActiveWorkbook
        Path = wb.Path
        On Error Resume Next
        VBA.MkDir (Path & "\csv")
        With wb
            .SaveAs Path & "\csv\" & Replace(wb.Name, ".xlsx", ".csv"), xlCSV
            .Close True
        End With
    Next
    MsgBox "已转换了" & (i - 1) & "个文档"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-29 14:11 | 显示全部楼层
本帖最后由 lsc900707 于 2017-1-29 14:20 编辑

这个位置留给积分突破5000分纪念:
JL}{}712Y3[1(VFD8O_%IZ9.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-31 10:16 | 显示全部楼层
本帖最后由 lsc900707 于 2017-1-31 10:20 编辑

一次性根据编码更新的内容更新工作薄中多个工作表相同编码的描单价、描述与费率
http://club.excelhome.net/thread-1325960-1-1.html
(出处: ExcelHome技术论坛)

sub lsc()
    Dim i&, j&, arr, d, sht As Worksheet
    Set d = CreateObject("scripting.dictionary")
    With Sheets("要更新的内容")
        r = .Cells(Rows.Count, 1).End(3).Row
        arr = .Range("A1:d" & r)
        For i = 2 To UBound(arr)
            For Each sht In Worksheets
                If sht.Name <> "要更新的内容" Then
                    With sht
                        For j = 2 To .Cells(Rows.Count, 1).End(3).Row
                            If .Cells(j, 1) = arr(i, 1) Then
                                 .Range("A" & j).Resize(1, UBound(arr, 2)) = WorksheetFunction.Index(arr, i)
                            End If
                        Next
                    End With
                End If
            Next
        Next
    End With
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-1 11:48 | 显示全部楼层
本帖最后由 lsc900707 于 2017-2-1 11:49 编辑

Sub 打印多个隐藏的工作表()
Dim sht As Worksheet
Dim CurStat As Variant
For Each sht In ActiveWorkbook.Worksheets
     If Not sht.Visible Then
          CurStat = sht.Visible
          sht.Visible = xlSheetVisible
          sht.PrintOut
         sht.Visible = CurStat
   End If
Next
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 16:59 | 显示全部楼层
本帖最后由 lsc900707 于 2017-2-3 17:09 编辑

如何实现按条件自动隐藏行?
http://club.excelhome.net/thread-1326145-1-1.html
(出处: ExcelHome技术论坛)

Sub 隐藏空表()
     For Each sht In Sheets
         With sht
                If .Name <> "录入数据" And .Name <> "系统参数" Then
                     .Rows.Hidden = False
                     For m = 1 To .UsedRange.Cells(Rows.Count, 3).End(xlUp).Row
                         If .Cells(m, 3) = "隐藏" Or .Cells(m, 4) = "隐藏" Then
                              .Cells(m, 3).EntireRow.Hidden = True
                              .Cells(m, 4).EntireRow.Hidden = True
                         End If
                   Next
               End If
         End With
    Next
    MsgBox "报表已生成"
End Sub
Sub 全部显示()
    For Each sht In Sheets
        With sht
            If .Name <> "录入数据" And .Name <> "系统参数" Then
                .Rows.Hidden = False
            End If
        End With
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 17:04 | 显示全部楼层
本帖最后由 lsc900707 于 2017-2-3 17:06 编辑

老师们帮忙看看 自己试着编写了一下 懵逼 就是不行
http://club.excelhome.net/thread-1326188-1-1.html
(出处: ExcelHome技术论坛)

按钮放工作表“1”里:
Sub lsc()
     Dim arr
     arr = Sheets("Sheet2").[a1].CurrentRegion.Value
     For i = 2 To UBound(arr)
         If arr(i, 3) <> 0 Then k = k + 1
         For j = 1 To UBound(arr, 2)
             arr(k, j) = arr(i, j)
        Next
     Next
     [K39:N200].ClearContents
     [K39].Resize(k, UBound(arr, 2)) = arr
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 16:14 | 显示全部楼层
本帖最后由 lsc900707 于 2017-2-8 16:18 编辑

一个按条件显示隐藏工作表的实例:
如何按条件自动显示或隐藏EXCEL表格
http://club.excelhome.net/thread-1326970-1-1.html
(出处: ExcelHome技术论坛)


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For i = 3 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) <> "" And Cells(i, 3) <> "" Then
             For Each sht In Sheets
                   If sht.Name <> "企业所得税年度纳税申报填报表单" Then
                        If Cells(i, 1) = Left(sht.Name, 7) And Cells(i, 3) = "是" Then
                              sht.Visible = -1
                        End If
                       If Cells(i, 1) = Left(sht.Name, 7) And Cells(i, 3) = "否" Then
                            sht.Visible = 2
                       End If
                  End If
             Next
        End If
   Next
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 21:22 | 显示全部楼层
本帖最后由 lsc900707 于 2017-2-12 21:28 编辑

麻烦修改代码
http://club.excelhome.net/thread-1327448-1-1.html
(出处: ExcelHome技术论坛)


Sub 矩形1_单击()
     a = [v1]: b = [v2]: c = [x1]: d = [x2]
     For j = 2 To 4 Step 2
          For i = 8 To [b65536].End(3).Row - 1
              If Cells(i, j) = a And Cells(i + 1, j) = b And Cells(i, j + 2) = c And Cells(i + 1, j + 2) = d Then
                    Cells(i, j).Interior.ColorIndex = 6
                    Cells(i + 1, j).Interior.ColorIndex = 6
                    Cells(i, j + 2).Interior.ColorIndex = 6
                    Cells(i + 1, j + 2).Interior.ColorIndex = 6
              End If
         Next
    Next
End Sub
Sub 矩形2_单击()
     a = [v1]: b = [v2]: c = [w1]: d = [x2]
     For j = 2 To 4
         For i = 8 To [b65536].End(3).Row - 1
            If Cells(i, j) = a And Cells(i + 1, j) = b And Cells(i, j + 1) = c And Cells(i + 1, j + 2) = d Then
                Cells(i, j).Interior.ColorIndex = 6
                Cells(i + 1, j).Interior.ColorIndex = 6
                Cells(i, j + 1).Interior.ColorIndex = 6
                Cells(i + 1, j + 2).Interior.ColorIndex = 6
            End If
         Next
     Next
End Sub


评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 21:31 | 显示全部楼层
多条件区域删除重复行
http://club.excelhome.net/thread-1327694-1-1.html
(出处: ExcelHome技术论坛)

Sub lsc()
     Dim d As Object, Rng As Range
     Set d = CreateObject("scripting.dictionary")
     With Sheet1
         arr = .[a1].CurrentRegion
         For i = 2 To UBound(arr)
             s = arr(i, 2) & "," & arr(i, 3) & "," & arr(i, 4) & "," & arr(i, 5) _
             & "," & arr(i, 6) & "," & arr(i, 7) & "," & arr(i, 8) & "," & arr(i, 9) & "," & arr(i, 10)
             If Not d.exists(s) Then
                 d(s) = ""
             Else
                 If Rng Is Nothing Then
                     Set Rng = .Rows(i)
                 Else
                     Set Rng = Union(Rng, .Rows(i))
                 End If
             End If
         Next
         If Not Rng Is Nothing Then Rng.Delete
     End With
End Sub

评分

3

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-26 11:33 , Processed in 0.042071 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

快速回复 返回顶部 返回列表