Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 7852|回复: 19

[已解决]能不能把代码改成数组?

[复制链接]
发表于 2014-4-8 08:41 | 显示全部楼层 |阅读模式
Sub ABC()
    Dim i As Integer
    Application.ScreenUpdating = False
    For i = Cells(2, Columns.Count).End(xlToLeft).Column To 1 Step -1
        Select Case Cells(2, i).Value
            Case "dd", "kk", "aa", "hh"
            Case Else
                Columns(i).Delete
        End Select
    Next
    Application.ScreenUpdating = True
End Sub

Sub DEF()
    For i = [IV2].End(xlToLeft).Column To 1 Step -1
        If InStr("|aa|hh|kk|dd|", "|" & Cells(2, i) & "|") = 0 Then
            Columns(i).Delete
        End If
    Next
End Sub
最佳答案
2014-4-23 17:01
  1. Sub Macro1()
  2. Dim arr, brr, i&, j%, s%
  3. arr = Range("a2").CurrentRegion
  4. Range("a2").CurrentRegion.Clear
  5. ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2))
  6. For j = 1 To UBound(arr, 2)
  7.     If arr(1, j) <> "dd" And arr(1, j) <> "kk" Then
  8.         s = s + 1
  9.         For i = 1 To UBound(arr)
  10.             brr(i, s) = arr(i, j)
  11.         Next
  12.     End If
  13. Next
  14. Range("a2").Resize(UBound(brr), s) = brr
  15. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-4-8 08:57 | 显示全部楼层
回复

使用道具 举报

发表于 2014-4-8 11:08 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2014-4-20 20:00 | 显示全部楼层
Sub abc1()
    Dim i, ar(1 To 256), n
    For i = 1 To Cells(2, Columns.Count).End(xlToLeft).Column
        If InStr("dd,kk", Cells(2, i)) = False Then
            n = n + 1
            ar(n) = Cells(2, i)
        End If
    Next
    [2:2] = ""
    [a2].Resize(1, n) = ar
End Sub

Sub abc2()
    Dim ar, br(), i As Integer
    ar = Cells(2, Cells(2, Columns.Count).End(xlToLeft).Column)
    ReDim br(1 To 1, 1 To UBound(ar, 2))
    For i = 1 To UBound(ar, 2)
        If ar(1, i) = "dd" Or ar(1, i) = "kk" Then
            j = j + 1
            br(1, j) = ar(1, i)
        End If
    Next
    [a2].Resize(, 99) = ""
    [a2].Resize(, j) = br
End Sub
回复

使用道具 举报

发表于 2014-4-21 10:06 | 显示全部楼层
没附件,看代码猜题意,费劲!
回复

使用道具 举报

 楼主| 发表于 2014-4-21 10:56 | 显示全部楼层
su45 发表于 2014-4-21 10:06
没附件,看代码猜题意,费劲!

第二行标题有dd aa hh ff kk等,只要标题不是dd和kk,就把该标题所在的列删除
附件: 附件.rar (1.89 KB, 下载次数: 18)
回复

使用道具 举报

发表于 2014-4-22 00:14 | 显示全部楼层
sub ddkk()
ar = sheet1.usedrange
redim br(1 To ubound(ar, 2))
for i = 1 To ubound(br)
    if ar(1, i) <> "dd" and ar(1, i) <> "kk" then
        n = n + 1
        br(n) = application.index(ar, 0, i)
    end if
next
for j = 1 To n
    sheet1.cells(2, j + 5).resize(ubound(ar), 1) = br(j)
next
end sub
试一试哈,行不行不知道

评分

参与人数 1 +1 收起 理由
hadha + 1 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-4-22 11:09 | 显示全部楼层
pengyx 发表于 2014-4-22 00:14
sub ddkk()
ar = sheet1.usedrange
redim br(1 To ubound(ar, 2))

sub ddkk()
ar = sheet1.usedrange
redim br(1 To ubound(ar, 2))
for i = 1 To ubound(br)
    if ar(1, i) <> "dd" and ar(1, i) <> "kk" then
        n = n + 1
        br(n) = application.index(ar, 0, i)
    end if
next
for j = 1 To n
    sheet1.cells(2, j + 5).resize(ubound(ar), 1) = br(j)
next
end sub
回复

使用道具 举报

 楼主| 发表于 2014-4-22 11:10 | 显示全部楼层
pengyx 发表于 2014-4-22 00:14
sub ddkk()
ar = sheet1.usedrange
redim br(1 To ubound(ar, 2))

应该是删除,不是添加复制
回复

使用道具 举报

发表于 2014-4-23 12:13 | 显示全部楼层
hadha 发表于 2014-4-22 11:10
应该是删除,不是添加复制

看一下,就是这个效果吧


附件.rar

8.86 KB, 下载次数: 3

回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 13:35 , Processed in 0.625091 second(s), 16 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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