确定单元格是否包含值

时间:2019-03-24 01:06:55

标签: excel vba

我有一个名为“更新”的命令按钮,用于激活宏。

宏应检查单元格是否在G:25到G:33范围内。
如果为空,则什么也不会发生。
如果将数字写到Box中,则应复制该数字,然后将其放入第二个工作表中的单元格中。
因此,应该将Sheet1。(G:25)复制到sheet2。(G14),然后迭代到G:25

什么都没发生。

Sheet1 =“Übersicht”
Sheet2 =“ Semester01”

$student = \DB::table('student')
               ->select(\DB::raw('CONCAT_WS(" ", `FirstName`, `MiddleName`, `Lastname`) as Name'))
               ->get();

2 个答案:

答案 0 :(得分:1)

我看不到所有额外变量和缺少/令人困惑的父引用的意义。我看不出 zelle 的来源。

Option Explicit

sub go()

    Dim cell As Range, c As Integer

    c = 14
    For Each cell In sheet1.Range("G25:G33")
        If val(cell.Value) < 0 Then
            cell.Copy destination:=Sheet2.Cells(7, c)
            c = c + 1
        End If

    Next cell

End Sub

如果使用Option Explicit,可以避免变量拼写错误。

答案 1 :(得分:0)

如果有条件则复制单元格...

Option Explicit

Sub ZelleCopy()

    Const cShS As String = "Übersicht"    ' Source Worksheet Name
    Const cShT As String = "Semester01"   ' Target Worksheet Name
    Const cRng As String = "G25:G33"      ' Source Column Range Address
    Const cTgtFR As Long = 14             ' Target First Row Number
    Const cTgtCol As Variant = "G"        ' Target Column Letter/Number ' or 7

    Dim wsS As Worksheet  ' Source Worksheet
    Dim wsT As Worksheet  ' Target Worksheet
    Dim cell As Range     ' Current Cell (For Each Control Variable)
    Dim c As Long         ' Target Cell (Row) Counter
    Dim Score As Long     ' Criteria Value

    ' Create references to Source and Target Worksheets.
    With ThisWorkbook
        Set wsS = .Worksheets(cShS)
        Set wsT = .Worksheets(cShT)
    End With

    ' Write Target First Row Number to Target Row Counter.
    c = cTgtFR
    ' Loop through each cell (row) in Source Column Range.
    For Each cell In wsS.Range(cRng)
        ' Write value of Current Cell to Criteria Value.
        Score = cell.Value
        ' Check if Criteria Value is less than 0.
        If Score < 0 Then
            ' Write Criteria Value to current cell in Target Column.
            wsT.Cells(c, cTgtCol) = Score
            ' Count Target Row.
            c = c + 1
        End If
    Next

End Sub