Vba 如果函数给我带来问题
我的值不断变假,而不是我添加的值,即使我输入男性。有人知道吗Vba 如果函数给我带来问题,vba,excel,if-statement,Vba,Excel,If Statement,我的值不断变假,而不是我添加的值,即使我输入男性。有人知道吗 If gender = "Male" Then Sheet2.Range("A2").Value = "he" & Sheet2.Range("A3").Value = "him" & Sheet2.Range("A4").Value = "his" Else Sheet2.Range("A2").Value = "she" & Sheet2.Range("A3").Value = "her" & Sh
If gender = "Male" Then
Sheet2.Range("A2").Value = "he" & Sheet2.Range("A3").Value = "him" & Sheet2.Range("A4").Value = "his"
Else
Sheet2.Range("A2").Value = "she" & Sheet2.Range("A3").Value = "her" & Sheet2.Range("A4").Value = "her"
End If
不能在同一语句中两次赋值。第一个后面的Any=字符被视为逻辑运算符(等于),而不是赋值 因此,如果你有:
a = b = c
VBA将语句视为:将“b等于c”的结果分配给A。
如果一个语句中有三个等号,情况会变得更复杂,但无论如何都不是你想要的
此外,&运算符用于字符串连接。所以
a = "top" & "hat"
将字符串“tophat”分配给a
您可能希望使用:运算符而不是&。它将语句分隔在一行上。所以试试这个:
If gender = "Male" Then
Sheet2.Range("A2").Value = "he" : Sheet2.Range("A3").Value = "him" : Sheet2.Range("A4").Value = "his"
Else
Sheet2.Range("A2").Value = "she" : Sheet2.Range("A3").Value = "her" : Sheet2.Range("A4").Value = "her"
End If
但是,我劝你不要使用:here。真的没有必要,我认为这会使代码更难阅读。我建议:
If gender = "Male" Then
Sheet2.Range("A2").Value = "he"
Sheet2.Range("A3").Value = "him"
Sheet2.Range("A4").Value = "his"
Else
Sheet2.Range("A2").Value = "she"
Sheet2.Range("A3").Value = "her"
Sheet2.Range("A4").Value = "her"
End If
不能在同一语句中两次赋值。第一个后面的Any=字符被视为逻辑运算符(等于),而不是赋值 因此,如果你有:
a = b = c
VBA将语句视为:将“b等于c”的结果分配给A。
如果一个语句中有三个等号,情况会变得更复杂,但无论如何都不是你想要的
此外,&运算符用于字符串连接。所以
a = "top" & "hat"
将字符串“tophat”分配给a
您可能希望使用:运算符而不是&。它将语句分隔在一行上。所以试试这个:
If gender = "Male" Then
Sheet2.Range("A2").Value = "he" : Sheet2.Range("A3").Value = "him" : Sheet2.Range("A4").Value = "his"
Else
Sheet2.Range("A2").Value = "she" : Sheet2.Range("A3").Value = "her" : Sheet2.Range("A4").Value = "her"
End If
但是,我劝你不要使用:here。真的没有必要,我认为这会使代码更难阅读。我建议:
If gender = "Male" Then
Sheet2.Range("A2").Value = "he"
Sheet2.Range("A3").Value = "him"
Sheet2.Range("A4").Value = "his"
Else
Sheet2.Range("A2").Value = "she"
Sheet2.Range("A3").Value = "her"
Sheet2.Range("A4").Value = "her"
End If
Rich Holton给了你代码不起作用的原因 在这里,我向大家介绍一些不同的技巧
Option Explicit
Sub main()
Dim valsArray As Variant '<--| declare a Variant variable to hold an array
Dim gender As String
gender = "Male" '<--| gender initialization for testing purposes
If gender = "Male" Then
valsArray = Array("he", "him", "his") '<--| fill your Variant with values corresponding to the 'gende'r value
Else
valsArray = Array("she", "her", "her") '<--| fill your Variant with values corresponding to the 'gende'r value
End If
Sheet2.Range("A2:A4").Value = Application.Transpose(valsArray) '<--| write all values in one shot (you need 'Transpose()' to change the "horizontal" Variant array to a "vertical" one and fit the range you're fill values of
End Sub
Rich Holton给了你代码不起作用的原因 在这里,我向大家介绍一些不同的技巧
Option Explicit
Sub main()
Dim valsArray As Variant '<--| declare a Variant variable to hold an array
Dim gender As String
gender = "Male" '<--| gender initialization for testing purposes
If gender = "Male" Then
valsArray = Array("he", "him", "his") '<--| fill your Variant with values corresponding to the 'gende'r value
Else
valsArray = Array("she", "her", "her") '<--| fill your Variant with values corresponding to the 'gende'r value
End If
Sheet2.Range("A2:A4").Value = Application.Transpose(valsArray) '<--| write all values in one shot (you need 'Transpose()' to change the "horizontal" Variant array to a "vertical" one and fit the range you're fill values of
End Sub
这个可怜的小伙子/姑娘显然是新来的可能不适合你的先进技术——顺便说一句,这些技术很酷。@RichHolton,你通过尝试先进来获得“先进”。。。我提出了一些意见,但如果OP需要更多的解释,他可以要求他们。但是,他可以在中断模式下运行该代码,并在VBA IDE中单击F8,然后使用即时窗口(CTRL+G弹出)查询相关变量或表达式(即键入
?Sheet2.范围(“A2:A4”)。地址,然后按return查看结果),从而逐步完成该代码。谢谢大家!所有伟大的建议!我完成了!要是没有你们,我早就做好了!这个可怜的小伙子/姑娘显然是新来的可能不适合你的先进技术——顺便说一句,这些技术很酷。@RichHolton,你通过尝试先进来获得“先进”。。。我提出了一些意见,但如果OP需要更多的解释,他可以要求他们。但是,他可以在中断模式下运行该代码,并在VBA IDE中单击F8,然后使用即时窗口(CTRL+G弹出)查询相关变量或表达式(即键入?Sheet2.范围(“A2:A4”)。地址,然后按return查看结果),从而逐步完成该代码。谢谢大家!所有伟大的建议!我完成了!要是没有你们,我早就做好了!