Excel精英培训网

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

[已解决]求VBA代码

[复制链接]
发表于 2012-8-17 21:14 | 显示全部楼层 |阅读模式
如何编写VBA代码实现附表中的效果呀?请高手指教谢谢各位大侠了!
最佳答案
2012-8-17 21:30
Private Sub CommandButton1_Click()
Dim i%, r%
m = 1: n = 1
For r = 2 To 4 Step 2
For i = 2 To 5
If Cells(i, r) = "×" Then
m = m + 1
Cells(m, 6) = Cells(i, r - 1)
Cells(m, 7) = Cells(i, r)
Else
n = n + 1
Cells(n, 8) = Cells(i, r - 1)
Cells(n, 9) = Cells(i, r)
End If
Next i
Next r
End Sub

求代码.rar

2.06 KB, 下载次数: 62

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-8-17 21:30 | 显示全部楼层    本楼为最佳答案   
Private Sub CommandButton1_Click()
Dim i%, r%
m = 1: n = 1
For r = 2 To 4 Step 2
For i = 2 To 5
If Cells(i, r) = "×" Then
m = m + 1
Cells(m, 6) = Cells(i, r - 1)
Cells(m, 7) = Cells(i, r)
Else
n = n + 1
Cells(n, 8) = Cells(i, r - 1)
Cells(n, 9) = Cells(i, r)
End If
Next i
Next r
End Sub

新建 Microsoft Excel 工作表.rar

8.64 KB, 下载次数: 49

回复

使用道具 举报

 楼主| 发表于 2012-8-18 19:33 | 显示全部楼层
不错谢了!
我也编了一个供大家参考
Sub test3()
Dim rg As Range
m = 1
n = 1
For Each rg In Range("b2:b" & Range("b65536").End(3).Row, "d2:d" & Range("d65536").End(3).Row)
   If rg = "×" Then
   m = m + 1
Cells(m, 7) = rg.Text
Cells(m, 6) = rg.Offset(0, -1).Text
Cells(1, 6) = "代码"
Cells(1, 7) = "状态"
ElseIf rg = "√" Then
  n = n + 1
Cells(n, 9) = rg.Text
Cells(n, 8) = rg.Offset(0, -1).Text
Cells(1, 8) = "代码"
Cells(1, 9) = "状态"
   End If
  Next rg
End Sub
回复

使用道具 举报

 楼主| 发表于 2012-8-18 19:47 | 显示全部楼层
怎么把代码放到excel对象中而不是模块中,并建立与commandButton之间的联系?
回复

使用道具 举报

 楼主| 发表于 2012-8-18 21:28 | 显示全部楼层
怎么把代码放到excel对象中而不是模块中,并建立与commandButton之间的联系?
回复

使用道具 举报

发表于 2012-8-30 15:23 | 显示全部楼层
回复

使用道具 举报

发表于 2013-6-28 14:08 | 显示全部楼层
学习
回复

使用道具 举报

发表于 2013-11-11 08:22 | 显示全部楼层
回复

使用道具 举报

发表于 2016-6-4 07:53 | 显示全部楼层
学习了,谢谢各位老师
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 15:44 , Processed in 0.249926 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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