简体   繁体   English

如何在基于另一个单元格的单元格中写入

[英]How to write in a cell based on another cell

I'm trying to check if a cell in one sheet has a certain value, and if that's the case write a text string in another cell in another sheet.我正在尝试检查一张工作表中的单元格是否具有特定值,如果是这种情况,请在另一张工作表的另一个单元格中写入文本字符串。

Afterwards I would like to call the same sub in some range in the first sheet and apply it to the corresponding range in the other sheet.之后,我想在第一张工作表的某个范围内调用相同的子并将其应用于另一张工作表中的相应范围。 The ranges don't have them same location, only the same size.这些范围没有相同的位置,只有相同的大小。

Here is an example:下面是一个例子:
在此处输入图片说明

The first binary matrix is in the first sheet.第一个二进制矩阵在第一张表中。 (The location could as an example be in A1:B2). (例如,该位置可以在 A1:B2 中)。
The second matrix is made based on the first matrix (The location could as an example be in A1:B2).第二个矩阵是基于第一个矩阵制作的(例如,位置可以在 A1:B2 中)。

This is my idea so far (notice, it isn't working yet)到目前为止,这是我的想法(注意,它还没有工作)

Sub Convert(SomeCell, AnotherCell)
    If Worksheets("Sheet1").Range(SomeCell).Value > 0 Then
        Worksheets("Sheet2").Range(AnotherCell).Value = "String"
    End If
End Sub

EDIT编辑

I'm searching for the corresponding VBA code for this IF statement formula我正在为这个 IF 语句公式搜索相应的 VBA 代码

= IF(Sheet1!A1>0;"String";"")

Which then can be dragged in some range.然后可以在某个范围内拖动。

This might give you an idea:这可能会给你一个想法:

Sub Convert(Source As Range, Target As Range, Replace As Variant)
    'Takes a binary matrix in source range and fills in target range
    'with value Replace taking the place of 1
    'assumes equal sized ranges
    
    Dim i As Long, j As Long
    For i = 1 To Source.Rows.Count
        For j = 1 To Source.Columns.Count
            If Source.Cells(i, j).Value = 1 Then Target.Cells(i, j).Value = Replace
        Next j
    Next i
End Sub

Called like:调用如下:

Sub test()
    Convert Sheets(1).Range("A1:B2"), Sheets(2).Range("A1:B2"), "String"
End Sub

There are probably better ways to do it, but this will give you a way to represent a binary matrix in one sheet with a string matrix in another (if that is what you were trying to do).可能有更好的方法来做到这一点,但这将为您提供一种在一张纸中表示二进制矩阵的方法,而在另一张纸中用字符串矩阵表示(如果这是您想要做的)。 Note that unexpected results could occur if the source and target range overlap.请注意,如果源和目标范围重叠,可能会出现意外结果。

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

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