Help with VBA Macro

braidp

New Member
Joined
Dec 27, 2018
Messages
39
Hi Guys,

I was wondering if anyone can help with the below. I have this Macro working and it's returning the data needed from each of the files, however it only works if I keep the button to run the macro in the 'raw data' worksheet. Ideally I would like this button to sit on the sheet named 'overview' that would then run the macro and paste the data into the raw data sheet? When I've tried it it references this line as being the error on the macro.

Many thanks in advance

ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))

Sub LoopThroughDirectory()
Dim Filepath As String
Dim erow
Filepath = Application.ActiveWorkbook.Path
MyFile = Dir(Filepath + "\*.*")
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Rows("2:" & Format(erow)).EntireRow.ClearContents

Do While Len(MyFile) > 0
If MyFile = "ZMasterFileDTL.xlsm" Then
Exit Sub
End If

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open (Filepath & "\" & MyFile)
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Range("A2:U900").Copy
Sheets("Sheet1").Visible = False
ActiveWorkbook.Close


erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))

MyFile = Dir

Application.DisplayAlerts = True


Loop


End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
It's an error because you're not pasting to the active sheet, you need to use

VBA Code:
Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1)).Paste
Application.CutCopyMode = False
The second line is to clear the copied data from the clipboard after pasting.
 
Upvote 0
Hi Jason,

I really appreciate your help with this, I've pasted your suggestion into the code as per below but that line is still returning an error, any ideas on what I can try next?

Many thanks!!

Sub LoopThroughDirectory()
Dim Filepath As String
Dim erow
Filepath = Application.ActiveWorkbook.Path
MyFile = Dir(Filepath + "\*.*")
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Rows("2:" & Format(erow)).EntireRow.ClearContents

Do While Len(MyFile) > 0
If MyFile = "ZMasterFileDTL.xlsm" Then
Exit Sub
End If

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open (Filepath & "\" & MyFile)
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Range("A2:U900").Copy
Sheets("Sheet1").Visible = False
ActiveWorkbook.Close


erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1)).Paste
Application.CutCopyMode = False

MyFile = Dir

Application.DisplayAlerts = True


Loop


End Sub
 
Upvote 0
If i try and run it from the page I want to run it from - I get Run Time Error 1004. Application-defined or object-defined error

If I now run it from the raw data page I now also get the error Run time error 438. Object doesn't support this property or method

Thanks Jason
 
Upvote 0
Instead of Paste write pastespecial like this:

Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1)).Pastespecial
 
Upvote 0
Thanks snjpverma, I gave that a try but it's returning the - Run Time Error 1004. Application-defined or object-defined error.

Any other suggestions?

Many thanks :)
 
Upvote 0
If i try and run it from the page I want to run it from - I get Run Time Error 1004. Application-defined or object-defined error

If I now run it from the raw data page I now also get the error Run time error 438. Object doesn't support this property or method
Are these errors both occurring on the same line?
 
Upvote 0
yes Jason,

Depending on which sheet I place the button to run the macro.

If I place it where I want to place it I get the 1004 error.
Trying it where it previously worked in "raw data" I get the 438 error, I amended it to Pastespecial as recoomended by snj which fixed it on the "raw data" sheet but still errors 1004 on the sheet I would prefer the button.
 
Upvote 0
Think I've just spotted the problem.

VBA Code:
With Worksheets("Raw Data")
    .Range(.Cells(erow, 31), .Cells(erow, 1)).Paste
End With

Range was referring to 'Raw Data' but the 2 instances of Cells were both still linked to the activesheet.
 
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,680
Members
449,328
Latest member
easperhe29

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