【问题标题】:Save 2 different worksheets in the same workbook using coding使用编码在同一个工作簿中保存 2 个不同的工作表
【发布时间】:2013-05-22 16:07:18
【问题描述】:

我使用 Excel 为我的公司制作发票系统。我不得不为使用该程序的其他一些员工提供“虚拟证明”。我使用几个代码使其成功。我有两张纸:Carolina Fireworks Order Form 和 Back Order。 Carolina Fireworks Order Form 上有一个宏,可将所有单元格复制到 Back Order Form(这是 Carolina Fireworks Order Form 的精确副本,除了 C7 中的客户名称放置它会自动显示客户名称和 BO)。

我有一个代码可以自动将文件保存到带有 C7(客户名称)和当前日期的特定文件夹中。有没有一种方法可以添加一个代码,如果我点击宏按钮复制 BO 单元格,它将自动保存 Back Order 表,文件名 C7 和当前日期?然后当我点击 x 按钮时,我的其他代码将自动保存 Carolina Fireworks 订单(表 1)?

这有意义吗?我不是代码编写者,所以我必须永远搜索才能使下面的代码正常工作。如果有更好的方法可以做到这一点,那么我完全愿意接受!以下是我用于模块 1 的当前代码:

Sub myOpenCode()
'Standard module code, like: Module1.
Dim strCustomer$, strMsg$, myUpDate$, strCustNm$

Application.EnableEvents = True
On Error GoTo myErr

strCustomer = Sheets("Carolina Fireworks Order Form").Range("C7").Value

'Test for current customer!
If strCustomer <> "" Then

strMsg = "The current customer name is:" & vbLf & vbLf & _
strCustomer & vbLf & vbLf & _
"Change this customer name to a different Name?"

'Test for customer name update?
myUpDate = MsgBox(strMsg, vbQuestion + vbYesNo, "Add Customer?")

'Chose "Yes" button!
If myUpDate = 6 Then
'Change current customer's name!
strCustNm = InputBox(strMsg, "Change Customer Name!", "")
End If

'Chose "No" button!
If myUpDate = 7 Then
'Keep current customer name!
Application.EnableEvents = True
Exit Sub
End If
Else

'Get customer name!
strMsg = "The current customer name is:" & vbLf & vbLf & _
"""EMPTY!""" & vbLf & vbLf & _
"Add a customer name:"

'Force add customer name add!
myGetCustNm:
strCustNm = InputBox(strMsg, "Add Customer Name!", "")

If strCustNm = "" Then GoTo myGetCustNm
End If

'Load customer name!
Sheets("Carolina Fireworks Order Form").Range("C7").Value = strCustNm
Application.EnableEvents = True
Exit Sub

myErr:
'GoTo Error routine!
Call myErrHandler(Err)
End Sub

Sub myCloseCode()
'Standard module code, like: Module1.
Dim strDate$, strCustomer$, strFileNm$, strMsg$, myUpDate$

Application.EnableEvents = False
On Error GoTo myErr

'Test for Save option or Exit without saving?
strMsg = "Save this file before closing?"

myUpDate = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Now?")

'Chose "Yes" button!
If myUpDate = 6 Then GoTo mySave

'Chose "No" button!
If myUpDate = 7 Then
Application.EnableEvents = True
Exit Sub
End If

mySave:
'Build file name!
strDate = DatePart("m", Date) & "-" & _
DatePart("d", Date) & "-" & _
Right(DatePart("yyyy", Date, vbUseSystemDayOfWeek, vbUseSystem), 4)

strCustomer = Sheets("Carolina Fireworks Order Form").Range("C7").Value

strFileNm = "\\Owner-hp\Users\Public\Customers\" & strCustomer & "-" & strDate & ".xlsm"

'Save current file!
ActiveWorkbook.SaveAs Filename:=strFileNm
Application.EnableEvents = True
ActiveWorkbook.Close
Exit Sub

myErr:
'GoTo Error routine!
Call myErrHandler(Err)
Application.EnableEvents = True
End Sub

Private Sub myErrHandler(myErr As ErrObject)
'Standard module code, like: Module1.
'Error Trap Routine!
Dim myMsg$

'Build Error Message!
myMsg = "Error Number : " & Str(myErr.Number) & vbLf & _
"Error Location: " & myErr.Source & vbLf & _
"Error Description: " & myErr.Description & vbLf & vbLf & _
"Context: " & myErr.HelpContext & vbLf & _
"Help File: " & myErr.HelpFile
'Show Error Message!

MsgBox myMsg & vbLf & vbLf & _
"Use the ""Help"" button for more information, on this ERROR!", _
vbCritical + vbMsgBoxHelpButton, _
Space(3) & "Error!", _
myErr.HelpFile, _
myErr.HelpContext
End Sub

模块 2:

Sub CopyBO()
'Copy cells of cols A,B,D from rows containing "BO" in
'col I of the active worksheet (source sheet) to cols
'A,B,D of Sheet2 (destination sheet)
Dim DestSheet        As Worksheet
Set DestSheet = Worksheets("Back Order")

Dim sRow       As Long     'row index on source worksheet
Dim dRow       As Long     'row index on destination worksheet
Dim sCount     As Long
sCount = 0
 For sRow = 1 To 65536
   'use pattern matching to find "BO" anywhere in cell
   If Cells(sRow, "I") Like "*BO*" Then
      sCount = sCount + 1
        'copy cols A,B, D
      Cells(sRow, "A").Copy Destination:=DestSheet.Cells(sRow, "A")
      Cells(sRow, "B").Copy Destination:=DestSheet.Cells(sRow, "B")
      End If
Next sRow

MsgBox sCount & " Back Ordered rows copied", vbInformation, "Transfer Done"
End Sub

【问题讨论】:

    标签: vba save


    【解决方案1】:

    以下代码将在调用过程CopyBO 时创建Back Order Sheet 的副本。

      Sub CopyBO()
        'Copy cells of cols A,B,D from rows containing "BO" in
        'col I of the active worksheet (source sheet) to cols
        'A,B,D of Sheet2 (destination sheet)
            Dim DestSheet As Worksheet
            Set DestSheet = Worksheets("Back Order")
            Dim n_Wkb As Workbook    ' new workbook
            Dim strFileNm As String
    
            Dim sRow As Long    'row index on source worksheet
            Dim dRow As Long    'row index on destination worksheet
            Dim sCount As Long
            sCount = 0
            For sRow = 1 To 65536
                'use pattern matching to find "BO" anywhere in cell
                If Cells(sRow, "I") Like "*BO*" Then
                    sCount = sCount + 1
                    'copy cols A,B, D
                    Cells(sRow, "A").Copy Destination:=DestSheet.Cells(sRow, "A")
                    Cells(sRow, "B").Copy Destination:=DestSheet.Cells(sRow, "B")
                End If
            Next sRow
    
    
            If sCount > 0 Then
                DestSheet.Copy
                Set n_Wkb = ActiveWorkbook
    
                ' Get the file path
                strCustomer = ThisWorkbook.Sheets("Carolina Fireworks Order Form").Range("C7").Value
                strFileNm = "\\Owner-hp\Users\Public\Customers\" & strCustomer
                strFileNm = strFileNm & Format(Now(), "DD-MM-YY hh.mm.ss") & ".xlsx"
    
                'save
                n_Wkb.SaveAs strFileNm
                n_Wkb.Close
    
            End If
    
    
            MsgBox sCount & " Back Ordered rows copied", vbInformation, "Transfer Done"
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2010-09-18
      • 2023-03-23
      相关资源
      最近更新 更多