Import Sheet

motabrasil

New Member
Joined
May 11, 2010
Messages
28
Hey fellows,

I have the code described that basically capture a sheet from another workbook and paste in the active workbook.
However, the problem I'm facing is, for each column the code only import the majority data (between TEXT or NUMBER) for each column.
For example, if in the column "C", we have 70% of the contents are NUMBERS and the others 30% are TEXT, the code will import only the 70% of NUMBERS. Not even the column header is imported.
Anyone know how to fix that?

Thank you!!!

Private Sub OptBOMYes_Click()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim SaveDriveDir As String, MyPath As String<o:p></o:p>
Dim FName As Variant<o:p></o:p>
Dim J As Long, X As Long, Y As Long, K As Long<o:p></o:p>
Dim LastColumn As Long, LastRow As Long<o:p></o:p>
Dim wsBOM As Worksheet<o:p></o:p>
<o:p></o:p>
SaveDriveDir = CurDir<o:p></o:p>
MyPath = Application.DefaultFilePath<o:p></o:p>
ChDrive MyPath<o:p></o:p>
ChDir MyPath<o:p></o:p>
FName = Application.GetOpenFilename(filefilter:="Excel Files, *.xlsx*")<o:p></o:p>
<o:p></o:p>
If FName = False Then<o:p></o:p>
'do nothing<o:p></o:p>
Else<o:p></o:p>
GetData FName, "CBOM", "A2:BB1000", Sheets("BOM").Range("B2"), False, False<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
ChDrive SaveDriveDir<o:p></o:p>
ChDir SaveDriveDir<o:p></o:p>
<o:p></o:p>
MsgBox "Done!!!"<o:p></o:p>
Unload Me<o:p></o:p>
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I'm pretty sure the problem isn't to do with this particular code but probably the GetData sub.
 
Upvote 0
Hard to tell without either knowing what GetData is meant to do and/or seeing it's code.:)
 
Upvote 0
Hi Norie

Basically, this code capture a sheet from another folder and paste in the current workbook, tab "BOM".Range("B2"). The problem is, as I explained previously, if I have in the same column, text and numbers, this macro will privilege what we have more, fulfilling with blanks the minority. The other issue I have is, this code only capture file with ".xlsx" extension. I need to turn it able to capture every excel files.

Could you help me?

Thank you once again

Private Sub CaptureSheet
Dim SaveDriveDir As String, MyPath As String<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

Dim FName As Variant<o:p></o:p>
Dim wsBOM As Worksheet<o:p></o:p>
<o:p></o:p>
SaveDriveDir = CurDir<o:p></o:p>
MyPath = Application.DefaultFilePath<o:p></o:p>
ChDrive MyPath<o:p></o:p>
ChDir MyPath<o:p></o:p>
FName = Application.GetOpenFilename(filefilter:="Excel Files, *.xlsx*")<o:p></o:p>
<o:p></o:p>
If FName = False Then<o:p></o:p>
'do nothing<o:p></o:p>
Else<o:p></o:p>
GetData FName, "CBOM", "A2:BB1000", Sheets("BOM").Range("B2"), False, False<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
ChDrive SaveDriveDir<o:p></o:p>
ChDir SaveDriveDir<o:p></o:p>
<o:p></o:p>
MsgBox "Done!!!"<o:p></o:p>
Unload Me<o:p></o:p>
End Sub <!-- / message -->
 
Upvote 0
We might be able to help if we could see the code for the GetData sub/function.:)
 
Upvote 0
See below:


Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
SourceRange As String, TargetRange As Range, Header As Boolean, UseHeaderRow As Boolean)
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long

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
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
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top