使用按钮导入CSV文件,但需要将其拖放到另一张纸上

时间:2013-10-09 08:16:03

标签: excel vba excel-vba csv

我已经获得了将数据复制到活动工作表的代码,现在我希望将数据粘贴到另一个工作表(导入的数据)上。这必须从A2开始。 有人可以帮我吗?

Sub ImportButton()

      Dim ResultStr As String
      Dim filename As String
      Dim FileNum As Integer
      Dim Counter As Double
      'Ask User for File's Name
      filename = InputBox("Please enter the entire path to the .csv file(including directory)")
      'Check for no entry
      If filename = "" Then End
      'Get Next Available File Handle Number
      FileNum = FreeFile()
      'Open Text File For Input
      Open filename For Input As #FileNum
      'Turn Screen Updating Off
      Application.ScreenUpdating = False
      'Set The Counter to 1
      Counter = 1

      'Loop Until the End Of File Is Reached
      Do While Seek(FileNum) <= LOF(FileNum)
         'Display Importing Row Number On Status Bar
          Application.StatusBar = "Importing Row " & _
          Counter & " of text file " & filename
          'Store One Line Of Text From File To Variable
          Line Input #FileNum, ResultStr

          'Store Variable Data Into Active Cell
          Dim splitValues As Variant
          splitValues = Split(ResultStr, ",")
          Cells(Counter + 5, 1) = Replace(splitValues(0), Chr(34), "")
          Cells(Counter + 5, 2) = Replace(splitValues(1), Chr(34), "")
          Cells(Counter + 5, 3) = Replace(splitValues(2), Chr(34), "")
          Cells(Counter + 5, 4) = Replace(splitValues(3), Chr(34), "")
          Cells(Counter + 5, 5) = Replace(splitValues(4), Chr(34), "")
          Cells(Counter + 5, 6) = Replace(splitValues(5), Chr(34), "")
          Cells(Counter + 5, 7) = Replace(splitValues(6), Chr(34), "")
          Counter = Counter + 1
      'Start Again At Top Of 'Do While' Statement
      Loop
      'Close The Open Text File
      Close
      'Remove Message From Status Bar
      Application.StatusBar = False
      MsgBox ("Records successfully imported")
End Sub

1 个答案:

答案 0 :(得分:0)

在哪里写入值添加工作表参考表(“导入的数据”)

Sheets("Imported Data").Cells(Counter + 5, 1) = Replace(splitValues(0), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 2) = Replace(splitValues(1), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 3) = Replace(splitValues(2), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 4) = Replace(splitValues(3), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 5) = Replace(splitValues(4), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 6) = Replace(splitValues(5), Chr(34), "")
Sheets("Imported Data").Cells(Counter + 5, 7) = Replace(splitValues(6), Chr(34), "")

希望这有帮助

Mucio