วิธีที่จะทำให้พาธไปยังโฟลเดอร์รูปแบบสากล?

0

คำถาม

ใหม่ที่จะต้อง VBA และมีงานต้องสร้างเรือดำน้ำนั่น pastes จากหนึ่ง workbook เข้าไปใหม่ workbook. เป็สำคัญที่สุสำหรับการจัดเก็บแฟ้มนั่นคือ"ที่พาธไปยังโฟลเดอร์เป็นรูปแบบสากลดังนั้นคนอื่นสามารถสร้างโฟลเดอร์นี้ด้วย". สิ่งที่แก้ไขฉันจะทำให้ ActiveWorkbook.SaveAs วิธีการเพื่อเติมเต็มความปรารถนาเรื่องนี้? ขอบคุณ

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 ระบบตัวแปรจะปรับตัวมันขึ้นอยู่กับหน้าต่างบัญชีผู้ใช้ที่ใช้งานอยู่แล้ว แค่ทำให้แน่ใจว่างแต่ละผู้ใช้มีพวกนั้นโฟลเดอร์ที่มีอยู่บนของพื้นที่ทำงานเหมือนกันหรือคุณจะได้ข้อผิดพลาด ฉันยังถูกเอาออก names จากโฟลเดอร์ชื่อที่ผมถือว่าคุณกำลังพยายามทำอะไรบางอย่างกับชื่อผู้ใช้ที่รออยู่ที่นั่นด้วย คุณสามารถที่จะปรับนั่นของคุณต้องการ

ของรูปแบบวันที่ที่ต้องการจะเปลี่ยนด้วยอย่างที่มันเป็นผิดกฎหมายรวมถึงตัวอักษร

คุณยังลืมให้รวมเป็นส่วนขยายของเอกสารดังนั้นผมแค่เพิ่มนั่นเช่นกัน

มันมีเรื่องเกิดขึ้นมากมายกับสายนั้นรวมถึงหลายๆอย่างพลาดดังนั้นคุณจะต้องเล่นกับมันนิดหน่อจนกว่าคุณเข้าใจสิ่งที่คุณต้องการนะ คุณอาจจะต้อง simplify มันนิดหน่อจนกว่านายจะได้แขวนคอของทุกสิ่งทุกอย่าง

2021-11-24 06:52:45
0

ฉันคิดว่าคุณต้องเพิ่มอีกหน่อเช็ค

สคริปต์หวังว่าชื่อของเครื่องมือ-ทาง-โฟลเดอร์เป็นอย่างต่อเนื่อง ToolFolder.

อีกอย่างที่สองอย่างต่อเนื่อง ToolBaseFolder มันอาจถูกตั้งค่าเป็นพ่อแม่คน-อเส้นทาง`ToolFolder,e.g. องเครือข่ายเส้นทางเดินของตัวเอง ถ้า 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่