Macro Run only a three lines of code and then stops working without any error messages

John01

New Member
Joined
Oct 12, 2017
Messages
20
I have the code that is working perfectly, but now it doesn't open the code line when comes to open the file from directory. I appreciate some help, thanks in advance. I have the following code:

Sub CENTRAL()

Dim cell3 As Range
Dim FileName As String
Dim CellName As String
Dim Fpath As String
Dim wb As Workbook
Dim SumResult As Double



Workbooks.Open FileName:="C:\Users\SKY\Desktop\Dom\CENTRAL.xlsx"




For Each cell3 In ActiveSheet.Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).row)

finalcolumn = 1 + ActiveSheet.Cells(cell3.row, 14).End(xlToLeft).Column <-- it is executing the code till here

MsgBox finalcolumn <--- to check if it is working

Fpath = ThisWorkbook.Path & "\" <----- from this part macro stops to work
CellName = cell3.Value

FileName = Dir(Fpath & "\*" & CellName & ".xlsx")
Select Case True
Case Trim(cell3.Value) = "Total"
Case Trim(cell3.Value) <> "" And Left (FileName,14) Like "*" & CellName & "*"

Workbooks.Open FileName:=Fpath & FileName
Set wb = ActiveWorkbook
SumResult = WorksheetFunction.Sum(wb.Worksheets(1).Range("B1:B6"))
Totalsum = Totalsum + SumResult

Windows("CENTRAL.xlsx").Activate
Worksheets(1).Range(Cells(cell3.row, finalcolumn), Cells(cell3.row, finalcolumn)).Value = Totalsum
Application.CutCopyMode = False
Application.DisplayAlerts = False

wb.Close



End Select

Next cell3



End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I expect it is a problem with the data rather than the code, try changing the end of your code to

VBA Code:
Case Else
    ctr = ctr + 1


End Select

Next cell3
MsgBox ctr

End Sub

This will tell you how many cells don't match any of the Select Case criteria, it it is all of them then you will know that the problem is in the data.
 
Upvote 0
I expect it is a problem with the data rather than the code, try changing the end of your code to

VBA Code:
Case Else
    ctr = ctr + 1


End Select

Next cell3
MsgBox ctr

End Sub

This will tell you how many cells don't match any of the Select Case criteria, it it is all of them then you will know that the problem is in the data.

Yes, it gives me 56 its just exactly the number of rows that should be compared with filename in directory. But, though I have the similar code to this that always worked and now also this code doesn't work at all. Is it because I tested more codes, and maybe in background some of the code stacked somewhere ? It is weird that on the same data before it worked perfectly and now macro doesn't start at all. This is the code :

VBA Code:
Sub OPEN_FILE_FROM_CELL()

Dim cell3 As Range
Dim FileName As String
Dim CellName As String
Dim Fpath As String
Dim d As String
Dim wb As Workbook

Workbooks.Open FileName:="C:\Users\sky\Desktop\ADM\list total.xlsx"

For Each cell3 In ActiveSheet.Range("C3:C" & Cells(Rows.Count, 3).End(xlUp).row)

eColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
If eColumn >= 1 Then eColumn = eColumn + 1

Fpath = ThisWorkbook.Path & "\"
CellName = cell3.Value

FileName = Dir(Fpath & "\*" & CellName & ".xlsx")
    Select Case True
    Case Trim(cell3.Value) <> "" And Right(FileName, 12) Like "*" & CellName & "*"
    

                  Workbooks.Open FileName:=Fpath & FileName
                  Set wb = ActiveWorkbook
                  Range("B1:B6").Select
                  Selection.Copy
                  
                  Windows("list total.xlsx").Activate
                  ActiveSheet.Range(Cells(cell3.row, eColumn), Cells(cell3.row, eColumn)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
               
                  Application.CutCopyMode = False
                  Application.DisplayAlerts = False
      
                  wb.Close
                  
   
                  
                  End Select

Next cell3


End Sub
 
Upvote 0
The only difference that I can see in the code is in this line,
VBA Code:
Case Trim(cell3.Value) <> "" And Left (FileName,14) Like "*" & CellName & "*"
In this procedure you have used Left, in the one that works you have used Right?
 
Upvote 0
The only difference that I can see in the code is in this line,
VBA Code:
Case Trim(cell3.Value) <> "" And Left (FileName,14) Like "*" & CellName & "*"
In this procedure you have used Left, in the one that works you have used Right?
Yes, that's right, that is only difference that I have, Left and Right in this two macros.
 
Upvote 0
Try adding another message box at this point
VBA Code:
FileName = Dir(Fpath & "\*" & CellName & ".xlsx")
MsgBox FileName
Select Case True
How does what you see differ from what you expect?
 
Upvote 0
Try adding another message box at this point
VBA Code:
FileName = Dir(Fpath & "\*" & CellName & ".xlsx")
MsgBox FileName
Select Case True
How does what you see differ from what you expect?
I've added the message box you suggested and it gives me empty Msgbox without any file selection, but I found the problem. When I am inserting path in Fpath=
"C:\Users\sky\Desktop\ADM\" the code starts to execute like before and when I put in Fpath= ThisWorkbook.Path & "\" then happens nothing, code stops here. So the problem is that the macro doesn't want to execute the path ThisWorkbook.Path & "\" and before executed this command just fine.
 
Upvote 0
Sounds like the workbook is saved in the wrong folder.
If you add the line
VBA Code:
MsgBox ThisWorkbook.Path
into your code, does it show the expected path of "C:\Users\sky\Desktop\ADM\" ?
 
Upvote 0
Sounds like the workbook is saved in the wrong folder.
If you add the line
VBA Code:
MsgBox ThisWorkbook.Path
into your code, does it show the expected path of "C:\Users\sky\Desktop\ADM\" ?
Yes, it shows "C:\Users\sky\Desktop\ADM\".
 
Upvote 0
Content of a cell and the full name of the file that it should match to?

This is going to be something similar to the problem in your last thread, I have my suspicions that it could even be the same problem in reverse.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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