snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Dim wb As Workbook
Dim ws As Worksheet
Dim cellValue As Variant
Dim strPath As String
Dim strWorkbookName As String
Dim strSheetName As String
Dim strCellAddress As String
strPath = "Z:\Finance-Dept\6. End of Month_EoM\2. End of Month_EoM for 2023\7. July 2023\"
strWorkbookName = "Fixed Assets Depreciation as of Jul 23.xlsx"
strSheetName = "FA List Update"
strCellAddress = "" ' Initialize the cell address
Set wb = Workbooks.Open(strPath & strWorkbookName)
Set ws = wb.Sheets(strSheetName)
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Dim i As Long
For i = 4 To lastRow
If Right(ws.Cells(i, "D"), 5) = "Total" Then
strCellAddress = "L" & i
Exit For
End If
Next i
If strCellAddress <> "" Then
cellValue = ws.Range(strCellAddress).Value
ActiveCell.Value = cellValue
End If
wb.Close False
End Sub
thanks
You do not have the required permissions to view the files attached to this post.
'Other code
For i = 28 To lastRow
If Right(ws.Cells(i, "A"), 5) = "Total" Then
strCellAddress = "L" & i
Exit For
End If
Next i
If strCellAddress <> "" Then
cellValue = ws.Range(strCellAddress).Value
ActiveCell.Value = cellValue
End If
'Other code
I run code it happens nothing .i want to pupulate if col A has the word " Total " populate value in column L to active cell and down ward for all col A has the word "Total".
Your code shows the result in active cell in each time, now it shows the first one that match condition. If you break or delete Exit for in your code the result in active cell will show the last one in column L.
You need to know what the active cell is while running your code. For easy to understand you should specify the target cell to show your result like this.
'Other code
If strCellAddress <> "" Then
cellValue = ws.Range(strCellAddress).Value
' ActiveCell.Value = cellValue
With ThisWorkbook.Worksheets("Sheet1")
.Range("h5").Value = cellValue
End With
End If
'Other code