3

I created an Import button which should import data from another opened Excel file, but instead I'm having the following issue. It is 2013 office version.

I have no idea what is wrong with it and would appreciate some advice.

Screenshots:

(Click to enlarge images.) ss1

ss2

ss3

Code of function responsible for importing data, which should take data from open excel file and paste it to another one:

Sub ImportORT()
Dim Rng2 As Range
Dim wbk As Workbook
Set wbk = ThisWorkbook
Dim RowCounter As Long
Dim clipboard As MSForms.DataObject
Dim str1 As String

Application.ScreenUpdating = False

Sheets("Data").Select
Sheets("Data").Cells.NumberFormat = "@"
Range("A1").Select
On Error GoTo Nopaste


Windows("mvrt.xlsx").Activate
ActiveSheet.Cells.Select
'Range("A1:U16").Select
Selection.Copy
Windows("Offsite Macro_2016_v20.xlsm").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

'ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= _
        False, NoHTMLFormatting:=True


Rows("1:1").Delete Shift:=xlUp
'Range("A:A").Delete Shift:=xlLeft  <--- kasowanie pierwszej kolumny (ma sens tylko jak wklejamy ze strony)

'changed:
Set Rng2 = Application.Intersect(ActiveSheet.UsedRange, Range("A:U"))


Rng2.SpecialCells(xlCellTypeVisible).Copy
Sheets("MVRT").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets("MVRT").Activate
Range("A:U").Columns.AutoFit
Range("A1:U1").AutoFilter
Application.Goto Reference:=Range("A1"), Scroll:=True

  '-------------------------------------------
    'NEW:

    'change column format
    Columns("U:U").Select
    Selection.NumberFormat = "General"

    'remove identical rows
    RowCounter = wbk.Sheets("MVRT").Cells(Rows.Count, 2).End(xlUp).Row

     wbk.Sheets("MVRT").Range("$A$1:$m$" & RowCounter).RemoveDuplicates Columns:=Array(2, 3, 9, 10, 11, _
     12, 13), Header:=xlYes


    'set formula
    Range("U2").Select
        ActiveCell.FormulaR1C1 = "=IF(COUNTIF(C[-14],RC[-14])>1,1,0)"
    Range("U2").Select

    If Range("A:A").Rows.End(xlDown).Row > 2 Then
        Selection.AutoFill Destination:=Range("U2:U" & Range("A:A").Rows.End(xlDown).Row)
    End If

    'sort by duplicates
    Range("U1").Value = "duplicated"
    Columns("A:U").Sort Key1:=Range("U1"), Order1:=xlDescending, key2:=Range("C1"), Order2:=xlAscending, key3:=Range("B1"), Order3:=xlAscending, Header:=xlYes

    '-------------------------------------------

    Sheets("Data").Cells.Delete
    Sheets("Control").Activate
    Application.Goto Reference:=Range("A1"), Scroll:=True
    Application.ScreenUpdating = True

    MsgBox "Codes Imported", vbInformation, "Codes Imported"
    Exit Sub

Nopaste:
'------------------------------------
'NEW:
Application.ScreenUpdating = True
'------------------------------------

Sheets("Control").Activate
Application.Goto Reference:=Range("A1"), Scroll:=True
MsgBox "No Data To Paste", vbExclamation, "No Data To Paste"
Exit Sub

End Sub
Community
  • 1
  • 1
Sebastian Wdowiarz
  • 161
  • 1
  • 2
  • 8
  • What happens when you simply try to open the file in question? – cybernetic.nomad Mar 13 '18 at 11:41
  • Please provide the code, that imports the workbook. Is this just happening to this particular file or every other file aswell? – Plagon Mar 13 '18 at 11:50
  • Apart from the error you should [avoid using `.Select`](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) and make an object reference to an `Worksheet/Range`, especially if you are working with multiple `Workbooks`. – Plagon Mar 13 '18 at 13:00

2 Answers2

1

Cause: The most probable cause of encountering this error message is the corruption of complete Excel file or corruption of one or more object in this file.

Well nobody has posted one simple solution for the problem:

Try to open your '.xlsx' file by making it 'read-only'.

1.Click on the ‘Office button’ and select save for new document or save as for previously saved document.

2.Now click on the ‘Tools’ and select ‘General Options’

3.And finally click on the ‘read-only’ check-box to make document read-only Open a new and blank '.xlsx' file and copy everything from the corrupt Excel file to this new file. Save this file and try to open it again.

0

The answer to "what happened and why" is likely in the log file listing repairs (as the dialog stated).

Click the link in the dialog or open the file in a text editor:

c:\users\KKWIET~1\AppData\Local\Temp\error094040_06.xml

Examine the file to try to understand where the problem lies.

If you still can't figure it out, add relevant text from the XML file to your question (with an edit).


More Information:


Other questions if it's not solved:

  • is this happening repeatedly every time you open the file?

  • do you have a backup?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105