I'm trying to create code to insert data from one workbook to another workbook obeying some criteria, however the code I created have error "run-time error '9' subscript out of range". Am beginner in vba. Below is the code:
Sub ImportBaseII()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim BaseII As Workbook, Base As Workbook
Dim BEmpPublic As Worksheet
Dim vFile As Variant
'Set source workbook
Set BaseII = ActiveWorkbook
'Open the target workbook
vFile = Application.GetOpenFilename("Excel-files,*.xlsx", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
'Set targetworkbook
Set Base = ActiveWorkbook
For i = 0 To 197
k = 11 + i * 5
j = i + 2
'For instance, copy data from a "range" in the first workbook to another range in the other workbook
Base.Worksheets("EPúblic").Cell(1, k).Value = BaseII.Worksheets("BEmpPublic").Cell(2, j).Value
t = 12 + i * 5
Base.Worksheets("EPúblic").Cell(7, t).Value = BaseII.Worksheets("BEmpPublic").Cell(6, j).Value
n = 14 + i * 5
Base.Worksheets("EPúblic").Cell(7, n).Value = BaseII.Worksheets("EmpPublic").Cell(7, j).Value
Next i
MsgBox ("Done")
End Sub
Someone help me, thank you in advance.
Bookmarks