하는 방법 폴더 만들기 경로 universal?

0

질문

새로운 VBA 고 있는 할당을 만들기위는 페이스트에서 하나의 통합으로 새로운 통합 문서입니다. 에 대한 요구 사항은 파일을 저장하는"폴더를 경로 보편적인 것 그래서 다른 사람들 수 있는 이 폴더를 만들이 너무". What 수정안은 내가 만들 ActiveWorkbook.다른 이름으로 수행하기 위한 방법 이? 감사

Sub pasteTable()

    Dim formatting As Variant 'create variable to hold formatting2 workbook path
    formatting = Application.GetOpenFilename()  'user is prompted and selects path to formatting2 workbook and assigns to formatting variable
    
    Workbooks.Open formatting  'formatting2 workbook is now active
    Worksheets("Formatting").Range("B3:R13").Copy  'copies table from formatting2 workbook
    Workbooks.Add  'add new workbook
    
    Worksheets(1).Range("B3:R13").Select  'selects range on worksheet of new workbook to paste table
    Selection.PasteSpecial xlPasteAll 'pastes table
    
    Columns("B:R").ColumnWidth = 20  'ensures table has proper row and column heights/widths
    Rows("3:13").RowHeight = 25
    
    Worksheets(1).Name = "Table Data"  'renames worksheet
        
    ActiveWorkbook.SaveAs "C:\Users\name\Desktop\names Excel Assessment VBA\names Excel Assessment VBA " & Format(Date, "dd/mmm/yyyy"), FileFormat:=xlOpenXMLWorkbookMacroEnabled
    'saves workbook according to desired specifications
End Sub
excel vba
2021-11-24 03:27:40
2
0

변경을 저장하는 라인이:

ActiveWorkbook.SaveAs "C:\Users\" & Environ("Username") & "\Desktop\Excel Assessment VBA\Excel Assessment VBA " & Format(Date, "dd-mmm-yyyy") & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

Username 시스템 변수에 따라 조정 Windows 계정에 사용. 지만 확인하면 각 사용자가 해당 폴더는 기존의 데스크톱에서 너무,또는 당신을 얻을 것이 오류가 있습니다. 또한 제거 names 에서 폴더 이름으로 나는 가정 당신이 뭔가를 시도하고 사용자 이름으로도 알 수 있습니다. 을 조정할 수 있습니다.

귀하의 형식으로 날짜를 변경하는 데 필요한도로 등 잘못된 문자가 있습니다.

당신은 또한 잊 포함할 파일의 파일 확장자,파일명서를 추가했을 뿐입니다.

이 많이 가는 라인을 포함한 많은 실수를,그래서 당신은 플레이와 함께 그것을 조금 얻을 때까지 정확하게 당신이 필요합니다. 패턴을 단순화할 수 있습 그것을 조금 얻을 때까지의 그 모든 것들입니다.

2021-11-24 06:52:45
0

나는 생각이 있을 추가하는 좀 더 확인

스크립트를 예상하는 도구의 이름-경로의 폴더로서 일정한 ToolFolder.

플러스 두번째 일정 ToolBaseFolder 할 수 있는 설정한 부모-경로를`ToolFolder,예를 들어 네트워크 경로에 있습니다. 는 경우 const 비어,사용자 데 사용됩니다.

이 경로는 아직 존재하지 않습니다 만들 수 있습니다.

Option Explicit

Private Const ToolBaseFolder As String = "" 'if ToolBaseFolder is an empty string desktop will be used instead
Private Const ToolFolder As String = "MyNameForToolFolder"


Public Sub testWbToToolFolder()
'this is just for testing
Dim wb As Workbook: Set wb = ActiveWorkbook
saveWbToToolFolder wb, "test.xlsx"
End Sub


Public Sub saveWbToToolFolder(wb As Workbook, filename As String)
'you don't need this sub - but have the same code line in your main routine
wb.SaveAs getToolFolder & filename
End Sub



Public Function getToolFolder() As String
'this returns the toolfolder e.g. C:\Users\xyz\Desktop\MyNameForToolFolder

Dim basepath As String
basepath = ToolBaseFolder & "\"

If existsFolder(basepath) = False Then
    If LenB(ToolBaseFolder) > 0 Then
        MsgBox ToolBaseFolder & " does not exist." & vbCrLf & _
            "File will be saved to " & ToolFolder & " on desktop ", vbExclamation
    End If
    basepath = getDesktopFolderOfUser
End If

Dim fullpath As String
fullpath = basepath & ToolFolder & "\"

If existsFolder(fullpath) = False Then
    makeFolder fullpath
End If

getToolFolder = fullpath

End Function


Private Function existsFolder(path As String) As Boolean
If Len(path) < 2 Then Exit Function 'can't be a valid folder
existsFolder = LenB(Dir(path, vbDirectory)) > 0
End Function

Private Function getDesktopFolderOfUser() As String
getDesktopFolderOfUser = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
End Function

Private Function makeFolder(path As String)
'https://stackoverflow.com/a/26934834/16578424 plus comment from rayzinnz
CreateObject("WScript.Shell").Run "cmd /c mkdir """ & path & """", 0, True
End Function

2021-11-24 04:46:46

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................