Excel精英培训网

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

[已解决]这VBA代码能精减吗?

[复制链接]
发表于 2016-4-20 14:32 | 显示全部楼层 |阅读模式
本帖最后由 h284124405 于 2016-4-20 15:08 编辑
  1.      If i > 4 And i <= 20 Then
  2.       If Cells(i,  8) = "一出二" Then
  3.          Cells(i,  9) = "1"
  4.          Cells(i,  10) = "150"
  5.   ElseIf Cells(i,  4) = "φ280" Then
  6.          Cells(i,  9) = "1"
  7.          Cells(i,  10) = "150"
  8.   ElseIf Cells(i,  4) = "φ350" Then
  9.          Cells(i,  9) = "1"
  10.          Cells(i,  10) = "150"
  11.   ElseIf Cells(i,  4) = "φ380" Then
  12.          Cells(i,  9) = "1"
  13.          Cells(i,  10) = "175"
  14.   ElseIf Cells(i,  4) = "φ379" Then
  15.          Cells(i,  9) = "1"
  16.          Cells(i,  10) = "175"
  17.   ElseIf Cells(i,  4) = "φ450" Then
  18.          Cells(i,  9) = "1"
  19.          Cells(i,  10) = "320"
  20.      End If
复制代码
最佳答案
2016-4-20 15:48
  1. If i > 4 And i <= 20 Then
  2.         tmp = Cells(i, 4): Cells(i, 9) = "1"
  3.         Cells(i, 10) = IIf(Cells(i, 8) = "一出二" Or tmp = "φ280" Or tmp = "φ350", "150", _
  4.                        IIf(tmp = "φ380" Or tmp = "φ379", "175", IIf(tmp = "φ450", "320", "")))
  5.      End If
复制代码
发表于 2016-4-20 14:57 | 显示全部楼层
本帖最后由 gufengaoyue 于 2016-4-20 14:58 编辑

这样?
  1. If i > 4 And i <= 20 Then
  2.   Cells(i, mr + 9) = "1"
  3.   TMP = Cells(i, mr + 4)
  4.   If TMP = "φ280" OR TMP = "φ350" or Cells(i, mr + 8) = "一出二" Then
  5.          Cells(i, mr + 10) = "150"
  6.   ElseIf TMP = "φ380" or TMP = "φ379" Then
  7.          Cells(i, mr + 10) = "175"
  8.   ElseIf TMP = "φ450" Then
  9.          Cells(i, mr + 10) = "320"
  10.   End If
  11. End If
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-4-20 15:10 | 显示全部楼层
  1. If i > 4 And i <= 20 Then
  2.   Cells(i, mr + 9) = "1"
  3.   TMP = Cells(i, mr + 4)
  4.   If TMP = "φ280" OR TMP = "φ350" or Cells(i, mr + 8) = "一出二" Then
  5.          Cells(i, mr + 10) = "150"
  6.   ElseIf TMP = "φ380" or TMP = "φ379" Then
  7.          Cells(i, mr + 10) = "175"
  8.   ElseIf TMP = "φ450" Then
  9.          Cells(i, mr + 10) = "320"
  10.   End If
  11. End If
复制代码
Cells(i, mr + 9) = "1"
这不是每行都是="1"
回复

使用道具 举报

发表于 2016-4-20 15:44 | 显示全部楼层
本帖最后由 gufengaoyue 于 2016-4-20 15:45 编辑
h284124405 发表于 2016-4-20 15:10
Cells(i, mr + 9) = "1"
这不是每行都是="1"

那加到里面去了。
  1. If i > 4 And i <= 20 Then  
  2. TMP = Cells(i, mr + 4)
  3.   If TMP = "φ280" OR TMP = "φ350" or Cells(i, mr + 8) = "一出二" Then
  4.          Cells(i, mr + 10) = "150": Cells(i, mr + 9) = "1"
  5.   ElseIf TMP = "φ380" or TMP = "φ379" Then
  6.          Cells(i, mr + 10) = "175": Cells(i, mr + 9) = "1"
  7.   ElseIf TMP = "φ450" Then
  8.          Cells(i, mr + 10) = "320": Cells(i, mr + 9) = "1"
  9.   End If  
  10. End If
复制代码
回复

使用道具 举报

发表于 2016-4-20 15:48 | 显示全部楼层    本楼为最佳答案   
  1. If i > 4 And i <= 20 Then
  2.         tmp = Cells(i, 4): Cells(i, 9) = "1"
  3.         Cells(i, 10) = IIf(Cells(i, 8) = "一出二" Or tmp = "φ280" Or tmp = "φ350", "150", _
  4.                        IIf(tmp = "φ380" Or tmp = "φ379", "175", IIf(tmp = "φ450", "320", "")))
  5.      End If
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 21:06 , Processed in 0.474763 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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