Macro to save active Sheet as new workbook, ask user for location and remove macros from the new workbook

前端 未结 3 1165
时光说笑
时光说笑 2021-01-20 03:50

I have a Workbook with three WorkSheets: Product , Customer, Journal. What I need is a macro assigned to a button within each one of the above Sheets. If the button is click

相关标签:
3条回答
  • 2021-01-20 04:18

    To piggyback on Lunatik's suggestion, you might add this:

    MyPath = Application.GetSaveAsFilename(FILEFILTER:="Excel Files (*.xls), *.xls", Title:="Something really clever about saving")
    
    If MyPath <> False Then
        ActiveWorkbook.SaveAs (MyPath)
    End If
    

    GetSaveAsFilename returns FALSE if the user hits cancel. You can also supply a default filename.

    This is a taste thing, but Format(Date, "dd.mm.yyyy") could replace your method.

    0 讨论(0)
  • 2021-01-20 04:26

    Another appoach: SHBrowseForFolder

    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const MAX_PATH = 260
    
    Private Declare Function SHBrowseForFolder Lib _
    "shell32" (lpbi As BrowseInfo) As Long
    
    Private Declare Function SHGetPathFromIDList Lib _
    "shell32" (ByVal pidList As Long, ByVal lpBuffer _
    As String) As Long
    
    
    Private Type BrowseInfo
       hWndOwner As Long
       pIDLRoot As Long
       pszDisplayName As Long
       lpszTitle As Long
       ulFlags As Long
       lpfnCallback As Long
       lParam As Long
       iImage As Long
    End Type
    
    
    Private Function Show_Save_WorkSheet() As String
    Dim lpIDList As Long
    Dim sBuffer As String
    Dim szTitle As String
    Dim tBrowseInfo As BrowseInfo
    
    szTitle = "Please, specify the location where you want the Worksheet to be stored"
    
    With tBrowseInfo
       .hWndOwner = Me.hWnd
       .lpszTitle = lstrcat(szTitle, "")
       .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
    End With
    
    lpIDList = SHBrowseForFolder(tBrowseInfo)
    
    If (lpIDList) Then
       sBuffer = Space(MAX_PATH)
       SHGetPathFromIDList lpIDList, sBuffer
       sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)       
       Show_Save_WorkSheet = sBuffer
    End If
    End Function
    
    0 讨论(0)
  • 2021-01-20 04:30

    The first one is simple. Use Application.GetSaveAsFilename to allow the user to nominate a path and filename.

    I've used the following from Chip Pearson to strip the VBA out of a copied workbook before, it should do what you are after:

    Sub DeleteAllVBACode()
            Dim VBProj As VBIDE.VBProject
            Dim VBComp As VBIDE.VBComponent
            Dim CodeMod As VBIDE.CodeModule
            
            Set VBProj = myWorkbook.VBProject
            
            For Each VBComp In VBProj.VBComponents
                If VBComp.Type = vbext_ct_Document Then
                    Set CodeMod = VBComp.CodeModule
                    With CodeMod
                        .DeleteLines 1, .CountOfLines
                    End With
                Else
                    VBProj.VBComponents.Remove VBComp
                End If
            Next VBComp
        End Sub

    Sorry, not got time to review your code in detail (leaving work!)

    0 讨论(0)
提交回复
热议问题