Sub ExtractData() ' Declare variables Dim MyFolder As String Dim MyFile As String Dim…
- User
Sub ExtractData() ' Declare variables Dim MyFolder As String Dim MyFile As String Dim SourceWB As Workbook Dim SourceWS As Worksheet Dim DestWB As Workbook Dim DestWS As Worksheet Dim NextRow As Long Dim LastRow As Long
' Set the folder path containing the source files MyFolder = ThisWorkbook.Path & "\input"
' Set the destination workbook and worksheet Set DestWB = ThisWorkbook Set DestWS = DestWB.Sheets("SOFUN")
' Find the next empty cell in column B of the destination worksheet LastRow = DestWS.Cells(DestWS.Rows.Count, "B").End(xlUp).Row If LastRow = 1 And IsEmpty(DestWS.Cells(1, "B")) Then NextRow = 1 Else NextRow = LastRow + 1 End If
' Loop through each file in the folder MyFile = Dir(MyFolder & "*.xlsx") Do While MyFile <> "" ' Open the source workbook Set SourceWB = Workbooks.Open(MyFolder & MyFile)
' Set the source worksheet Set SourceWS = SourceWB.Sheets("Settings")
' Extract the data and copy to the destination worksheet DestWS.Cells(NextRow, "B").Value = SourceWS.Range("B2").Value DestWS.Cells(NextRow, "C").Value = SourceWS.Range("C2").Value DestWS.Cells(NextRow, "D").Value = SourceWS.Range("D2").Value DestWS.Cells(NextRow, "E").Value = SourceWS.Range("E2").Value
' Close the source workbook without saving changes SourceWB.Close False
' Move to the next row in the destination worksheet NextRow = NextRow + 1
' Move to the next file in the folder MyFile = Dir Loop
' Display a message box when the extraction is complete MsgBox "Data extraction is complete!", vbInformation, "Complete" End Sub
I have this VBA script in an excel marco workbook. however, the compiled results do not print from the first available cell which is B2, and prints on the next available row which is after B8. Cells B2 to B7 are empty.
- Assistant
It seems that the variable
NextRowis not being initialized correctly. Instead of finding the last used row in column B, the code is assuming that the first empty row is the next available row.To fix this issue, you can modify the code to find the last used row in column B, and add 1 to get the next available row. You can replace the following code:
javaLastRow = DestWS.Cells(DestWS.Rows.Count, "B").End(xlUp).Row If LastRow = 1 And IsEmpty(DestWS.Cells(1, "B")) Then NextRow = 1 Else NextRow = LastRow + 1 End Ifwith:
sqlNextRow = DestWS.Cells(DestWS.Rows.Count, "B").End(xlUp).Row + 1This will set
NextRowto the next available row in column B of theDestWSworksheet.Alternatively, if you want to start printing the results from the first available cell in column B, you can modify the above code to:
sql - User
both alternatives do not work. It still prints from B9
- Assistant
If the previous solutions did not work, it's possible that there is some data or formatting in the cells B2:B8 that is preventing the script from writing to those cells.
To overwrite any existing data in cells B2:B8, you can modify the code to clear the contents of those cells before writing to them. You can replace the following lines of code:
arduinoDestWS.Cells(NextRow, "B").Value = SourceWS.Range("B2").Value DestWS.Cells(NextRow, "C").Value = SourceWS.Range("C2").Value DestWS.Cells(NextRow, "D").Value = SourceWS.Range("D2").Value DestWS.Cells(NextRow, "E").Value = SourceWS.Range("E2").Valuewith:
sqlWith DestWS .Range("B2:B8").ClearContents .Cells(2, "B").Value = SourceWS.Range("B2").Value .Cells(2, "C").Value = SourceWS.Range("C2").Value .Cells(2, "D").Value = SourceWS.Range("D2").Value .Cells(2, "E").Value = SourceWS.Range("E2").Value End WithThis code first clears the contents of cells B2:B8 using the method. Then, it writes the new data to cell B2 and subsequent cells in row 2.
- User
This works but each result is overridden with data from the previous file
- Assistant
If each result is being overwritten with data from the previous file, it's likely that the
NextRowvariable is not being incremented correctly.You can modify the code to ensure that
NextRowis incremented by the correct number of rows after writing to theDestWSworksheet. You can replace the following lines of code:vbnet' Extract the data and copy to the destination worksheet DestWS.Cells(NextRow, "B").Value = SourceWS.Range("B2").Value DestWS.Cells(NextRow, "C").Value = SourceWS.Range("C2").Value DestWS.Cells(NextRow, "D").Value = SourceWS.Range("D2").Value DestWS.Cells(NextRow, "E").Value = SourceWS.Range("E2").Value ' Move to the next row in the destination worksheet NextRow = NextRow + 1 - User
can you reprint the entire script for me
- Assistant
Sure, here's the modified script with the changes I suggested:
vbnet - User
no the result still prints from B9 when B2 to E2 is empty