VBA to write data from Excel

Here’s an example of VBA code that writes data to an Excel worksheet:

Sub WriteDataToExcel()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim filePath As String
    Dim rowData As Variant
    
    ' Specify the path and filename of the Excel file
    filePath = "C:\Path\to\your\File.xlsx"
    
    ' Specify the data to be written to the worksheet
    rowData = Array("John", "Doe", 25) ' Replace with your data
    
    ' Open the Excel file
    Set wb = Workbooks.Open(filePath)
    
    ' Specify the worksheet where the data should be written
    Set ws = wb.Worksheets("Sheet1") ' Replace "Sheet1" with your sheet name
    
    ' Find the last row in the worksheet
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
    
    ' Write data to the next available row
    ws.Cells(lastRow, 1).Resize(1, UBound(rowData) + 1).Value = rowData
    
    ' Save and close the Excel file
    wb.Save
    wb.Close
    
    ' Clean up the objects
    Set ws = Nothing
    Set wb = Nothing
End Sub

In this code, you need to modify the filePath variable to the actual path and filename of your Excel file. Also, replace "Sheet1" with the name of the worksheet where you want to write the data.

The rowData variable contains the data you want to write to the Excel worksheet. You can customize it according to your requirements. The example provided assumes three data values: “John,” “Doe,” and 25.

When you run this code, it will open the specified Excel file, determine the last available row in the worksheet, write the data to the next available row, save the changes, close the Excel file, and clean up the objects.

Note: Make sure that the specified Excel file exists in the specified path and is accessible by the VBA code.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech