Excel宏(如果大于或小于,则更改所选单元格的值)

时间:2014-04-02 09:10:34

标签: excel vba excel-vba

我有一项任务要做,而且耗时,搜索一些解决方案,但它不会起作用。

我有这个工作簿需要在每个单元格中编辑。 如果我选择B2到B8之类的范围,其内容为

B2 = 70, B3 = 75, B4 = 80, B5 = 85, B6 = 90, B7 = 95, B8 = 100

条件是单元格是<=100 but >=96 =A, <=95 but >=91 =B, <=90 but >=86 =C, <=85 but >=81 =D, <=80 but >=76 = E, <=75 but >=71 =F And <=70 =G.

因此,每个单元格将更改为例如B2 = 70,现在它将更改为70(G),使用&#34;()&#34;。

所以B2到B8

B2 = 70(G), B3 = 75(F), B4 = 80(E), B5 = 85(D), B6 = 90(C), B7 = 95(B), B8 = 100(A)

1 个答案:

答案 0 :(得分:0)

选择您的值后,运行以下宏:

Sub transform()
Dim v, a As String
For Each v In Selection
 Select Case v
  Case Is <= 70
   a = "G"
  Case Is <= 75
   a = "F"
  Case Is <= 80
   a = "E"
  Case Is <= 85
   a = "D"
  Case Is <= 90
   a = "C"
  Case Is <= 95
   a = "B"
  Case Is <= 100
   a = " A"
 End Select
 v.Value = v & "(" & a & ")"
Next
End Sub