简体   繁体   English

宏或VBA代码可自动更新每日每周和每月报告的数据

[英]Macro or vba code to automatically update data for daily weekly and monthly report

I have excel sheet with 9 columns and name are: 我有9列的Excel工作表,名称是:

  1. SNO SNO
  2. PO Number 订单号
  3. Created Date 创建日期
  4. Currency 货币
  5. PO Amount 采购单金额
  6. Global Funds Transfer Count 全球资金转移计数
  7. BankName 银行的名字
  8. Status 状态
  9. Prepared User 准备的用户

I want to write a macro or VBA code which can help me save daily weekly and monthly report. 我想编写一个宏或VBA代码,可以帮助我保存每日的每周和每月报告。

On sheet1 I will be pasting data for 150 rows for above 9 columns on daily basis and I want out of that to save 5 columns: 在sheet1上,我每天将粘贴150行以上9列的数据,并且我希望其中的数据节省5列:

1.SNO 2.Bank name 3.po amount 4.Global Funds Transfer Count 5.prepared users to save automatically to sheet2. 1.SNO 2.银行名称3.po金额4.全球资金转帐计数5.准备好的用户自动保存到sheet2。

Whenever I paste any data in sheet1 I want data of above 5 columns to be saved in sheet2 on date wise for each day. 每当我将任何数据粘贴到sheet1中时,我希望每天将日期超过5列的数据按日期保存在sheet2中。 And from sheet2 I want my full data of sheet2 to sheet3 to take monthly report for above 5 column. 从sheet2,我希望我的sheet2到sheet3的完整数据能够获取上述5列的月度报告。

But when I update data old data from sheet2 get delete. 但是,当我更新数据时,sheet2中的旧数据会被删除。

Sub sbCopyRangeToAnotherSheet()

    Sheets("Sheet1").Range("B1:B100").Copy _
      Destination:=Sheets("Sheet2").Range("A1")
    Sheets("Sheet1").Range("H1:H100").Copy _
      Destination:=Sheets("Sheet2").Range("B1") 
    Sheets("Sheet1").Range("G1:G100").Copy _
      Destination:=Sheets("Sheet2").Range("C1") 
    Sheets("Sheet1").Range("F1:F100").Copy _
      Destination:=Sheets("Sheet2").Range("D1") End Sub 

    Dim rng As Range

    'Store blank cells inside a variable
      On Error GoTo NoBlanksFound
        Set rng = Range("E1:E130").SpecialCells(xlCellTypeBlanks)
      On Error GoTo 0

    'Delete blank cells and shift upward
      rng.Rows.Delete Shift:=xlShiftUp

    Exit Sub

    'ERROR HANLDER
    NoBlanksFound:
      MsgBox "No Blank cells were found"

End Sub

If you want it fully automated, a 'best guess' at a trigger can be made but it is by no means fool-proof. 如果您希望它完全自动化,则可以对触发器进行“最佳猜测”,但这绝不是万无一失的。

My 'best guess' is based on your statement of 'pasting data for 150 rows for above 9 columns' 我的“最佳猜测”基于您的陈述: “粘贴9列以上的150行数据”

I've avoided your msgbox error control since a truly automated process doesn't require one if error control has been provided. 我避免了您的msgbox错误控制,因为如果提供了错误控制,那么真正的自动化过程不需要一个。

In lieu of confirmation, I've assumed that Range("E1:E130") belongs to Sheet1. 代替确认,我假设Range(“ E1:E130”)属于Sheet1。

Put this in the Sheet1 private code sheet (right-click worksheet name tab then View Code). 将其放在Sheet1专用代码表中(右键单击工作表名称选项卡,然后单击“查看代码”)。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A:I")) Is Nothing Then
        If Intersect(Target, Range("A:I")).Count >= 1350 Then
            On Error GoTo safe_exit
            Application.EnableEvents = False

            Range("B1:B100").Copy Destination:=Worksheets("Sheet2").Range("A1")
            Range("H1:H100").Copy Destination:=Worksheets("Sheet2").Range("B1")
            Range("G1:G100").Copy Destination:=Worksheets("Sheet2").Range("C1")
            Range("F1:F100").Copy Destination:=Worksheets("Sheet2").Range("D1")

            If Application.CountA(Range("E1:E130")) < 130 Then _
                Range("E1:E130").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp

        End If
    End If

safe_exit:
    Application.EnableEvents = True
End Sub

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

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