i have two workbooks: the first that have column 'code' with range in format 111111 displayed like text and the second workbook that have column 'code' with same range like the first workbook, but this workbook has one more column 'name'. i have task that in the first workbook i compare column 'name' with column in the second workbook and when i find same values in first workbook i insert new column 'code and name' that contains value 'code' column - 'name' column from second workbook. i created for loop but this operation is very slow because the second workbook has 5595 rows. my code is
VBA Code:
Option Explicit
Sub CreateKontoNaziv()
Dim rng As Range
Dim DefaultRange As Range
Dim FormatRuleInput As String
Dim iCol As Long
Dim iCount As Long
Dim i As Long
Dim br As Long
Dim bk As Long
Dim PathNameSifarnikC As String
Dim PathNameSifarnikD As String
Dim PathNameSifarnikE As String
Dim PathNameSifarnikF As String
Dim PathNameSifarnikG As String
Dim CheckDirC As String
Dim CheckDirD As String
Dim CheckDirE As String
Dim CheckDirF As String
Dim CheckDirG As String
Dim owb As Workbook
Dim Sifarnik As Workbook
Dim KontoSifarnik_VInivo As Long
Dim KontoSifarnik_IVnivo As Long
Dim KontoSifarnik_IIInivo As Long
Dim KontoSifarnik_IInivo As Long
Dim j As Long
Dim fso As Object
On Error Resume Next
Set owb = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx")
On Error GoTo 0
Set fso = CreateObject("Scripting.FileSystemObject")
'Provera na kojoj se particiji nalazi sifarnik
PathNameSifarnikC = "C:\sifarnik"
PathNameSifarnikD = "D:\sifarnik"
PathNameSifarnikE = "E:\sifarnik"
PathNameSifarnikF = "F:\sifarnik"
PathNameSifarnikG = "G:\sifarnik"
If owb Is Nothing Then
If fso.FolderExists(PathNameSifarnikC) And fso.GetDrive("C:\").DriveType = 2 Then
Set Sifarnik = Workbooks.Open("C:\sifarnik\Pravilnik o stand klas okviru i k plan.xlsx")
KontoSifarnik_VInivo = Sifarnik.Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Sifarnik.Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Sifarnik.Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Sifarnik.Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
ElseIf fso.FolderExists(PathNameSifarnikD) And fso.GetDrive("D:\").DriveType = 2 Then
Set Sifarnik = Workbooks.Open("D:\sifarnik\Pravilnik o stand klas okviru i k plan.xlsx")
KontoSifarnik_VInivo = Sifarnik.Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Sifarnik.Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Sifarnik.Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Sifarnik.Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
ElseIf fso.FolderExists(PathNameSifarnikE) And fso.GetDrive("E:\").DriveType = 2 Then
Set Sifarnik = Workbooks.Open("E:\sifarnik\Pravilnik o stand klas okviru i k plan.xlsx")
KontoSifarnik_VInivo = Sifarnik.Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Sifarnik.Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Sifarnik.Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Sifarnik.Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
ElseIf fso.FolderExists(PathNameSifarnikF) And fso.GetDrive("F:\").DriveType = 2 Then
Set Sifarnik = Workbooks.Open("F:\sifarnik\Pravilnik o stand klas okviru i k plan.xlsx")
KontoSifarnik_VInivo = Sifarnik.Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Sifarnik.Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Sifarnik.Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Sifarnik.Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
ElseIf fso.FolderExists(PathNameSifarnikG) And fso.GetDrive("G:\").DriveType = 2 Then
Set Sifarnik = Workbooks.Open("G:\sifarnik\Pravilnik o stand klas okviru i k plan.xlsx")
KontoSifarnik_VInivo = Sifarnik.Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Sifarnik.Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Sifarnik.Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Sifarnik.Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
Else
MsgBox "Nemate sifarnik konta, u excel-u. Kreirajte folder sifarnik i prebacite u njega fajl Pravilnik o stand klas okviru i k plan.xlsx", vbOKOnly
Exit Sub
End If
Else
KontoSifarnik_VInivo = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Range("A" & Rows.count).End(xlUp).Row
KontoSifarnik_IVnivo = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Range("D" & Rows.count).End(xlUp).Row
KontoSifarnik_IIInivo = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Range("G" & Rows.count).End(xlUp).Row
KontoSifarnik_IInivo = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Range("J" & Rows.count).End(xlUp).Row
'Postavljanje ovog workbook-a da je aktivan
ThisWorkbook.Activate
End If
'Determine a default range based on user's Selection
If TypeName(Selection) = "Range" Then
Set DefaultRange = Selection
Else
Set DefaultRange = ActiveCell
End If
'Get A Cell Address From The User to Get Number Format From
On Error Resume Next
Set rng = Application.InputBox( _
Title:="Opseg izbora konta", _
Prompt:="Izaberi kolonu, gde su smestena konta, u formatu A1:A5", _
Default:=DefaultRange.Address, _
Type:=8)
'to get the number of columns that you want to insert with an input box
'iCount = InputBox(Prompt:="Unesite broj kolona za unos?", Default:=1)
'to get the column number where you want to insert the new column
iCol = InputBox _
(Prompt:= _
"Iza koje kolone zelite da unesete kolonu(e), broj kolone u formatu: 1,2,3...? ")
'loop to insert new column(s)
'For i = 1 To iCount
Columns(iCol).EntireColumn.Offset(, 1).Insert
'Next i
' RAD SA KONTIMA
Cells(1, iCol + 1).Value = "Konto i Naziv"
On Error GoTo 0
'Test to ensure User Did not cancel
If rng Is Nothing Then Exit Sub
'Opseg selektovane kolone
rng.Select
'Opseg sifarnika Konto
'Petlja koja omogucava pomeranje kroz tekucu tabelu
For br = rng.Row To rng.Row + rng.Rows.count + 1
bk = iCol + 1
'Petlja za sifarnik Po nivoima konta
If (Len(ActiveSheet.Cells(br, rng.Column).Value) = 2) Then
For j = 3 To KontoSifarnik_IInivo
'Dodela vrednosti koloni koja spaja kolonu koja sadrzi konta na II nivou i naziva konta iz sifarnika Konta
If ActiveSheet.Cells(br, rng.Column).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 10).Value Then
Cells(br, bk).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 10).Value & " - " & Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 11).Value
Columns(bk).EntireColumn.AutoFit
Cells(br, bk).HorizontalAlignment = xlLeft
Else
Cells(br, bk).Value = "-"
End If
Next j
ElseIf (Len(ActiveSheet.Cells(br, rng.Column).Value) = 3) Then
For j = 3 To KontoSifarnik_IIInivo
'Dodela vrednosti koloni koja spaja kolonu koja sadrzi konta na III nivou i naziva konta iz sifarnika Konta
If ActiveSheet.Cells(br, rng.Column).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 7).Value Then
Cells(br, bk).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 7).Value & " - " & Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 8).Value
Columns(bk).EntireColumn.AutoFit
Cells(br, bk).HorizontalAlignment = xlLeft
Else
Cells(br, bk).Value = "-"
End If
Next j
ElseIf (Len(ActiveSheet.Cells(br, rng.Column).Value) = 4) Then
For j = 3 To KontoSifarnik_IVnivo
'Dodela vrednosti koloni koja spaja kolonu koja sadrzi konta na IV nivou i naziva konta iz sifarnika Konta
If ActiveSheet.Cells(br, rng.Column).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 4).Value Then
Cells(br, bk).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 4).Value & " - " & Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 5).Value
Columns(bk).EntireColumn.AutoFit
Cells(br, bk).HorizontalAlignment = xlLeft
Else
Cells(br, bk).Value = "-"
End If
Next j
ElseIf (Len(ActiveSheet.Cells(br, rng.Column).Value) = 6) Then
For j = 3 To KontoSifarnik_VInivo
'Dodela vrednosti koloni koja spaja kolonu koja sadrzi konta na VI nivou i naziva konta iz sifarnika Konta
If ActiveSheet.Cells(br, rng.Column).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 1).Value Then
Cells(br, bk).Value = Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 1).Value & " - " & Workbooks("Pravilnik o stand klas okviru i k plan.xlsx").Sheets("Po nivoima konta").Cells(j, 2).Value
Columns(bk).EntireColumn.AutoFit
Cells(br, bk).HorizontalAlignment = xlLeft
Else
Cells(br, bk).Value = "-"
End If
Next j
End If
Next br
End Sub