使用 EXCEL VBA 时如何将我的数据复制并粘贴到所选文件中

How to Copy and paste my data in the selected file when I use EXCEL VBA

我正在使用 Excel VBA 作为新 VBA 用户,很多人帮助我克服了挑战。

我正在使用以下代码使用 excel VBA 命令按钮打开一个 txt 文件,但它作为一个单独的 txt 文件打开,我需要第一个 sheet打开 txt 文件自动 select 到剪贴板并粘贴到我正在使用的模板中。并关闭 txt 文件,因为它不是必需的。

这个txt文件的文件名不是唯一的,它应该作为变量记录在程序内存中(我相信是字符串)

Private Sub CommandButton1_Click()
With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "I:\Group"
    .Filters.Clear
    .Title = "Your Title"
    If Not .Show Then
        MsgBox "No file selected.": Exit Sub
    End If
    Workbooks.OpenText .SelectedItems(1), Origin:=xlMSDOS, StartRow:=23, DataType:=xlFixedWidth, FieldInfo:= _
            Array(Array(0, 1), Array(6, 2), Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _
            Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), TrailingMinusNumbers:= _
            True
End With
End Sub
Private Sub CommandButton1_Click()
 With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "I:\Group"
    .Filters.Clear
    .Title = "Your Title"
    If Not .Show Then
        MsgBox "No file selected.": Exit Sub
    End If
    Workbooks.OpenText .SelectedItems(1), Origin:=xlMSDOS, StartRow:=23, DataType:=xlFixedWidth, FieldInfo:= _
            Array(Array(0, 1), Array(6, 2), Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _
            Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), TrailingMinusNumbers:= _
            True

'The below is to copy the text file into a new sheet in the workbook and paste those values in sheet 1

    Set myfile = ActiveWorkbook
    ActiveWorkbook.Sheets(1).Copy _
    after:=ThisWorkbook.Sheets(1)
    myfile.Close
    ActiveSheet.Cells.Select
    Selection.Copy
    Sheets("Sheet1").Select
    Cells.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets("Sheet2").Select
    Application.CutCopyMode = False
End With
End Sub