yop6
asked on
Creating Named Ranges in Excel Worksheet for columns A:O
I'm using a loop to loop through 100+ workbooks to push Qrys to respective files. A new query was added and I want to create a named range within the loop for each individual file one the same worksheet in reference to the same columns
I have a string variable set for each file path "PATHx"
The worksheets are name "xwalk"
The number of rows in each query varies but the data need to be dropped in columns A:O without headers from starting in A1
I have a string variable set for each file path "PATHx"
The worksheets are name "xwalk"
The number of rows in each query varies but the data need to be dropped in columns A:O without headers from starting in A1
Did the answer provided fixed your issue ? or still having a problem ?
gowflow
gowflow
ASKER
the code from @capricorn1 works with some file and others it does not. I'm not sure as to why. Any idea?
< works with some file and others it does not>
well, you have to inform us why you think it did not work, what is not working...
well, you have to inform us why you think it did not work, what is not working...
ASKER
Dim VBA_16 As dao.Database
Set VBA_16 = CurrentDb
Dim arrayIDDA() As Variant
Dim rstIDDA As dao.Recordset
Set rstIDDA = VBA_16.OpenRecordset("SELE CT catbTSx, PATHx FROM tblIDDA", dbOpenDynaset)
With rstIDDA
.MoveLast
.MoveFirst
arrayIDDA = rstIDDA.GetRows(.RecordCou nt)
End With
rstIDDA.Close
Set rstIDDA = Nothing
Dim arrayREP() As Variant
Dim rstREP As dao.Recordset
Set rstREP = VBA_16.OpenRecordset("SELE CT catbTSx, PATHx FROM tblREP", dbOpenDynaset)
With rstREP
.MoveLast
.MoveFirst
arrayREP = rstREP.GetRows(.RecordCoun t)
End With
rstREP.Close
Set rstREP = Nothing
'Looping through rstGWA using arrayREP for variables
Dim rstxwalk As dao.Recordset
Set rstxwalk = VBA_16.OpenRecordset("Qry_ sf133xwalk ")
Dim catbTSx As String
Dim PATHx As String
Dim QRYx As String
Dim qdfQRY As dao.QueryDef
Dim rs As dao.Recordset, xlObj As Object
Set rs = CurrentDb.OpenRecordset("Q ry_sf133xw alk")
For i = 0 To 55
catbTSx = arrayIDDA(0, i)
PATHx = arrayIDDA(1, i)
Set xlObj = CreateObject("excel.applic ation")
xlObj.workbooks.Open PATHx
With xlObj
.worksheets("sf133xwalk"). select
.range("A6").copyfromrecor dset rs
.activeworkbook.Save
End With
xlObj.Quit
Next
For i = 0 To 113
catbTSx = arrayREP(0, i)
PATHx = arrayREP(1, i)
Set xlObj = CreateObject("excel.applic ation")
xlObj.workbooks.Open PATHx
With xlObj
.worksheets("sf133xwalk"). select
.range("A6").copyfromrecor dset rs
.activeworkbook.Save
End With
xlObj.Quit
Next
End Function
Set VBA_16 = CurrentDb
Dim arrayIDDA() As Variant
Dim rstIDDA As dao.Recordset
Set rstIDDA = VBA_16.OpenRecordset("SELE
With rstIDDA
.MoveLast
.MoveFirst
arrayIDDA = rstIDDA.GetRows(.RecordCou
End With
rstIDDA.Close
Set rstIDDA = Nothing
Dim arrayREP() As Variant
Dim rstREP As dao.Recordset
Set rstREP = VBA_16.OpenRecordset("SELE
With rstREP
.MoveLast
.MoveFirst
arrayREP = rstREP.GetRows(.RecordCoun
End With
rstREP.Close
Set rstREP = Nothing
'Looping through rstGWA using arrayREP for variables
Dim rstxwalk As dao.Recordset
Set rstxwalk = VBA_16.OpenRecordset("Qry_
Dim catbTSx As String
Dim PATHx As String
Dim QRYx As String
Dim qdfQRY As dao.QueryDef
Dim rs As dao.Recordset, xlObj As Object
Set rs = CurrentDb.OpenRecordset("Q
For i = 0 To 55
catbTSx = arrayIDDA(0, i)
PATHx = arrayIDDA(1, i)
Set xlObj = CreateObject("excel.applic
xlObj.workbooks.Open PATHx
With xlObj
.worksheets("sf133xwalk").
.range("A6").copyfromrecor
.activeworkbook.Save
End With
xlObj.Quit
Next
For i = 0 To 113
catbTSx = arrayREP(0, i)
PATHx = arrayREP(1, i)
Set xlObj = CreateObject("excel.applic
xlObj.workbooks.Open PATHx
With xlObj
.worksheets("sf133xwalk").
.range("A6").copyfromrecor
.activeworkbook.Save
End With
xlObj.Quit
Next
End Function
ASKER
^After looping through both arrays, it appears the files have been edited. When I open to verify, the data from the query has not been pasted on "sf133xwalk" in cell A6.
How would I go about creating a named range inside the For Next from A6:O5000 named "xwalk" for each individual file? I feel like this will be most effective in combination with a DoCmd export
How would I go about creating a named range inside the For Next from A6:O5000 named "xwalk" for each individual file? I feel like this will be most effective in combination with a DoCmd export
to see if the recordset was copied to the excel file, use this codes
For i = 0 To 55
catbTSx = arrayIDDA(0, i)
PATHx = arrayIDDA(1, i)
Set xlObj = CreateObject("excel.applic ation")
xlObj.workbooks.Open PATHx
With xlObj
.visible=true 'this will make the excel visible
.worksheets("sf133xwalk"). select
.range("A6").copyfromrecor dset rs
stop 'this will halt the codes to execute so you can see if the records were copied. remove this later after test
.activeworkbook.Save
End With
xlObj.Quit
Next
For i = 0 To 55
catbTSx = arrayIDDA(0, i)
PATHx = arrayIDDA(1, i)
Set xlObj = CreateObject("excel.applic
xlObj.workbooks.Open PATHx
With xlObj
.visible=true 'this will make the excel visible
.worksheets("sf133xwalk").
.range("A6").copyfromrecor
stop 'this will halt the codes to execute so you can see if the records were copied. remove this later after test
.activeworkbook.Save
End With
xlObj.Quit
Next
ASKER
The recordset was copied in the first iteration, but not the second.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Rey!
'here is a generic code to do this
dim rs as dao.recodrset, xlObj as object
'open the recordset
set rs=currentdb.openrecordset
'open the excel file
set xlobj=createobject("eacel.
xlobj.workbooks.open pathx
with xlObj
.worksheets("xwalk").selec
.range("A1").copyfromrecor
.activeworkbook.save
end with
xlobj.quit