gpt4 book ai didi

excel - 导入逗号分隔到特定工作表

转载 作者:行者123 更新时间:2023-12-04 22:01:10 25 4
gpt4 key购买 nike

我正在尝试将逗号分隔的文本文件导入工作簿中的特定工作表。我有导入它的代码,但我不知道如何告诉它转到已经创建的特定工作表,因为现在它只是创建一个新工作表并将数据转储在那里。

我的代码是

'Import Report

Dim vPath As Variant
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set wb = Excel.ActiveWorkbook
Set ws = Excel.Sheets("Data")
vPath = Application.GetOpenFilename("CSV (Comma Delimited) (*.csv),*.csv" _
, 1, "Select a file", , False)

''//Show the file open dialog to allow user to select a CSV file

If vPath = False Then Exit Sub

''//Exit macro if no file selected

Workbooks.OpenText Filename:=vPath, Origin:=xlMSDOS, StartRow:=1 _
, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, Comma:=True _
, FieldInfo:=Array(Array(1, xlTextFormat), Array(2, xlTextFormat), _
Array(3, xlTextFormat))

''//The fieldinfo array needs to be extended to match your number of columns

Columns.EntireColumn.AutoFit

''//Resize the columns

Sheets(1).Move Before:=wb.Sheets(1)

''//Move the data into the Workbook

我想我只需要添加类似的东西;
Destination:=Range("Sheet2!$A$1")

某处,但我不知道在哪里,我尝试过的所有地方都导致了错误

最佳答案

这是一个开始:

Sub CSV_Reader()
Dim vPath As Variant
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim rng As Range, TextLine As String
Dim rw As Long, col As Long
Dim i As Long, j As Long, ary() As String, a As Variant

Set wb = Excel.ActiveWorkbook

vPath = Application.GetOpenFilename("CSV (Comma Delimited) (*.csv),*.csv" _
, 1, "Select a file", , False)
MsgBox vPath
Set rng = Application.InputBox(Prompt:="Pick a Sheet and a Cell", Type:=8)
rng.Parent.Parent.Activate
rng.Parent.Activate
rw = rng(1).Row
col = rng(1).Column

Close #1
i = rw
Open vPath For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
ary = Split(TextLine, ",")
j = col
For Each a In ary
Cells(i, j).Value = a
j = j + 1
Next a
i = i + 1
Loop
Close 1

End Sub

关于excel - 导入逗号分隔到特定工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com