vba Excel - 如果空白,如何显示信息或移至下一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28118508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:57:04  来源:igfitidea点击:

Excel - How to display information or move on to next column if blank

excelvbaexcel-vbacountif

提问by akpersad

So this may be a little confusing.

所以这可能有点令人困惑。

I'm trying to figure out how to get excel to display a cell's text or move onto another column if that original cell is blank.

我试图弄清楚如何让 excel 显示单元格的文本或如果原始单元格为空白,则移动到另一列。

In other words, if A1 has "Dog", I want it to display "Dog". But if A1 is blank, I want it to check C1. If C1 has "Cat" in it, I want it to display "Cat". If C1 is blank, I want it to check E1....so on and so forth.

换句话说,如果 A1 有“狗”,我希望它显示“狗”。但如果 A1 为空,我希望它检查 C1。如果 C1 中有“Cat”,我希望它显示“Cat”。如果 C1 为空,我希望它检查 E1....等等。

Is this possible?

这可能吗?

Thanks!

谢谢!

回答by RubberDuck

I think everybody is overthinking this a bit. There's no reason at all to use VBA. Just use a combination of the Excel formulas IFand ISBLANK.

我认为每个人都想得太多了。完全没有理由使用 VBA。只需使用 Excel 公式IFISBLANK.

=IF(ISBLANK(A1),IF(ISBLANK(C1),E1,C1),A1)

回答by NoChance

Try something like this:

尝试这样的事情:

Option Explicit

Sub A()

Dim rng As Range, cell As Range  
Set rng = Range("A1:G2")  
Dim jump As Boolean  
jump = False  

For Each cell In rng  
If jump = False Then  
    If (cell.Value <> "") Then  
        MsgBox ("Contents of cell at row:" & cell.Row & "," & cell.Column & vbCrLf & "is:" & vbCrLf & cell.Value)  
    Else  
    'if A1 has "Dog", I want it to display "Dog". But if A1 is blank, I want it to check C1  
    jump = True  
    End If  
Else  
    jump = False  
End If  
Next cell  

End Sub

enter image description here

在此处输入图片说明

回答by user3561813

This is one possible approach. Keep in mind, it doesn't break in case of NO entry, so it will throw an error once it reaches the end of the sheet.

这是一种可能的方法。请记住,在没有输入的情况下它不会中断,因此一旦到达工作表的末尾就会抛出错误。

Sub ShowText()
    Dim rngStart As Range
    Dim strResult As String

    Const OFFSET_VALUE As Integer = 2


    Set rngStart = ActiveSheet.Range("A1")
    strResult = ""

    Do
        strResult = rngStart.Value
        Set rngStart = rngStart.Offset(, OFFSET_VALUE)
    Loop While strResult = ""

    MsgBox strResult

End Sub

回答by knw

This seems to work:

这似乎有效:

Range("A1").Select

Do While IsEmpty(Selection.Value)
Selection.Offset(0, 2).Select
Loop

MsgBox Selection.Value