带有= sum的Vba代码

时间:2016-09-03 06:55:33

标签: excel-vba sum vba excel

我使用以下

Sub CopyDataByDay()
    Dim data As Range
    Dim today As Date

    Set data = ThisWorkbook.Worksheets(1).Range("A1")
    today = Date

    If (Weekday(today) = vbMonday) Then
        ThisWorkbook.Worksheets(1).Range("B1") = data.Value
    ElseIf (Weekday(today) = vbTuesday) Then
        ThisWorkbook.Worksheets(1).Range("B2") = data.Value
    ElseIf (Weekday(today) = vbWednesday) Then
        ThisWorkbook.Worksheets(1).Range("B3") = data.Value
    ElseIf (Weekday(today) = vbThursday) Then
        ThisWorkbook.Worksheets(1).Range("B4") = data.Value
    ElseIf (Weekday(today) = vbFriday) Then
        ThisWorkbook.Worksheets(1).Range("B5") = data.Value
    ElseIf (Weekday(today) = vbSaturday) Then
        ThisWorkbook.Worksheets(1).Range("B6") = data.Value
    End If

    data.Value = ""
End Sub

但是在A1中我每次使用此VBA时都会删除=sum。如何停止移除=sum中的A1

3 个答案:

答案 0 :(得分:4)

不是尝试来回答问题,因为@Vahid已经指出了问题所在。对于如何编写简洁代码,这更像是pointer

这是编写代码的最短方法

Sub Sample()
    Dim data As Range
    Dim i As Long

    With ThisWorkbook.Worksheets(1)
        Set data = .Range("A1")

        For i = 2 To 7
            If Weekday(Date) = i Then .Range("B" & i - 1) = data.Value
        Next i
    End With
End Sub

请注意,vbMonday的值为2,vbSaturday为7.因此,您实际上可以为其编写循环。

这是一个非常大的循环,行

If Weekday(Date) = i Then .Range("B" & i - 1) = data.Value

也可以写成

If Weekday(Date) = i Then .Range("B" & i - 1) = data.Value: Exit For

答案 1 :(得分:3)

您需要删除此行

  

data.Value =“”

答案 2 :(得分:1)

尝试始终定义和引用Sheet对象。 此外,使用<?php $con = mysqli_connect("localhost","root","1234","users"); if(mysqli_connect_errno()){ echo "Couldn't connect ".mysqli_connect_error(); }else{ //echo "connection successful <br />"; } $uname_tb = $_POST['username']; $pass_tb = $_POST['password']; $query ="SELECT * FROM user"; $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result)){ if(($row['username'] == $uname_tb) && ($row['password'] == $pass_tb)){ echo "Login Successful"; header('Location: dashbord.php'); exit(); }else{ echo "You are not in our database".mysqli_connect_error(); } } mysqli_close($con); ?> ,简化了很多:

Select Case