vba设置代码,如何任意修改单元格数据,代码涉及的数据能自动同步更新,谢谢。

2019-04-21 10:08发布

1、代码所涉及到的单元格发生变动,能自动同步更新

2、代码显示【***】,如果【】内无内容,”【】“就不显示

3、单元格录入单个数字,设置后显示成2位数,如录入“1”,显示“02”,希望代码提取该数字也能显示成“01”


以上请各位大神帮忙看一下,能实现一个算一个,先谢谢了!


Private Sub Worksheet_Change(ByVal Target As Range)

 If Target.Column = 25 And Target.Count = 1 Then

 

          If Target.Value <> 0 Then

       'AA=A.B.C GE X N【O】U【V】【W】

        Target.Offset(, 2).Value = Target.Offset(, -24).Value & "." & Target.Offset(, -23).Value & "." & Target.Offset(, -22).Value _

                  & " " & Target.Offset(, -18).Value & Target.Offset(, -20).Value & " " & Target.Offset(, -1).Value & " " & Target.Offset(, -11).Value _

                  & "【" & Target.Offset(, -8).Value & Target.Offset(, -10).Value & "】 " & Target.Offset(, -4).Value & " 【" & Target.Offset(, -3).Value & "】 " & "【" & Target.Offset(, -2).Value & "】"

 

End If

End If

 

End Sub


2条回答
贝尔梅尔娜美
2019-04-21 11:00 .采纳回答

AA=A.B.C GE X N【O】U【V】【W】

AA=A.B.C GE X N[O]U[V][W]

显示效果不同。

英文左中括号([)前面和英文右中括号(])后面不加空格不会显示下面效果:

18.02.06 王维1003 大 曹操 [曹操2] A1 [A2] [A3]

只会显示成:

18.02.06 王维1003 大 曹操[曹操2]A1[A2][A3]

你的这一代码:

Private Sub Worksheet_Change(ByVal Target As Range)

 If Target.Column = 25 And Target.Count = 1 Then

          If Target.Value <> 0 Then

       'AA=A.B.C GE X N【O】U【V】【W】

        Target.Offset(, 2).Value = Target.Offset(, -24).Value & "." & Target.Offset(, -23).Value & "." & Target.Offset(, -22).Value _

                  & " " & Target.Offset(, -18).Value & Target.Offset(, -20).Value & " " & Target.Offset(, -1).Value & " " & Target.Offset(, -11).Value _

                  & "【" & Target.Offset(, -8).Value & Target.Offset(, -10).Value & "】 " & Target.Offset(, -4).Value & " 【" & Target.Offset(, -3).Value & "】 " & "【" & Target.Offset(, -2).Value & "】"

 End If

End If

End Sub

(1)向AA列中输入数据会触发工作表的Change事件,造成重复运行此代码

(2)换个思路,向AA列输入公式的方法,代码如下。

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

If Target.Column = 25 And Target.Count = 1 And Target.Row > 1 Then

  Range("AA" & Target.Row) = "=IF(RC[-2]=0,"""",CONCATENATE(TEXT(RC[-26]+ISODD(RC[-26]),""00""),""."",TEXT(RC[-25]+ISODD(RC[-25]),""00""),""."",TEXT(RC[-24]+ISODD(RC[-24]),""00""),"" "",RC[-20],RC[-22],"" "",RC[-3],"" "",RC[-13],IF(RC[-12]="""","""","" [""&RC[-12]&""] ""),RC[-6],"" "",IF(RC[-5]="""","""",""[""&RC[-5]&""] ""),IF(RC[-4]="""","""",""[""&RC[-4]&""]"")))"

End If

Application.EnableEvents = True

End Sub


上面粗体标注的代码不用担心写法,你录制宏——在AA2单元格输入公式:

=IF(Y2=0,"",CONCATENATE(

TEXT(A2+ISODD(A2),"00"),".",

TEXT(B2+ISODD(B2),"00"),".",

TEXT(C2+ISODD(C2),"00")," ",

G2,

E2," ",

X2," ",

N2,

IF(O2="",""," ["&O2&"] "),

U2," ",

IF(V2="","","["&V2&"] "),

IF(W2="","","["&W2&"]")))

然后停止录制宏就有了。

结果如下图所示:

一周热门 更多>