我正在尝试创建VBScript,它将从Excel的一个选项卡中获取数据,并且该数据将替换Excel工作表的另一个选项卡中可用的特定字符串。
在“查询”标签中,有一列“查询”,该列的位置为B或2,其中包含下面的字符串。
Select account from table1 where columname=<<field>>
在数据选项卡中,“字段”变量可用,包含值10。
<<field>>
需要替换为该值10,因此上面的查询需要这样显示:
Select account from table1 where columname=10
我已经创建了这个VBScript:
Set objexcel = CreateObject("excel.application")
objexcel.Visible = True
Set wb = objexcel.Workbooks.Open("C:\Users\admin\Desktop\Datasheet.xlsx")
Set ws = wb.Worksheets("Query")
Row = ws.UsedRange.Rows.Count
For i = 2 To Row
finalvalue = ws.Cells(i, 2).Value
splitvalues = Split(finalvalue, " ")
For j = 0 To UBound(splitvalues)
correctvalue = splitvalues(j)
If InStr(correctvalue, ">>") > 0 Then
variablename = Trim(Replace(Replace(correctvalue, "<<", " "), ">>", " "))
Set ws = wb.Worksheets("Data")
Col = ws.UsedRange.Columns.Count
Row2 = ws.UsedRange.Rows.Count
For k = 2 To Row2
If variablename = ws.Cells(k, 1).Value Then
For P = 2 To Col
temp2 = ws.Cells(k, P).Value
Latestvalue = Trim(Replace(variablename, temp2))
Set fsoObject = CreateObject("Scripting.FileSystemObject")
filepath = "C:\Users\admin\Desktop"
Set MyFile =fsoObject.CreateTextFile("C:\Users\admin\Desktop\Query.p", 1, True)
MyFile.Write Latestvalue
MyFile.Close
Next
End If
Next
Set MyFile = Nothing
Set fsoObject = Nothing
End If
Next
Next
wb.Close
objexcel.Quit
Set objexcel = Nothing
尝试运行vbs文件时出现错误:
“意外的下一个”
我也不知道我是否能够从“查询”标签导航到“数据”标签。