The program does not write the value on the sheet

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I created a code to open the most recent file in a specific folder and copy the value if a specific cell to another in a different excel file, the program executes everything, including I receive the value "msgbox" but the values are not written on the second sheet " EEC QEC.xlsx ", could someone help me?


VBA Code:
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
    
    Call WriteValues

End Sub


Public Sub AutoRunMacro()
        
    Call recentFilesSpecificFolder

End Sub


Private Sub KillEmpty()

    Dim k As Long
    
    Workbooks.Open Filename:=Environ("userprofile") & "\Documents\Projeto_Luis\EEC QEC.xlsx"
    
    ws2 = ActiveWorkbook.Sheets.Count
    ActiveWorkbook.Sheets("2019").Select
    
    Range("H141").Select
    k = ActiveSheet.Range("H141", ActiveSheet.Range("H141").End(xlDown)).Rows.Count
    
    
    If ActiveCell.Value = "" Then
        Call FillCells
    
    ElseIf ActiveCell.Value <> "" Then
        Do Until ActiveCell.Value = ""
            ActiveCell.Offset(1, 0).Select
        Loop
        Call FillCells
        
    End If
End Sub


Private Sub FillCells()

    Set sh = ActiveSheet
    For counter = 1 To ws
        For counter2 = 1 To ws2
            If sh.Name <> "2019" And sh.Name <> "2020" And sh.Name <> "2021" And sh.Name <> "Total" And sh.Name <> "Iluminação Exterior" Then
                Workbooks(myDirectory & myMostRecentFile).Worksheets(counter).Range("W110").Copy _
                Workbooks("EEC QEC.xlsm").Worksheets("counter2").Range("H141")
                Workbooks(myDirectory & myMostRecentFile).Worksheets(counter).Range("AI110").Copy _
                Workbooks("EEC QEC.xlsm").Worksheets("counter2").Range("I141")
            End If
        Next counter2
    Next counter

MsgBox "ola"
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi KhallP,

I wonder how you pass the name of the workbooks or the sheetnames. I´d expected to see either a variable for the module holding the name or the name or an object of the workbook to be passed as parameter to the procedures. Unless that happens the information is only available for the procedure itself.

Once the workbook with the most recent date has been opened it may referred to only by the name and extension. And the code would need any information about what the variable should look like.

The first procedures may look like this:
VBA Code:
Dim myRecentFile As String
'

Public Sub recentFilesSpecificFolder()

    Dim myFile As String, fileExtension As String
    Dim recentDate As Date, myDirectory As String

    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
    MsgBox "Path: " & myDirectory & vbCrLf & "File: " & myRecentFile
    Workbooks.Open Filename:=myDirectory & myRecentFile
   
''!!!!    Call WriteValues        '!!!>>>> procedure is missing

End Sub


Public Sub AutoRunMacro()
' Shouldn´t it be an Auto_Open although since Excel97 it´s preferably Workbook_Open in ThisWorkbook
       
    Call recentFilesSpecificFolder

End Sub


Private Sub KillEmpty()

    Dim k As Long
    Dim wbkMaster As Workbook
   
    Set wbkMaster = Workbooks.Open(Filename:=Environ("userprofile") & "\Documents\Projeto_Luis\EEC QEC.xlsx")
   
    Range("H141").Select
    k = 141
   
    With wbkMaster.Sheets("2019")
      If .Cells(k, "H").Value = "" Then
          Call FillCells
      Else
          Do Until .Cells(k, "H").Value = ""
             k = k + 1
          Loop
          Call FillCells
      End If
    End With

End Sub
Sorry but I´m having trouble understanding FillCells as you call it from one sheet but loop through all worksheets in both workbooks? Do the workbooks have the same number of sheets as well as the same sheetnames?

Ciao,
Holger
 
Upvote 0
Hi KhallP,

I wonder how you pass the name of the workbooks or the sheetnames. I´d expected to see either a variable for the module holding the name or the name or an object of the workbook to be passed as parameter to the procedures. Unless that happens the information is only available for the procedure itself.

Once the workbook with the most recent date has been opened it may referred to only by the name and extension. And the code would need any information about what the variable should look like.

The first procedures may look like this:
VBA Code:
Dim myRecentFile As String
'

Public Sub recentFilesSpecificFolder()

    Dim myFile As String, fileExtension As String
    Dim recentDate As Date, myDirectory As String

    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
    MsgBox "Path: " & myDirectory & vbCrLf & "File: " & myRecentFile
    Workbooks.Open Filename:=myDirectory & myRecentFile
 
''!!!!    Call WriteValues        '!!!>>>> procedure is missing

End Sub


Public Sub AutoRunMacro()
' Shouldn´t it be an Auto_Open although since Excel97 it´s preferably Workbook_Open in ThisWorkbook
     
    Call recentFilesSpecificFolder

End Sub


Private Sub KillEmpty()

    Dim k As Long
    Dim wbkMaster As Workbook
 
    Set wbkMaster = Workbooks.Open(Filename:=Environ("userprofile") & "\Documents\Projeto_Luis\EEC QEC.xlsx")
 
    Range("H141").Select
    k = 141
 
    With wbkMaster.Sheets("2019")
      If .Cells(k, "H").Value = "" Then
          Call FillCells
      Else
          Do Until .Cells(k, "H").Value = ""
             k = k + 1
          Loop
          Call FillCells
      End If
    End With

End Sub
Sorry but I´m having trouble understanding FillCells as you call it from one sheet but loop through all worksheets in both workbooks? Do the workbooks have the same number of sheets as well as the same sheetnames?

Ciao,
Holger



Hi, thanks for offering help, yes, the goal is to go through all the worksheets on both work legs and no, the folders do not have the same number of worksheets or the same name
 
Upvote 0
Hi KhallP,

hard to understand for me because in the original code only fixed addreses are given and will be overwritten. you mention ActiveSheet, from the code before I assume that to be EEC QEC.xlsx with sheet 2019. Should the check be made on the latest workbook? And really transfer data according to the index of the workbooks? So data on all worksheets is identical?

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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