i hope you had a lovely day. I was trying to automate an excel to autocomplete with data, this is my code:
Sub FillExcelFiles()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim excelApp As Object 'Late-bound Excel Application object
Dim excelWorkbook As Object 'Late-bound Excel Workbook object
'Set the worksheet where your data is located
Set ws = ThisWorkbook.Worksheets("Sheet1")
'Set the range containing the data, assuming column A has the file names and columns B, C, D contain the content
Set rng = ws.Range("A2:R" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
'Create a new instance of Excel Application
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True 'Make Excel application invisible
'Loop through each row in the range
For Each cell In rng.Rows
'Get the file name from column A
Dim fName As String
fName = cell.Cells(1, 1).Value
'Open the draft template file
Set excelWorkbook = excelApp.Workbooks.Open("T:ISCC 2024Robotelul de declaratiiSustainability-Declaration_RED-II_v3.0_211223.xlsx")
'Fill the specified cells with content from columns B, C, D
'unique numer
excelWorkbook.Worksheets("SD Form").Range("J4").Value = cell.Cells(1, 2).Value
excelWorkbook.Worksheets("SD Form").Range("J6").Value = cell.Cells(1, 3).Value
'Furnizor + adresa
excelWorkbook.Worksheets("SD Form").Range("D10").Value = cell.Cells(1, 4).Value
excelWorkbook.Worksheets("SD Form").Range("D13").Value = cell.Cells(1, 5).Value
excelWorkbook.Worksheets("SD Form").Range("D14").Value = cell.Cells(1, 6).Value
excelWorkbook.Worksheets("SD Form").Range("D15").Value = cell.Cells(1, 7).Value
'certificat + contract
excelWorkbook.Worksheets("SD Form").Range("D20").Value = cell.Cells(1, 8).Value
excelWorkbook.Worksheets("SD Form").Range("P20").Value = cell.Cells(1, 9).Value
'adrese + perioada livrare
excelWorkbook.Worksheets("SD Form").Range("J22").Value = cell.Cells(1, 10).Value
excelWorkbook.Worksheets("SD Form").Range("J25").Value = cell.Cells(1, 11).Value
excelWorkbook.Worksheets("SD Form").Range("J28").Value = cell.Cells(1, 12).Value
'Polologia si originea
excelWorkbook.Worksheets("SD Form").Range("J35").Value = cell.Cells(1, 15).Value
excelWorkbook.Worksheets("SD Form").Range("J39").Value = cell.Cells(1, 16).Value
excelWorkbook.Worksheets("SD Form").Range("J34").Value = cell.Cells(1, 13).Value
'Cantitatea
excelWorkbook.Worksheets("SD Form").Range("J41").Value = cell.Cells(1, 17).Value
'Eec
excelWorkbook.Worksheets("SD Form").Range("E64").Value = cell.Cells(1, 18).Value
'Eec
excelWorkbook.Worksheets("SD Form").Range("M75").Value = cell.Cells(1, 14).Value
'Save the filled Excel file as PDF with the specified name
excelWorkbook.SaveAs fileName:="T:ISCC 2024Robotelul de declaratiiExceluri" & fName & ".xlsx", FileFormat:=xlOpenXMLWorkbook
'Close the workbook
excelWorkbook.Close SaveChanges:=False
Next cell
'Quit Excel application
excelApp.Quit
'Cleanup objects
Set excelWorkbook = Nothing
Set excelApp = Nothing
End Sub
I tried for 2-3 days to find a solution but no clue… Any idea?
I have an excel with the data that need to autocomplete in another excel but for some reason I can not save the 10-15 excels files with the data from the rows because:saveAs method of Workbook class failed.
I tried with debugger and stopped me here:
excelWorkbook.SaveAs fileName:=”T:ISCC 2024Robotelul de declaratiiExceluri” & fName & “.xlsx”, FileFormat:=xlOpenXMLWorkbook
fName has values, idk what to do
Stefan Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1