如果细胞发生变化,Vba会同时保存

时间:2017-12-13 03:01:56

标签: excel-vba vba excel

我对VBA很新,我非常感谢任何帮助! 我有一个代码来保存每天00:10,这很好,我有一个细胞更改的代码,但我不知道如何将它们组合...    如果单元格发生变化,则需要保存,但等待00:10hrs,从保存日期减去1天并保存,因为温度数据值属于前一天。提前谢谢!

'Cell change is at Sheet2
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$20" Then
        Call teste 
    End If
If Target.Address = "$G$20" Then
        Call teste
    End If
End Sub

'ThisWorkbook
Private Sub Workbook_Open()
   Application.OnTime TimeValue("00:10:00"), "Abre"
End Sub

'Module1
Sub Abre()
    Application.OnTime TimeValue("00:10:00"), "Abre"
    Dim datestr As String
    datestr = Format(Now, "yyyymmdd, hhmm")
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "D:\Temperature Data\DailyTemp " & datestr & ".xlsm"
End Sub

'Module2
Sub teste()
MsgBox "Did not work"
End Sub

1 个答案:

答案 0 :(得分:0)

根据this帖子,您可以将参数传递给OnTime函数。将Abre更改为接受日期,该日期可用于保存文件并使用Worksheet_ChangeAbre本身的相应日期进行调用。下面的代码没有经过测试,但我认为你会得到这个想法。要检查的一件事是,如果您的Worksheet_Change方法被调用两次会发生什么:在调用新的OnTime之前,您是否必须取消之前的'Cell change is at Sheet2 Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$C$20" Then Call teste End If If Target.Address = "$G$20" Then Call teste End If Dim datestr As String datestr = Format(Now, "yyyymmdd, hhmm") Application.OnTime TimeValue("00:10:00"), procedureCalled, , False procedureCalled = "'Abre """ & datestr & """'" Application.OnTime TimeValue("00:10:00"), procedureCalled End Sub 'ThisWorkbook Private Sub Workbook_Open() Dim datestr As String datestr = Format(Now, "yyyymmdd, hhmm") procedureCalled = "'Abre """ & datestr & """'" Application.OnTime TimeValue("00:10:00"), procedureCalled End Sub 'Module1 Public procedureCalled As String '<--variable to keep last scheduled procedure, so you can unshedule it in Worksheet_Change Sub Abre(savedate As String) Dim datestr As String datestr = Format(Now, "yyyymmdd, hhmm") procedureCalled = "'Abre """ & datestr & """'" Application.OnTime TimeValue("00:10:00"), procedureCalled Application.DisplayAlerts = False ActiveWorkbook.SaveAs "D:\Temperature Data\DailyTemp " & savedate & ".xlsm" End Sub 'Module2 Sub teste() MsgBox "Did not work" End Sub

<?php

    $message='bad username or password!';

    $login_info = array('batman'=>'404','superman'=>'502');
    $form_usr = isset( $_POST['usr'] ) ? $_POST['usr'] : false;
    $form_pass = isset( $_POST['pass'] ) ? $_POST['pass'] : false;
    $form_submit = isset( $_POST['submit'] ) ? $_POST['submit'] : false;

    if ( $form_submit && $form_usr && $form_pass ) {
        if( array_key_exists( $form_usr, $login_info ) && $login_info[ $form_usr ]==$form_pass ){
            $message="Successfully logged in !";
        }
        echo $message;
    }

?>