Hi there,
the following code ought to be used to get data from an excel file to update data in my master file.
Unfortunately the process just stops, excel closes itself and tries to repair the file and the input file is opened.
I have no idea why it doesnt go through ... any help is greatly appreciated. It does work though when the input file only has a couple of filled rows. I do need it to process thousands of rows though.
I am using Office 365 (latest version) on Windows 10. Speed and space on my laptop shouldnt be the issue at all (64 GB RAM for example).
Sub Input_Values()
Dim iwb As Workbook, iws As Worksheet, ws As Worksheet
Application.ScreenUpdating = False
b = GetFile(ThisWorkbook.Path)
If b = "" Then
MsgBox "Select proper File", vbCritical
Exit Sub
Else
file_path = b
End If
col_st = InputBox("Please write the first paste column number")
col_st = CLng(col_st)
Set iwb = Workbooks.Open(file_path)
Set iws = iwb.Sheets(1)
Set ws = ThisWorkbook.ActiveSheet
ilRow = iws.Cells(iws.Rows.Count, 1).End(xlUp).Row
ilCol = iws.Cells(1, iws.Columns.Count).End(xlToLeft).Column
lRow = ws.Cells(iws.Rows.Count, 1).End(xlUp).Row
lCol = ws.Cells(iws.Rows.Count, 1).End(xlUp).Row
For i = 1 To ilRow
fo = 0
For j = 4 To lRow
If iws.Cells(i, 1).Value = ws.Cells(j, 1).Value Then
iws.Range(iws.Cells(i, 2), iws.Cells(i, ilCol)).Copy ws.Cells(j, col_st)
Application.CutCopyMode = False
fo = 1
End If
Next j
If fo = 0 Then
iws.Cells(i, 1).Copy ws.Cells(lRow + 1, 1)
iws.Range(iws.Cells(i, 2), iws.Cells(i, ilCol)).Copy ws.Cells(lRow + 1, col_st)
lRow = lRow + 1
End If
Next i
iwb.Close
Set iwb = Nothing
End Sub
Function GetFile(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFilePicker)
With fldr
.Title = "Select a File"
.AllowMultiSelect = False
.InitialFileName = strPath
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFile = sItem
Set fldr = Nothing
End Function
Thank you!
|