Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

ConnectionString

The document contains a VBA subroutine named ProcessData that processes an Excel file and its specified sheet. It identifies the last used column and the highest row with data, then fills in missing values in column A based on the current file name. Finally, it saves and closes the workbook after making the necessary updates.

Uploaded by

muralirpa2024
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ConnectionString

The document contains a VBA subroutine named ProcessData that processes an Excel file and its specified sheet. It identifies the last used column and the highest row with data, then fills in missing values in column A based on the current file name. Finally, it saves and closes the workbook after making the necessary updates.

Uploaded by

muralirpa2024
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Sub ProcessData(strExcelFilePath As String, strExcelSheetName As String)

Dim lastRow As Long, lastCol As Long


Dim i As Long, j As Long
Dim currentstrExcelFilePath As String
Dim hasValue As Boolean
Dim highestRow As Long
Dim strstrExcelFilePath As Workbook
Dim strWorkSheet As Worksheet

' Open the workbook and set the reference to the opened workbook
Set strstrExcelFilePath = Workbooks.Open(strExcelFilePath)
Set strWorkSheet = strstrExcelFilePath.Sheets(strExcelSheetName) ' Set the
worksheet to the provided sheet name

' Find the last used column (using the first row to determine the last used
column)
lastCol = strWorkSheet.Cells(1,
strWorkSheet.Columns.Count).End(xlToLeft).Column

' Find the highest row with data across all columns (loop through each column)
highestRow = 1 ' Initialize highestRow to row 1
For j = 1 To lastCol
' Find the last row in each column
highestRow = WorksheetFunction.Max(highestRow,
strWorkSheet.Cells(strWorkSheet.Rows.Count, j).End(xlUp).Row)
Next j

' Loop through each row up to the highest row


For i = 2 To highestRow
' If there is a file name in column A, store it as the current file name
If strWorkSheet.Cells(i, 1).Value <> "" Then
currentstrExcelFilePath = strWorkSheet.Cells(i, 1).Value
End If

' Check if there are values in the other columns (B to the last column)
hasValue = False
For j = 2 To lastCol
If strWorkSheet.Cells(i, j).Value <> "" Then
hasValue = True
Exit For
End If
Next j

' If there are values in other columns (and column A is empty), fill in the
file name in column A
If hasValue And strWorkSheet.Cells(i, 1).Value = "" Then
strWorkSheet.Cells(i, 1).Value = currentstrExcelFilePath
End If
Next i

' Save and close the workbook


strstrExcelFilePath.Save ' Save changes to the workbook
strstrExcelFilePath.Close ' Close the workbook

' Clean up
Set strWorkSheet = Nothing
Set strstrExcelFilePath = Nothing
End Sub

You might also like