Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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
Vlookup在循环过程中更改保存文件名VBA_Vba_Excel_Vlookup - Fatal编程技术网

Vlookup在循环过程中更改保存文件名VBA

Vlookup在循环过程中更改保存文件名VBA,vba,excel,vlookup,Vba,Excel,Vlookup,所有-我想写一个循环,我可以改变文件名和文件夹的位置,这取决于它在循环中运行的值。例如,如果我从单元格G2:G7运行宏,当进程从G2移动到G3时,我希望文件名和文件夹位置根据某个参考表进行更改(查看图像了解详细信息)。实际上,我希望filename和foldername是对基金类型的查找 Public Sub Get_File() Dim sFiletype As String Dim sFilename As String 'Save the file as this

所有-我想写一个循环,我可以改变文件名和文件夹的位置,这取决于它在循环中运行的值。例如,如果我从单元格G2:G7运行宏,当进程从G2移动到G3时,我希望文件名和文件夹位置根据某个参考表进行更改(查看图像了解详细信息)。实际上,我希望filename和foldername是对基金类型的查找

Public Sub Get_File()

    Dim sFiletype As String
    Dim sFilename As String     'Save the file as this name, if "" then default
    Dim sFolder As String       'Save to this folder, if "" then default
    Dim bReplace As Boolean     'To replace the existing file or not
    Dim sURL As String          'The URL to the location to extract information
    Dim cell, Rng As Range
    Dim sheet As Worksheet

    'Initialize variables

    Set Rng = Range("I2:I10")
    Set sheet = ActiveWorkbook.Sheets("Macro_Button")

    For Each cell In Rng
        If cell <> "" Then
        sFiletype = cell.Value
        sFilename = sFiletype & "_" & Format(Date, "mmddyyyy")

        sFolder = Application.WorksheetFunction.VLookup(sFiletype, sheet.Range("G2:J10"), 4, False)
        bReplace = True
        sURL = "www.preqin.com"

        'Download using the desired approach, XMLHTTP / IE
        Call Download_Use_IE(sURL, sFilename, sFolder, bReplace)

        Else
        Exit Sub
        End If
    Next

End Sub
Public子Get_文件()
将sFiletype设置为字符串
Dim sFilename As String“将文件另存为此名称,如果为”,则为默认值
Dim S文件夹为字符串“保存到此文件夹,如果为”,则为默认值
是否替换现有文件
Dim sURL As String“提取信息的位置的URL”
暗淡单元格,Rng As范围
将工作表设置为工作表
'初始化变量
设置Rng=范围(“I2:I10”)
Set sheet=ActiveWorkbook.Sheets(“宏按钮”)
对于Rng中的每个单元
如果是单元格“”,则
sFiletype=单元格.Value
sFilename=sFiletype&“\uyyyy”格式(日期,“mmddyyyy”)
sFolder=Application.WorksheetFunction.VLookup(sFiletype,sheet.Range(“G2:J10”),4,False)
bReplace=True
sURL=“www.preqin.com”
'使用所需的方法下载,XMLHTTP/IE
呼叫下载\使用\ IE(sURL、sFilename、sFolder、bReplace)
其他的
出口接头
如果结束
下一个
端接头
谢谢大家的意见


其中存在
VLookUp
的限制,其中匹配项必须与最左边的列对齐,并且只能向右搜索。如建议中的,考虑<代码>索引/匹配< /代码>替换,将列与列进行比较,并在匹配行(在任意方向)上返回值:

如果需要使用
VLookUp()
,则需要减小查找范围:

sFolder = Application.WorksheetFunction.VLookup(sFiletype, sheet.Range("I2:J10"), 2, False)

我建议不要使用
vlookup
,而是将确切的文件夹放入代码中,因为您只有8个选择。这使得调试变得显而易见。您可以通过
case
语句执行此操作。有关更多信息,请参阅

Select Case sFilename

Case abc
    sFolder = "C:\One\"
Case def
    sFolder = "C:\Two\"
Case ghi
    sFolder = "C:\Three\"
'so forth for 8 cases...

End Select

你能把这个密码缩小到你遇到麻烦的地方吗?因此,鼓励一种新的方法,但也建议精确定位相关部分。-以上是我正在使用的,但这似乎不起作用。我目前被困在vlookup行中,不断收到运行时错误“1004”,无法获取worksheetfunction类的vlookup属性
Select Case sFilename

Case abc
    sFolder = "C:\One\"
Case def
    sFolder = "C:\Two\"
Case ghi
    sFolder = "C:\Three\"
'so forth for 8 cases...

End Select