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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
Yep....merged cells and VBA simply don't mix !!
You'll need to unmerge for this to work
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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