How to perform 'insert rows' instead of 'copy rows' using CopyFromRecordset(excel vba) -
i new excel vba. have requirement have copy table values sql server 2005 excel worksheet. have googled , written code above requirement (listed below).
in excel sheet there fixed set of rows displays legends , dates. these rows should displayed after database/table values printed. using .copyfromrecordset copy records recordset excel sheet, rows displaying legend , dates overwritten database/table values. please let me know how perform insert of rows instead of copy. or there way achieve above.
---code------------
sub getsqlserverdata() dim cn adodb.connection dim server_name string dim database_name string dim user_id string dim password string dim sqlstr string dim userid string dim rs adodb.recordset set rs = new adodb.recordset userid = range("c1").value 'input form excel template. server_name = "" 'enter server name database_name = "" 'enter database name user_id = "" 'sql server user id password = "" sql server password sqlstr = "select end_date,period period_map userid='" + userid + "'" set cn = new adodb.connection cn.open "driver={sql server};server=" & server_name & ";database=" & database_name & _ ";uid=" & user_id & ";pwd=" & password & ";" rs.open sqlstr, cn, adopenstatic worksheets("sheet1").range("a2:d2") ' enter sheet name , range here .clearcontents .copyfromrecordset rs end rs.close set rs = nothing cn.close set cn = nothing end sub
you insert row before copying rs?
worksheets("sheet1").rows(worksheets("sheet1").range("a2:d2").row).insert _ shift:=xldown, _ copyorigin:=xlformatfromleftorabove
Comments
Post a Comment