为什么这个VBA代码不能对我的excel表做任何修改?

问题描述 投票:0回答:1

我想让这段代码用于如果我在C表中的基因的rowJ, "brain? "旁边的rowG中输入 "yes",那么它就会在B表中找到该基因,并将yes放入B表中的rowJ中。

excel vba
1个回答
0
投票

检查你在GitHub上的Excel文件,我相信你指的是方法。inbrain() 在模块 Module3 (以后请在你的问题中加入该信息)。

我相信你的错别字是在 cell.Offset(0, 3).Value = brainB 这段代码的第一行

    Dim mmc2 As Workbook: Set mmc2 = ThisWorkbook
    Dim shB As Worksheet
    Set shB = mmc2.Sheets("(B) Mouse thymocytes RAG1")
    Dim shC As Worksheet
    Set shC = mmc2.Sheets("(C) Mouse preB RAG1")

    Dim colG As Range
    Set colG = shC.Range("G3:G3388")

    For Each cell In colG
        Dim brain As String
        brain = cell.Offset(0, 3).Value

        Dim brainB As String

        If brain = "yes" Then
            Dim found As Range
            Set found = shB.Range("G3:G3537").Find(What:=cell.Value)

            If Not found Is Nothing Then
                brainB = "yes"
            End If
            cell.Offset(0, 3).Value = brainB
        End If
    Next cell

应改为 cell.Offset(0, 4).Value = brainB

我还会扩展If语句来清除单元格,如果没有匹配,就像这样。

        If brain = "yes" Then
            Dim found As Range
            Set found = shB.Range("G3:G3537").Find(What:=cell.Value)

            If Not found Is Nothing Then
                brainB = "yes"
            End If
            cell.Offset(0, 4).Value = brainB
        Else
            cell.Offset(0, 4).Value = ""
        End If

0
投票

我写的代码是把 "是 "放在C表中已经有 "是 "的地方。"为了把 "是 "放在B表中已经找到值的地方,我用了以下方法

found.Offset(0,3).Value

而不是

cell. Offset(0,3). Value
© www.soinside.com 2019 - 2024. All rights reserved.