W.E.B
asked on
Excel/ paste next Column
Hello,
I use below code,
I'm trying to get the zonefrom, and zone to from Range Column A2, AND Column C2
I'm trying to paste the information on next column.
I use below code,
I'm trying to get the zonefrom, and zone to from Range Column A2, AND Column C2
I'm trying to paste the information on next column.
Sub Complete_All_in_One()
Dim sh As Worksheet
Dim cnn As ADODB.Connection
Set sh = Sheets("Crossed")
Dim rs As New ADODB.Recordset
Dim sql As String
Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=xxxxx!;Initial Catalog=data;Data Source=Laptop;"
sql = " Select (Price + Round((Price * 0.04)*20,0)/20) from priceszone where priceplanID = 2834" & _
"AND (ZONEFROMID = " & sh.[A2] & " OR ZONETOID = " & _
sh.[C2] & ") AND ZoneToID NOT IN (1266,1267,1268,1269,1270,1271,1272,1273)"
'Delete old data
sh.Range("E2:Z65000").ClearContents
'Get new data
rs.Open sql, cnn, adOpenDynamic, adLockOptimistic
sh.Range("E2").CopyFromRecordset rs
rs.Close
cnn.Close
MsgBox "Done"
End Sub
sample.xlsx
ASKER
Hello aikimark,
My Code is gwtting the information correctly,
my issue is with getting the zonefrom, and zone to from Range Column A2, AND Column C2 and to paste the information on next column.
Example:
AND (ZONEFROMID = " & sh.[A2] & " OR ZONETOID = sh.[C2] & ")
A2 = 319 C2= 319
paste into Column E,
THEN get next raw
AND (ZONEFROMID = " & sh.[A3] & " OR ZONETOID = sh.[32] & ")
A3 = 320 C3= 320
paste into Column F,
THEN get next raw
AND (ZONEFROMID = " & sh.[A4] & " OR ZONETOID = sh.[C4] & ")
A4 = 321 C4= 321
paste into Column G,
And so on.
Thanks,
My Code is gwtting the information correctly,
my issue is with getting the zonefrom, and zone to from Range Column A2, AND Column C2 and to paste the information on next column.
Example:
AND (ZONEFROMID = " & sh.[A2] & " OR ZONETOID = sh.[C2] & ")
A2 = 319 C2= 319
paste into Column E,
THEN get next raw
AND (ZONEFROMID = " & sh.[A3] & " OR ZONETOID = sh.[32] & ")
A3 = 320 C3= 320
paste into Column F,
THEN get next raw
AND (ZONEFROMID = " & sh.[A4] & " OR ZONETOID = sh.[C4] & ")
A4 = 321 C4= 321
paste into Column G,
And so on.
Thanks,
how many rows does your current query return?
ASKER
same number of rows as in Column A
so, in this example,
it returns 140 rows.
so, in this example,
it returns 140 rows.
I think you will need to repeat the process four times, using the different ZONEFROMID and ZONETOID values in the A and C columns and specifying different (columnar) destinations with each invocation. You can put this in a looping structure.
Example:
Example:
Sub Complete_All_in_One()
Dim lngLoop As Long
Dim sh As Worksheet
Dim cnn As ADODB.Connection
Set sh = Sheets("Crossed")
Dim rs As New ADODB.Recordset
Dim sql As String
Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=xxxxx!;Initial Catalog=data;Data Source=Laptop;"
'Delete old data
sh.Range("E2:Z65000").ClearContents
For lngLoop = 1 To 4
sql = " Select (Price + Round((Price * 0.04)*20,0)/20) from priceszone where priceplanID = 2834" & _
"AND (ZONEFROMID = " & sh.cells(lngLoop, 1) & " OR ZONETOID = " & _
sh.cells(lngLoop, 3) & ") AND ZoneToID NOT IN (1266,1267,1268,1269,1270,1271,1272,1273)"
'Get new data
rs.Open sql, cnn, adOpenDynamic, adLockOptimistic
sh.cells(1, lngLoop + 4).CopyFromRecordset rs
Next
rs.Close
cnn.Close
MsgBox "Done"
End Sub
ASKER
Hello,
Appreciate your help,
I get error
Runtime error 3705
Operation is not allowed when the object is open.
Appreciate your help,
I get error
Runtime error 3705
Operation is not allowed when the object is open.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Perfect,
Thank you
Thank you
Open in new window
You might get better performance with this:Open in new window