简体   繁体   English

Excel VBA:将公式值转换为文本

[英]Excel VBA: convert formula value to text

I would like to have an answer to a following situation. 我想对以下情况做出回答。 I have basic VBA knowledge, sorry if I'm using incorrect expressions. 我有基本的VBA知识,如果我使用的表达式不正确,抱歉。

Let's say I would like to go on a long trip, but have to check the tires' conditions before departing. 假设我想长途旅行,但是在出发前必须检查轮胎的状况。 I have to have answers on 3 questions: 我必须回答3个问题:

  1. Are the tires under/overinflated? 轮胎是否充气不足/过度充气? (answer to G1) (对G1的回答)
  2. Is any of the tires flat? 轮胎是否放气? (G2) (G2)
  3. Do I have to change the tires due to different weather conditions? 由于天气条件的不同,我是否必须更换轮胎? (G3) (G3)

Thing is if any of the questions remain unanswered or the answer is Yes, further actions are required (in subsequent rows of the excel sheet, rows 5-9). 如果有任何问题仍未解决,或者答案为“是”,则需要采取进一步的措施(在excel工作表的后续行中,第5-9行)。 In case all the 3 answers are No, tires are OK and I want the subsequent rows hidden. 如果所有3个答案均为“否”,轮胎就可以了,我希望以后的行都隐藏。

To wrap up the conclusion, I have entered the following formula to G4: 最后,我在G4中输入以下公式:

=IF(AND(G1="No",G2="No",G3="No"), "No", "Yes") . =IF(AND(G1="No",G2="No",G3="No"), "No", "Yes")

The formula answers perfectly. 公式完美地回答了。 I pasted some excerpts of the code I have so far: 我粘贴了到目前为止的代码摘录:

Worksheet_Change(ByVal Target As Range)
 Select Case Target.Address
  Case "$G$4"
  Rows("5:9").EntireRow.Hidden = (Target = "No")
...

The issue is that the code is not reading the answer of the formula in cell G4. 问题在于该代码未读取单元格G4中公式的答案。 If I delete the formula and rewrite the answer manually, the code is working perfectly and hides the subsequent rows. 如果我删除公式并手动重写答案,则代码运行正常,并且隐藏了后续行。 So the main issue is to convert the result of the formula in G4 to make it readable for the code. 因此,主要问题是在G4中转换公式的结果以使其对代码可读。

Can anybody help please? 有人可以帮忙吗? Or is there a better solution? 还是有更好的解决方案? Thanks in advance. 提前致谢。

The problem is not "to convert the result of the formula in G4 to make it readable for the code" since it is readable. 问题不在于“可以转换G4中的公式结果以使其对代码可读”,因为它是可读的。 But the target of Worksheet_Change event will not be G4 but either G1 or G2 or G3 which will then change the value of G4 through the formula calculation. 但是Worksheet_Change事件的目标不是G4而是G1G2G3 ,它们将通过公式计算来更改G4的值。 But this calculation is not a Worksheet_Change event but a Worksheet_Calculate event. 但是,此计算不是Worksheet_Change事件,而是Worksheet_Calculate事件。

So either you are checking whether G1 or G2 or G3 is target in Worksheet_Change event 因此,您要检查Worksheet_Change事件中的目标是G1还是G2G3

Private Sub Worksheet_Change(ByVal Target As Range)
 Select Case Target.Address
  Case "$G$1", "$G$2", "$G$3"
  Me.Rows("5:9").EntireRow.Hidden = (Me.Range("G4").Value = "No")
 End Select
End Sub

Or you check the result of the formulas recalculation in Worksheet_Calculate event: 或者,您可以在Worksheet_Calculate事件中检查公式重新计算的结果:

Private Sub Worksheet_Calculate()
 Me.Rows("5:9").EntireRow.Hidden = (Me.Range("G4").Value = "No")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM