:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

fetch cell value to activecell

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

fetch cell value to activecell

#1

Post by sna »

Hi there
i have some codes to copy values from file in a folder of sharedrive .it is not working due to last row containing blank cells

Code: Select all

  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.
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#2

Post by snasui »

:D Please attach the example of source file.
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#3

Post by sna »

here is a sample
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#4

Post by snasui »

:D Your code does not match with your latest file. Please check the file name and sheet name in your code again.
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#5

Post by sna »

I change but it still not working
strWorkbookName = "FA Depreciation as of Jul-23.xlsx"
strSheetName = "After"
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#6

Post by snasui »

:D Please attach the updated file again.
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#7

Post by sna »

here it is
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#8

Post by snasui »

:D The word "Total" is in column A not D. You can adjust your code as below.

Code: Select all

'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
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#9

Post by sna »

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".
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#10

Post by snasui »

:D 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.
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#11

Post by sna »

i try when run code it shows nothing
User avatar
snasui
Site Admin
Site Admin
Posts: 30988
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: fetch cell value to activecell

#12

Post by snasui »

:D 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.

Code: Select all

'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
sna
Silver
Silver
Posts: 699
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: fetch cell value to activecell

#13

Post by sna »

thanks
Post Reply