( I have edited this questions, see below ) I have searched on here and google but I still cannot solve my issue. I am trying to use an excel's named range equivalent in my .vbs file. This below works in VBA in excel but I cannot get it to work in a VBS file.
ThisWorkbook.Sheets(1).Range("A1:AX" & Range("AX" & Rows.Count).End(xlUp).Row).Name = "DATA"
strSql = "SELECT * FROM DATA"
So, I have tried different variations of referencing to DATA
, but with no luck.
This is what I have now:
' ws = worksheet, wb = workbook
Set rng = ws.Range("B" & i+1 & ":AX" & j-1)
Call createAndInsertRecordSet(wb.FullName, rng)
Sub CreateAndInsertRecordSet(ByVal fullname, ByRef rng)
' Create objects
Dim cn, rs, strCon, strSql, cn2
rng = "DATA"
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fullname & ";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"";"
strSql = "SELECT * FROM DATA"
And I have tried different variations like taking parameter ByVal, using rng instead of DATA (string type), "SELECT * FROM " & rng, etc..
The error msg when running:
Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.
C:\Users\admin\Desktop\UpdateSourceTbl.vbs(119, 5) Microsoft JET Databas e Engine: The Microsoft Jet database engine could not find the object 'DATA'. M ake sure the object exists and that you spell its name and the path name correctly.
Any help greatly appreciated!
EDIT:
The whole point of doing this using VBScript its to update a database with a single click on one icon ( ill have a script on the server that will run file automatically )
Below is a sample of what the Excel file looks like. I cant modify the design of the file. Also, I do not want to create a 'copy' that I could design as a relational database ( bad efficiency ). I just want to stick to this design, ok? :) note the empty rows between records, different lengths of each records. I am only interested in uploading a few columns though (date,wo,operator,operation,start time, check time, both amounts of parts, breaks - the rest can be calculated) -. So I thought of creating a recordset for each record ( record = data in between spaces in rows ) and exclusively choosing only recordset.Fields(n) that I want.
Code: ( .vbs )
So, I am opening the excel file and looping through all rows on all worksheets. finding first and last row for each record, and trying to dynamically name found Range and upload it to my database. I have taken out lines here, and there to make the code as simple as required to understand what im doing.
Option Explicit
'Declare
Dim xl, wb, fPath, rng
'Path to file
fPath = "C:\Users\admin\Desktop\Source\source2.xlsm"
'Process
Call OpenFile (xl, wb, fPath)
Call UpdateSourceTbl (wb)
Call CloseFile (xl, wb)
'Open and link excel file
Sub OpenFile(ByRef xl, ByRef wb, ByVal fPath)
'create an excel instance
Set xl = CreateObject("Excel.Application")
xl.Visible = False
'open workbook
Set wb = xl.Workbooks.Open(fPath)
End Sub
'Update Source Table in the db
Sub UpdateSourceTbl(ByRef wb)
'declare locals
Dim ws, lastRow, i
'iterate through all wSheets
'for i = 1 to wb.Sheets.Count-4
for i = 1 to 1
'link to sheet
Set ws = wb.Sheets(i)
'get last row used in the active sheet
lastRow = ws.UsedRange.Rows.Count
'run engine
if lastRow >= 2 then Call ProcEachRst(ws, lastRow)
Set ws = Nothing
next
End Sub
'Proccess Each Recordset
Sub ProcEachRst(ByRef ws, byVal lastRow)
dim i, j
'for i = 2 to lastRow
for i = 2 to 30
'first and -one line- record
if i = 2 and ws.Range("B" & i+1).Value = "" then
' named range
Set rng = ws.Range("B2:AX2")
ws.Range("B2:AX2") = "MyRange"
Call createAndInsertRecordSet(wb.FullName)
elseIf (i = 2 and ws.Range("B" & i+1).Value <> "" ) or (ws.Range("B" & i).Value = "" and ws.Range("B" & i+1).Value <> "") then
j = i + 1
do until ws.Range("B" & j).Value = ""
j = j + 1
loop
if i = 2 then
'always starts at row 2
Set rng = ws.Range("B2:AX" & j-1)
ws.Range("B2:AX" & j-1 & ") = "MyRange"
Call createAndInsertRecordSet(wb.FullName)
else
'add 1 to i - first record ( i > 2 )
Set rng = ws.Range("B" & i+1 & ":AX" & j-1)
ws.Range("B" & i+1 & ":AX" & j-1 & ") = "MyRange"
Call createAndInsertRecordSet(wb.FullName)
end if
end if
' release rng ref
set rng = Nothing
next
End Sub
'Close and unlink excel file
Sub CloseFile(ByRef xl, ByRef wb)
xl.Quit
Set wb = Nothing
Set xl = Nothing
End Sub
' SQL Server 2008 connection
Sub CreateAndInsertRecordSet(ByVal fullname)
' Create objects
Dim cn, rs, strCon, strSql, cn2
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fullname & ";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"";"
strSql = "SELECT * FROM myRange"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
' Connect to workbook
cn.Open strCon
' Execute sql query on open workbook
rs.Open strSql, cn
' Execute new query
' *Inserts Recordset to db
Set cn2 = CreateObject("ADODB.Connection")
With cn2
.CursorLocation = adUseClient
.Open "Driver={SQL Server};Server=SERVERPATH; Database=prod_rep; UID=ADMIN; PWD=PASSWORD"
.CommandTimeout = 0
rs.MoveFirst
Do While Not rs.EOF
.Execute "INSERT INTO prod_rep.dbo.report ( [date], [wo], [operator], [operation], [start time], [check time], [amountST], [amountCHK] ) VALUES ('" & rs.Fields(0) & "', '" & rs.Fields(3) & "', '" & rs.Fields(9) & "', " & rs.Fields(12) & "', " & rs.Fields(21) & "', " & rs.Fields(24) & "', " & rs.Fields(30) & "', " & rs.Fields(33) & "', " & rs.Fields(45) & ")"
rs.MoveNext
Loop
End With
' Close connections
' Unlink
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
cn2.Close
Set cn2 = Nothing
End Sub
EDIT2: I used 2 parameters with row numbers, may not be the best solution - but it works! and i cant see nothing wrong with it to be fair
Call createAndInsertRecordSet(wb.FullName, ws.Name, i+1, j-1)
Sub CreateAndInsertRecordSet(ByVal fullname, ByVal wsName, ByVal stRow, byVal enRow )
strSql = "SELECT * FROM [" & wsName & "$B" & stRow & ":AX" & enRow & "]"
xlUp
is not defined outside of Excel. You need to replace it with its actual value (which you can find using the object browser in excel (F2 in the VB Editor). It would help to update your question with the complete VBscript you're using. – Tim Williams Apr 5 at 16:22