You are on page 1of 2

Sub Macro1()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp", _
Destination:=Range("$A$1"))
.Name = "TxRef1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """tb_principal1"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub

#2) Run another VBA routine to get the data from Excel to SQL Server.

Sub Rectangle1_Click()
'TRUSTED CONNECTION
On Error GoTo errH

Dim con As New ADODB.Connection


Dim rs As New ADODB.Recordset
Dim strPath As String
Dim intImportRow As Integer
Dim strFirstName, strLastName As String

Dim server, username, password, table, database As String

With Sheets("Sheet1")

server = .TextBox1.Text
table = .TextBox4.Text
database = .TextBox5.Text

If con.State <> 1 Then

con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial


Catalog=" & database & ";Integrated Security=SSPI;"
'con.Open

End If
'this is the TRUSTED connection string
Set rs.ActiveConnection = con

'delete all records first if checkbox checked


If .CheckBox1 Then
con.Execute "delete from tbl_demo"
End If

'set first row with records to import


'you could also just loop thru a range if you want.
intImportRow = 10

Do Until .Cells(intImportRow, 1) = ""


strFirstName = .Cells(intImportRow, 1)
strLastName = .Cells(intImportRow, 2)

'insert row into database


con.Execute "insert into tbl_demo (firstname, lastname) values ('"
& strFirstName & "', '" & strLastName & "')"

intImportRow = intImportRow + 1
Loop

MsgBox "Done importing", vbInformation

con.Close
Set con = Nothing

End With

Exit Sub

errH:
MsgBox Err.Description
End Sub

You might also like