I can't copy the files to another Excel spreadsheet

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
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

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
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top