Having a few issues....

cweggleto81

New Member
Joined
Jan 11, 2018
Messages
21
Hi all,
It's been a while since I did any VBA stuff. I have the code below and it does work, however I want the data to start on Row 4 on the spreadsheet. Currently it only goes for the first available blank line which is no good for the purpose. Any ideas? Any help is appreciated

Sub Macro1()

Dim objFSO As Object, objFolder As Object, objFile As Object
Dim strFolderName As String
Dim wb As Workbook
Dim lngMyRow As Long
Dim FldrPicker As FileDialog

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
.Title = "Select Target Charges Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
strFolderName = .SelectedItems(1) & ""
End With

NextCode:
strFolderName = strFolderName
If strFolderName = "" Then GoTo ResetSettings


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolderName)

For Each objFile In objFolder.Files
If InStr(objFSO.GetExtensionName(objFile.Name), "xls") > 0 Then 'Only interested in Excel files
Set wb = Workbooks.Open(objFolder & "" & objFile.Name)
'Puts the data from the named ranges into the next available row in columns A, B abd C in 'Sheet1'. Change to suit if necessary.
On Error Resume Next 'In case there's no data in 'Sheet1' manually set the variable
lngMyRow = ThisWorkbook.Sheets("Sheet1").Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
If lngMyRow = 0 Then
lngMyRow = 4 'Default initial output row number. Change to suit if necessary
End If
On Error GoTo 0
With ThisWorkbook.Sheets("Sheet1")
.Range("A" & lngMyRow) = wb.Names("ACCNTNAME").RefersToRange
.Range("B" & lngMyRow) = wb.Names("ACCNTNUM").RefersToRange
.Range("C" & lngMyRow) = wb.Names("TOTALCOST").RefersToRange
End With
wb.Close False
End If
Next objFile

Application.ScreenUpdating = True

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
For Each objFile In objFolder.Files
If InStr(objFSO.GetExtensionName(objFile.Name), "xls") > 0 Then 'Only interested in Excel files
Set wb = Workbooks.Open(objFolder & "" & objFile.Name)
'Puts the data from the named ranges into the next available row in columns A, B abd C in 'Sheet1'. Change to suit if necessary.
On Error Resume Next 'In case there's no data in 'Sheet1' manually set the variable
'lngMyRow = ThisWorkbook.Sheets("Sheet1").Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
'If lngMyRow = 0 Then
lngMyRow = 4 'Default initial output row number. Change to suit if necessary
'End If
On Error GoTo 0
With ThisWorkbook.Sheets("Sheet1")
.Range("A" & lngMyRow) = wb.Names("ACCNTNAME").RefersToRange
.Range("B" & lngMyRow) = wb.Names("ACCNTNUM").RefersToRange
.Range("C" & lngMyRow) = wb.Names("TOTALCOST").RefersToRange
End With
wb.Close False
lngMyRow = lngMyRow + 1
End If
Next objFile
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

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