将用户输入设置为工作簿

问题描述 投票:0回答:1

我对 VBA 非常陌生,无法找到解决我遇到的问题的方法。

我编写了一个宏,用于从工作簿文件夹中获取最后一行数据并将其放入活动工作簿中。但现在我正在尝试添加用户特定的工作簿作为此复制数据的目的地。

'Need help setting the user input string as the name of the workbook
Dim inputData As Variant
Dim userBook As Workbook
Dim destBook As Workbook
   
    'Get name of final destination Workbook
    inputData = InputBox("Enter the name of the destination workbook")
    
    'Set the user input as place holder workbook variable
    Set userBook = inputData
    
    'Set your master workbook as the placeholder workbook
    Set destBook = userBook

我认为我要么 1) 需要一种方法来更改变量 inputData 最初存储的类型,但不确定哪种类型适合我的目的,或者 2) 仅在目标工作簿中运行宏。

整个宏也在这里,以防我在尝试进行更改时错过了一些其他干扰。

Sub Copying()
'
' Copying Macro
'

Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim destSheet As Worksheet
Dim CopyLastRow As Long
Dim fileName As String
Dim sheetName As String
Dim folderPath As String
Dim lastRow As Long

'Need help setting the user input string as the name of the workbook
Dim inputData As Variant
Dim userBook As Workbook
Dim destBook As Workbook
   
    'Get name of final destination Workbook
    inputData = InputBox("Enter the name of the destination workbook")
    
    'Set the user input as place holder workbook variable
    Set userBook = inputData
    
    'Set your master workbook as the placeholder workbook
    Set destBook = userBook
    
    
    
    
    'Set the master "Data" worksheet
    Set destSheet = destBook.Sheets("Sheet1")
    
    'Specify the folder path where your source workbooks are located
    folderPath = "C:\Users\USKAALV1\Documents\FlexLogger\data\"

    'Get the file name from the folder
    fileName = Dir(folderPath & "*.csv")

Do While fileName <> ""
        'Get the sheet name from the file name
        sheetName = Left(fileName, Len(fileName) - 4)
    
        'Open the workbook
        Set sourceBook = Workbooks.Open(folderPath & fileName)
        
        'Set the source worksheet
        Set sourceSheet = sourceBook.Sheets(sheetName)
        
        'Find the last used row in the source worksheet
        lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
        
        'Copy range from source worksheet
        sourceSheet.Range("A" & lastRow & ":D" & lastRow).Copy
        
        'Find the last row in the master "Data" sheet
        lastRow = destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row
        
        'Paste into the master "Data" worksheet
        destSheet.Cells(lastRow + 1, 1).PasteSpecial xlPasteValues
        
        'Clear clipboard
        Application.CutCopyMode = False
        
        'Close the source workbook without saving
        sourceBook.Close SaveChanges:=False
        
        'Cycle to the next file in the folder
        fileName = Dir
Loop
    
excel vba
1个回答
0
投票

您的脚本即将完成。

我猜用户输入(inputData)是源文件(csv)的关键字。您的脚本尝试将数据从 csv 复制到 ActiveWorkbook 上的 Sheet1。

Sub Copying()
    
    ' your code

    'Get name of final destination Workbook
    inputData = InputBox("Enter the name of the destination workbook")
     ' **
    If Len(inputData) = 0 Then 
        MsgBox "Please input the source file."
        Exit Sub
    End If
     ' **
    'Set the user input as place holder workbook variable
    Set userBook = inputData
    'Set your master workbook as the placeholder workbook
    Set destBook = ActiveWorkbook ' or ThisWorkbook ' **
    'Set the master "Data" worksheet
    Set destSheet = destBook.Sheets("Sheet1")
    'Specify the folder path where your source workbooks are located
    folderPath = "C:\Users\USKAALV1\Documents\FlexLogger\data\"
    'Get the file name from the folder
    fileName = Dir(folderPath & "*" & inputData & "*.csv") ' **
    Do While fileName <> ""

            ' your code
            
    Loop
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.