I am trying to create a macro that passes values between 2 different excel files, I tried to create a counter whenever the values are inserted in the spreadsheets, the program does the same for all the spreadsheets available in these files, in case the cell where the value will be inserted the program is not empty, it loops until it finds an empty cell to write this value, but an error occurs when I select the spreadsheet depending on the counter, can someone help me?
'Variables
'Variables
Code:
Option Explicit
Public myMostRecentFile As String, myDirectory As String, Teste As String
Public ws As Integer, ws2 As Integer, counter As Integer
Public sh As Worksheet
Public k As Long
VBA Code:
[COLOR=rgb(97, 189, 109)]'Checks the last file inserted in a specific folder[/COLOR]
Public Sub recentFilesSpecificFolder()
Dim myFile As String, myRecentFile As String, fileExtension As String
Dim recentDate As Date
myDirectory = Environ("userprofile") & "\Documents\Projeto_Luis\Andre\EEC\QE"
fileExtension = "*.xls"
If Right(myDirectory, 1) <> "\" Then myDirectory = myDirectory & "\"
myFile = Dir(myDirectory & fileExtension)
If myFile <> "" Then
myRecentFile = myFile
recentDate = FileDateTime(myDirectory & myFile)
Do While myFile <> ""
If FileDateTime(myDirectory & myFile) > recentDate Then
myRecentFile = myFile
recentDate = FileDateTime(myDirectory & myFile)
End If
myFile = Dir
Loop
End If
myMostRecentFile = myRecentFile
MsgBox "Path: " & myDirectory & vbCrLf & "File: " & myMostRecentFile
Workbooks.Open Filename:=myDirectory & myMostRecentFile
Teste = myMostRecentFile
Call WriteValues
End Sub
[COLOR=rgb(97, 189, 109)]'Fill cells in 2 Sheets[/COLOR]
Private Sub FillCells()
Workbooks.Open Filename:=Environ("userprofile") & "\Documents\Projeto_Luis\EEC QEC.xlsx"
ws = Workbooks((Teste).Sheets.Count
ws2 = Workbooks("EEC QEC.xlsx").Sheets.Count
k = Range("H5", Range("H5").End(xlDown)).Rows.Count
For counter = 1 To ws
For counter2 = 1 To ws2
[COLOR=rgb(0, 0, 0)] Error[/COLOR]
-----------------------------------------------------
[COLOR=rgb(209, 72, 65)] [/COLOR][COLOR=rgb(184, 49, 47)] Workbooks(Teste).Worksheets(counter).Select[/COLOR]
-----------------------------------------------------
Workbooks("EEC QEC.xlsx").Worksheets(counter2).Select
Set sh = Workbooks("EEC QEC.xlsx").Worksheets(counter2).Select
If sh.Name = "QEC 1.2 - montagem" Or sh.Name = "QEC 2.2 -SALA LIMPA" Or sh.Name = "QEC 2.4 Logística" Or sh.Name = "QEC 4.1 - MONTAGEM MANUAL(past)" Or sh.Name = "QEC 4.2 - Desmoldagem" Or sh.Name = "QEC 4,3 - RTM" Or sh.Name = "QEC 4,4 - HOT DRAPE" Then
If Range("H5").Value = "" Then
Workbooks(Teste).Worksheets(counter).Range("W110").Copy _
Workbooks("EEC QEC.xlsx").Worksheets("counter2").Range(CStr(k + 1)).Paste
Workbooks(Teste).Worksheets(counter).Range("AI110").Copy
Workbooks("EEC QEC.xlsx").Worksheets("counter2").Range(CStr(k + 1)).Paste
Else
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
Workbooks(Teste).Worksheets(counter).Range("W110").Copy _
Workbooks("EEC QEC.xlsx").Worksheets("counter2").Range(CStr(k + 1)).Paste
Workbooks(Teste).Worksheets(counter).Range("AI110").Copy _
Workbooks("EEC QEC.xlsx").Worksheets("counter2").Range(CStr(k + 1)).Paste
End If
End If
Next counter2
Next counter
[COLOR=rgb(97, 189, 109)]
'The textbox appears[/COLOR]
MsgBox "ola"
End Sub