Hello,
Wondering if there is anyway to include the paste formats and values into the below macro? Every time I try I get a Compile error, expected end of statement.
Thanks
Include This
Into this
Hello,
Wondering if there is anyway to include the paste formats and values into the below macro? Every time I try I get a Compile error, expected end of statement.
Thanks
Include This
Into this
Re: Trouble including paste special in macro
What is GetData? Where's the code for it?
Re: Trouble including paste special in macro
The below code is placed in a function module.
Option Explicit
Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
SourceRange As String, TargetRange As Range, Header As Boolean, UseHeaderRow As Boolean)
' 30-Dec-2007, working in Excel 2000-2007
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long
' Create the connection string.
If Header = False Then
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
If SourceSheet = "" Then
' workbook level name
szSQL = "SELECT * FROM " & SourceRange$ & ";"
Else
' worksheet level name or range
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If Header = False Then
TargetRange.Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1
TargetRange.Cells(1, 1 + lCount).Value = _
rsData.Fields(lCount).Name
Next lCount
TargetRange.Cells(2, 1).CopyFromRecordset rsData
Else
TargetRange.Cells(1, 1).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
' Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, _
vbExclamation, "Error"
On Error GoTo 0
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function Array_Sort(ArrayList As Variant) As Variant
Dim aCnt As Integer, bCnt As Integer
Dim tempStr As String
For aCnt = LBound(ArrayList) To UBound(ArrayList) - 1
For bCnt = aCnt + 1 To UBound(ArrayList)
If ArrayList(aCnt) > ArrayList(bCnt) Then
tempStr = ArrayList(bCnt)
ArrayList(bCnt) = ArrayList(aCnt)
ArrayList(aCnt) = tempStr
End If
Next bCnt
Next aCnt
Array_Sort = ArrayList
End Function
Display More
Re: Trouble including paste special in macro
You need to explain. The additional code you posted uses ADO to get records using CopyFromRecordSet. There are no 'formats' to paste.
Re: Trouble including paste special in macro
Cross posted here
Re: Trouble including paste special in macro
Correct link: http://www.excelforum.com/exce…ste-special-in-macro.html
I seem to remember another thread, about a week ago, cross posted as well. Might suggest you learn the etiquette for when you post on boards.
Re: Trouble including paste special in macro
yes, you are correct. I guess I did cross post. I didn't know the two boards were ran and monitored by the same folks. Sorry
Re: Trouble including paste special in macro
Quote from Grimes0332;768820You need to explain. The additional code you posted uses ADO to get records using CopyFromRecordSet. There are no 'formats' to paste.
Thanks for the comment, as you can probably tell I really don't know much about vba (just learning some stuff).
In my original dataset it has borders around the data and some of the cells has red text. I was just trying to find a way that when it copies the data to paste it with the original cell formatting. Thanks
Re: Trouble including paste special in macro
As far as I'm aware, they're totally separate boards. But if you post the same issue on multiple boards and have multiple people replying, chances are some of those replies will be similar, if not identical. If that happens then someone has wasted their time - and that is the problem. Read this - http://www.excelguru.ca/content.php?184
Don’t have an account yet? Register yourself now and be a part of our community!