Need Help Looking for errors

proe0229

New Member
Joined
Nov 1, 2018
Messages
3
Hello Everyone,

I cant seem to find what is cauing my error.. I s a simpl code to on a file in the foldercopy a column ofdata nd pase in thedestination file on the next open column.

Can someone help, maybe Ijus need more sleep, becaue I antsee it.

Sub LoopThroughDirectory()
Dim MyFile As String
Dim ecol
Dim Filepath As String

Filepath = "C:\Users\HansoD02\Downloads\Desktop\Suppliers-master"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If



Workbooks.Open (Filepath & MyFile)
Range("C6:C12").Copy
ActiveWorkbook.Close




ecol = Sheet1.Cells(2, Column.Count).End(xlToRight).Offset(1, 2).Column
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(1, ecol), Cells(1, ecol))

MyFile = Dir
Loop
End Sub
 

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
951
Is this working?

You were missing a backslash from the path

Code:
Sub LoopThroughDirectory()
Dim MyFile As String
Dim ecol
Dim Filepath As String


Filepath = "C:\Users\HansoD02\Downloads\Desktop\Suppliers-master\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If






Workbooks.Open (Filepath & MyFile)
Range("C6:C12").Copy
ActiveWorkbook.Close








ecol = Sheet1.Cells(2, Column.Count).End(xlToRight).Offset(1, 2).Column
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(1, ecol), Cells(1, ecol))


MyFile = Dir
Loop
End Sub
 
Last edited:

proe0229

New Member
Joined
Nov 1, 2018
Messages
3
Thank you mrshl9898


I put in the backslash but I gt ru time error 424
Object required n this line

ecol = Sheet1.Cells(2, Column.Count).End(xlToRight).Offset(1, 2).Column

I als tried changing xlToLett

ecol = Sheet1.Cells(2, Column.Count).End(xlToLeft).Offset(1, 2).Column

Bt same error
 

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
951
last column is:

ecol = Sheets("Sheet1").Cells(2, Sheets("Sheet1").Columns.Count).End(xlToLeft).Column

or with your offset

ecol = Sheets("Sheet1").Cells(2, Sheets("Sheet1").Columns.Count).End(xlToLeft).Offset(1,2).Column
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,199
Office Version
2013
Platform
Windows
the 2nd one works for me...


Code:
Dim ecol as Integer
ecol = Sheet1.Cells(2, Columns.Count).End(xlToLeft).Offset(1, 2).Column
 
Last edited:

proe0229

New Member
Joined
Nov 1, 2018
Messages
3
ecol = Sheet1.Cells(2, Columns.Count).End(xlToLeft).Offset(1, 2).Column

This worked
BUT now I get the runtime error 1004
We cant do that to a merged cell for this line

ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(1, ecol), Cells(1, ecol))

Here is the curent code

Sub LoopThroughDirectory()
Dim MyFile As String
Dim ecol As Integer
Dim Filepath As String

Filepath = "C:\Users\HansoD02\Downloads\Desktop\Suppliers-master"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If



Workbooks.Open (Filepath & MyFile)
Range("C6:C12").Copy
ActiveWorkbook.Close


ecol = Sheet1.Cells(2, Columns.Count).End(xlToLeft).Offset(1, 2).Column


ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(1, ecol), Cells(1, ecol))

MyFile = Dir
Loop
End Sub
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,199
Office Version
2013
Platform
Windows
Yep....merged cells and VBA simply don't mix !!
You'll need to unmerge for this to work
 

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top