VBA - 将目录中的复制更改为从服务器复制

时间:2014-01-15 22:32:31

标签: vba excel-vba csv directory excel

如果我的术语不正确,请提供帮助,请原谅。

这是一个从 loacation A 打开文件并复制指定内容的基本宏, 然后,它将内容粘贴到已运行此宏的当前工作簿,指定的工作表和单元格中。

我的问题围绕“FileName.csv”。目前计划定期将其转储到位置A“V:\ Dir1 \ SubDir1 \”

如果我开始安排将其转储到 loacation B "http://172.1.2.3/Dir1/SubDir1/FileName.csv" 服务器中,我将如何检索此文件“FileName.csv”某种程度? 我只是想编辑现有的宏以允许这种改变。

Sub CopyCSVFile1()

'workbook to copy from
WBToCopy = "FileName.csv"
'workbook path to copy from
WBpthToCopy = "V:\Dir1\SubDir1\"
'workbook to paste to
'WBToPasteTo = "ResourcesV1.xlsm" not needed here as pasting to active workbook
'workbook sheet to paste to
WBSheetToPasteTo = "Raw1"
''workbook path to paste to
'WBPthToPasteTo = "N:\Engineering\Network Performance\Capacity\SG_GG\SGSN Resources\" ' not needed here as pasting to active workbook
'range to select to copy
RangeToSelectToCopy = "A3:B149"
'cell to paste to
CellToPasteTo = "A3" ' need to work this out before assignment

Dim Merged As Object
Dim Data As Object
Set Data = ActiveWorkbook

'debug.print "ActiveWorkbook.Path = " & ActiveWorkbook.Path
Debug.Print "ActiveWorkbook.Path = " & Data.Path

Sheets(WBSheetToPasteTo).Select ' this is the sheet where you want to paste to
Workbooks.Open Filename:=WBpthToCopy & WBToCopy, local:=True

Set Merged = ActiveWorkbook ' this assigns the current active workbook to merged whish is the one I want to copy from

Range(RangeToSelectToCopy).Select ' this value just for this example should be A4 normally
Selection.Copy

Data.Activate ' this activates the Data workbook which happens to be the workbook where this macro resides

Range(CellToPasteTo).Select ' select where I want to past my data

ActiveSheet.Paste ' paste the data
Application.DisplayAlerts = False
Merged.Close 'SaveChanges = False

Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

这对我有用:

Sub CopyCSVFile1()

    Dim wb As WorkBook, WBToCopy As String, WBpthToCopy As String

    'workbook to copy from
    WBToCopy = "test.csv"

    'workbook path to copy from
    WBpthToCopy = "http://127.0.0.1/testpages/"

    'open the source workbook
    Set wb = Workbooks.Open(WBpthToCopy & WBToCopy)

    '...
    'do something with wb...
    '...

    wb.Close False

End Sub