Macro not pulling the correct data - Help!

Status
Not open for further replies.

braidp

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

I hope you can help with the below macro. It runs well as long as Sheet1 is visible (Ideally I'd like it to work even when sheet1 is hidden) however I've also now noticed that it's actually pulling the data from the last tabbed saved in the workbook rather than referencing Sheet1.

So if Sheet 1 is the tab saved before closing the workbook the macro works, If it is saved on a different tab then it pulls the wrong data.

Any help on this would be amazing.

Currently I use this to extract a line of data out of multiple workbooks to collate the information on one workbook


Code:
[COLOR=#333333]Sub LoopThroughDirectory()[/COLOR]
[COLOR=#333333]Dim Filepath As String[/COLOR]
[COLOR=#333333]Dim erow[/COLOR]
[COLOR=#333333]Filepath = Application.ActiveWorkbook.Path[/COLOR]
[COLOR=#333333]MyFile = Dir(Filepath + "\*.*")[/COLOR]


[COLOR=#333333]erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row[/COLOR]
[COLOR=#333333]Sheet1.Rows("2:" & Format(erow)).EntireRow.ClearContents[/COLOR]


[COLOR=#333333]Do While Len(MyFile) > 0[/COLOR]
[COLOR=#333333]If MyFile = "ZMasterFile.xlsm" Then[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]
[COLOR=#333333]End If[/COLOR]


[COLOR=#333333]Application.DisplayAlerts = False[/COLOR]


[COLOR=#333333]Workbooks.Open (Filepath & "" & MyFile)[/COLOR]
[COLOR=#333333]Range("A2:M900").Copy[/COLOR]


[COLOR=#333333]ActiveWorkbook.Close[/COLOR]




[COLOR=#333333]erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row[/COLOR]
[COLOR=#333333]ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 1), Cells(erow, 13))[/COLOR]


[COLOR=#333333]MyFile = Dir[/COLOR]


[COLOR=#333333]Application.DisplayAlerts = True[/COLOR]


[COLOR=#333333]Loop[/COLOR]


[COLOR=#333333]End Sub[/COLOR]
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Status
Not open for further replies.

Forum statistics

Threads
1,214,780
Messages
6,121,527
Members
449,037
Latest member
tmmotairi

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