VBA - 如果列A中的单元格不为空,则列B等于 [英] VBA - If a cell in column A is not blank the column B equals

查看:519
本文介绍了VBA - 如果列A中的单元格不为空,则列B等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些可以查看列A的代码,只要列A中的单元格不为空,则列B中的相应单元格将等于具体值。



所以如果单元格A1>然后单元格B1.Value =MyText
并重复,直到列A中的单元格为空或空。



为了增加一点澄清,我已经看过这里回答的各种循环问题和回答。他们有所帮助。但是,我不清楚如何使循环遍历列A,以验证列B中的每个单元格在列B中的相应单元格中不为空,并添加一些我指定的文本。



此外,这将需要是VBA宏的一部分,而不是单元格公式的一部分,例如= IF

解决方案

如果您真的想要一个vba解决方案,您可以循环遍历这样的范围:

 子检查()
Dim dat As Variant
Dim rng As Range
Dim i As Long

设置rng =范围(A1:A100)
dat = rng
对于i = LBound(dat,1)到UBound(dat,1)
如果dat(i,1)< 然后
rng(i,2).Value =My Text
End If
Next
End Sub



而不是使用变量,你可以循环遍历这样的范围:

  Sub Check()
Dim rng As Range
Dim i As Long

'设置您要循环通过$ b $的列A中的范围b设置rng =范围(A1:A100)
对于每个单元格在rng
'测试如果单元格为空
如果cell.Value<> 然后
'写入相邻单元格
cell.Offset(0,1).Value =My Text
End If
Next
End Sub


I'm looking for some code that will look at Column A and as long as the cell in Column A is not blank, then the corresponding cell in Column B will equal a specific value.

So if Cell A1 <> "" then Cell B1.Value = "MyText" And repeat until a cell in Column A is blank or empty.

To add a little more clarification, I have looked through the various loop questions asked and answered here. They were somewhat helpful. However, I'm unclear on how to get the loop to go through Column A to verify that each cell in Column A isn't blank AND in the corresponding cell in Column B, add some text that I specify.

Also, this will need to be part of a VBA macro and not part of a cell formula such as =IF

解决方案

If you really want a vba solution you can loop through a range like this:

Sub Check()
    Dim dat As Variant
    Dim rng As Range
    Dim i As Long

    Set rng = Range("A1:A100")
    dat = rng
    For i = LBound(dat, 1) To UBound(dat, 1)
        If dat(i, 1) <> "" Then
            rng(i, 2).Value = "My Text"
        End If
    Next
End Sub

*EDIT*

Instead of using varients you can just loop through the range like this:

Sub Check()
    Dim rng As Range
    Dim i As Long

    'Set the range in column A you want to loop through
    Set rng = Range("A1:A100")
    For Each cell In rng
        'test if cell is empty
        If cell.Value <> "" Then
            'write to adjacent cell
            cell.Offset(0, 1).Value = "My Text"
        End If
    Next
End Sub

这篇关于VBA - 如果列A中的单元格不为空,则列B等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆