【问题标题】:VBA - Changing Copying from a Directory to copying from a serverVBA - 将复制从目录更改为从服务器复制
【发布时间】:2014-01-15 22:32:31
【问题描述】:

感谢任何帮助,如果我的术语不正确,请见谅。

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

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

如果我开始安排将其转储到 位置 B "http://172.1.2.3/Dir1/SubDir1/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

【问题讨论】:

    标签: vba excel csv directory


    【解决方案1】:

    这对我有用:

    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
    

    【讨论】:

    • tks Tim 我会试一试的。
    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2022-01-17
    • 1970-01-01
    • 2015-09-25
    • 2013-06-01
    相关资源
    最近更新 更多