简体   繁体   English

VBA如果工作表1上的单元格(或范围)包含任何值,则清除工作表2上的单元格范围内容

[英]VBA If cell (or range) on sheet1 contains any value then clear content of cell range on sheet 2

I am trying to achieve something which would be quite simple for you guys but proving a bit difficult for myself as beginner. 我正在尝试实现对你们来说很简单但对初学者来说有点困难的东西。

I am try to create script for following example: 我尝试为以下示例创建脚本:

"If Cells A1:A4 on sheet1 contain ANY value (text or int) then clear A1:A4 on sheet2" “如果sheet1上的单元格A1:A4包含任何值(文本或整数),则清除sheet2上的A1:A4”

I tried to construct few scripts but all failed. 我尝试构建一些脚本,但全部失败。

Many thanks! 非常感谢!

Try this 尝试这个

Sub Clear()
 If Not IsEmpty("A1") And Not IsEmpty("A2") And Not IsEmpty("A3") And Not IsEmpty("A4") Then
    Range("A1:A4").Clear
 End If
End Sub

How about: 怎么样:

Sub ClearCell()
    Dim r As Range

    For Each r In Sheets("Sheet1").Range("A1:A4")
        If r.Value <> "" Then Sheets("Sheet2").Range(r.Address).Clear
    Next r
End Sub

This assumes you need the check to be cell-by-cell. 假设您需要逐个单元进行检查。

Here's a one liner, the cell value is counted when it is an Empty String. 这是一个底线,当它为空字符串时,将对单元格值进行计数。

    Public Sub ClearRange()
        If Application.WorksheetFunction.CountIf(Range("a1:a4"), "<>") > 0 Then Range("a1:a4").ClearContents
    End Sub

暂无
暂无

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

相关问题 VBA 清除单元格范围内的内容,工作表 1 除外 - VBA clear content in cell range, except for sheet 1 搜索范围Sheet1对Sheet2中的范围根据横向单元格中的值复制到Sheet3 - Searching Range Sheet1 Against Range in Sheet2 Copy to Sheet3 depending on value in lateral cell 复制范围工作表1粘贴到活动单元格工作表2中 - Copy Range Sheet1 Paste in Active Cell Sheet 2 复制范围表1粘贴到所选单元格表2中 - Copy range sheet1 paste in selected cell sheet 2 从“ sheet1”上的单元格区域复制到“ sheetB3”上的相同区域 - Copy from cell range on 'sheet1' to same range on 'sheetB3' 有没有办法从 Sheet1 上的单元格或单元格范围中提取值并将其放在左页脚中,并为页脚设置字体颜色? - Is there a way to pull a value from a cell or range of cells on Sheet1 and put it in the left footer, and set font color for the footer? 如果单元格与范围中的任何其他单元格匹配,则将内容复制到“结果”表 - If cell matches any others in range copy content to “results” sheet Excel VBA-在不同工作表中重复单元格范围 - Excel VBA - Repeating Cell Range in different sheet 如果工作表sheet2中的值与工作表sheet1中的标头的值匹配的Vba代码,从工作表sheet1检索整列 - Vba code to retrieve an entire column from sheet1 if the value in a cell of sheet2 matches the value of a header in sheet1 VBA如果Sheet1中的特定单元格包含的数据&gt; 0,则在Sheet2上放置特定的代码 - VBA If a specific cell in Sheet1 contains data > 0, put a specific code on Sheet2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM