Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用按钮导入CSV文件,但需要将其放到另一张图纸上_Vba_Excel_Csv - Fatal编程技术网

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

Vba 使用按钮导入CSV文件,但需要将其放到另一张图纸上,vba,excel,csv,Vba,Excel,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

我已经得到了代码,它可以将数据复制到活动工作表中,现在我希望将数据粘贴到另一个工作表(导入的数据)上。这必须从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
子导入按钮()
Dim ResultStr As字符串
将文件名设置为字符串
Dim FileNum作为整数
双色暗计数器
'要求用户输入文件名
filename=InputBox(“请输入.csv文件的完整路径(包括目录)”)
'检查是否有人进入
如果filename=”“,则结束
'获取下一个可用的文件句柄号
FileNum=FreeFile()
'打开文本文件进行输入
打开文件名作为#FileNum输入
'关闭屏幕更新
Application.ScreenUpdating=False
'将计数器设置为1
计数器=1
'循环,直到到达文件末尾

Do While Seek(FileNum)在其中写入值并添加图纸参考图纸(“导入数据”)

希望这有帮助


Mucio

可能更好的方法是使用表单(“Sheet2”),然后只使用.Cells(计数器+5,1)=Replace(splitValues(0),Chr(34),“”)等等
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), "")