【问题标题】:How to Copy and paste my data in the selected file when I use EXCEL VBA使用 EXCEL VBA 时如何将数据复制并粘贴到所选文件中
【发布时间】:2015-01-20 03:54:02
【问题描述】:

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

我正在使用以下代码使用excel VBA命令按钮打开一个txt文件,但它作为一个单独的txt文件打开,我需要这个打开的txt文件的第一张自动选择到剪贴板并粘贴到我正在使用的模板。并关闭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

【问题讨论】:

标签: vba excel


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2019-11-25
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多