Question : Pasting values in VBA with a single double quote - Excel

I have a small piece of VBA code that attempts to copy from one range to another using range selection and then selection.copy, selecting a target cell and activesheet.paste.   This works most of the time, except if I have a cell with text data in it and it begins with a " and doesn't have an ending ", the paste operation doesn't work correctly.  It appears to take values from the cells beyond the offending cell with the single " and append them all together.

Answer : Pasting values in VBA with a single double quote - Excel

what the cause is I'm not certain but the fix appears to be to NOT close the source until after the paste.

note that this imports all the formats which may not be what you want.  use PasteSpecial if dont want formats.
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
U

I've reworked the ImportCode a bit to use the workbook objects directly

eg set b = Workbooks.open (fileIn)

(ive renamed the routine and the input [arameter as i try not to use names that could clash with a defined name. just my quirk.)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
Function ImportData(fileIn As Variant)
    'Purpose:    Import data from source file selected into this file. remove duplciate information
    '            on dropdown tabs.
    ' Enhancements needed:
    '   1. Verify the format of the source workbook to ensure that it is in the same format.
    
    'Application.ScreenUpdating = False
    
    ' This workbook
    Dim a As Workbook
    ' Source Workbook
    Dim b As Workbook
    
    Dim ca As Range
    Dim cb As Range
    Dim wsx As Worksheet
    Dim ur As Range
    Dim sr As String
    
    
    'file = FunctionGetFileName(file)
    'If Not IsFileOpen(file) Then
    
    Set a = ThisWorkbook
    'Application.Workbooks.Open file
    'Else
    
    Set b = Workbooks.Open(fileIn)   'Workbooks(file)
    With b
    ' Make all worksheets visibile
        For Each wsx In b.Worksheets
        
            wsx.Visible = True
        Next wsx
    
    End With
    
    b.Sheets("detail").Activate
    
    ' Remove header row from range.
    Set cb = Range("Page").Offset(1, 0).Resize(Range("Page").Rows.Count - 1, Range("Page").Columns.Count)
    
    'Range("Page").Select
    cb.Select
    
    Selection.Copy
    
    ' Now handle the Detail tab by appending to existing data
    ' How to handle large amount of data on clipboard?
    ' Need to use another method besides copy to clipboard especially for large amount of data.  Maybe
    ' row by row?
    ' Consider using Detailsource range as input
    
    a.Sheets("Detail").Activate
    
    Dim lc As Integer
    
    ' Try going to next row of Page Range.
    lc = Range("Page").Row + Range("Page").Rows.Count
    Cells(lc, 2).Select
    'Set cb = Range("Page").Offset(1, 0).Resize(Range("Page").Rows.Count - 1, Range("Page").Columns.Count)
    
    'Range("Page").Select
    'cb.Select
    ActiveSheet.Paste
    'Selection.Paste
    'Cells(lc, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    
    Sheets("Detail").Activate
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    b.Close (False)

    'End If

End Function
Random Solutions  
 
programming4us programming4us