0

我有代码,可以将数据复制到活动工作表,现在我希望将数据粘贴到另一个工作表(导入数据)上。这必须从 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
4

1 回答 1

0

在您写入值的位置添加工作表参考 Sheets("Imported Data")

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), "")

希望这可以帮助

穆乔

于 2013-10-09T08:57:05.770 回答