: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

VBA copy date

ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ 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: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

VBA copy date

#1

Post by sna »

Hi Dear

I try to write code to loop through files in a folder path.
The file name located in cell of column AD.i want to find if cell value in column AD match file name in the folder path it would loop through sheet to find date from column K or L if column N=0 and column O >0 then offset one row until column N>0 and column O>0 then return date from that row and place date on column AG.if file not found place file not found on AG.and if the data is blank place no data on AG too
Best wishes,


I also attached working file
Attachments
Restructure loan EMI.xlsm
(39.71 KiB) Downloaded 6 times
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#2

Post by sna »

One more thing if data on col AG same as date col in U place OK on col AJ
User avatar
snasui
Site Admin
Site Admin
Posts: 31205
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: VBA copy date

#3

Post by snasui »

sna wrote: Mon Aug 24, 2020 9:28 pm I try to write code to loop through files in a folder path.
The file name located in cell of column AD
:D Could you please attach some file on column AD for test your code?
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#4

Post by sna »

Hi Dear

Here is an attached file

Best wishes
Attachments
PSN_EMI - LD1916105734 - TAING BUNNGUN.xlsx
(15.57 KiB) Downloaded 3 times
User avatar
snasui
Site Admin
Site Admin
Posts: 31205
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: VBA copy date

#5

Post by snasui »

:D Try with this code.

Code: Select all

Sub Import_StartDate()
    Dim wk As Workbook, path As String, Fname As String
    Dim Rng As Range, Dn As Range, Cel As Range
    Dim Ng As Range, Fdt As Range
    Dim Dt As Range, SRn As String
    Dim Vdt As String, FileName As String
    Dim ThisWb As String, Ndata As Double
    Dim i As Long, j As Long
    
    ThisWb = ThisWorkbook.Name
    
    path = "\\op023\Data Center\OPERATION DEPARTMENT\4- CENTRALIZED OPERATION UNIT\4- Restructure Loan in 2020\Schedule Before and After\"
    
    Set Rng = RST1.Range(Range("AD3"), Range("AD" & Rows.Count).End(xlUp))
    
    For Each Dn In Rng
        Fname = Dn & ".xlsx"
        FileName = VBA.FileSystem.Dir(path & Fname)
        If FileName = VBA.Constants.vbNullString Then
            Dn.Offset(0, 3) = "File not found"
        Else
            Set wk = Workbooks.Open(path & Fname)
            Ndata = Range("K" & Rows.Count).End(xlUp).Row
            If Ndata < 3 Then
                Dn.Offset(0, 3) = "No DATA"
                wk.Close SaveChanges:=False
            Else
                Set Cel = Range(Range("K1"), Range("K" & Ndata))
                j = 0
                For i = Cel.Rows.Count To 1 Step -1
                    If Cel(i).Offset(0, 3).Value = 0 And Cel(i).Offset(0, 4) > 0 Then
                        Dn.Offset(0, 3).Value = Cel(i).Offset(1, 0).Value
                        j = j + 1
                        wk.Close False
                        Exit For
                    End If
                Next i
            End If
        End If
        If j = 0 Then
            Dn.Offset(0, 3) = "No DATA"
        End If

        ThisWorkbook.Activate
    Next Dn
    Call MsgBox("Transtion Completed !!!", vbInformation)
End Sub
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#6

Post by sna »

Thank you so much
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#7

Post by sna »

One more thing

Code: Select all

Sub Checking ()
Dim ThisCell1 As Range
Dim ThisCell2 As Range
    For Each ThisCell1 In Range("AG3:AG9999")
    'This is the range of cells to check
        For Each ThisCell2 In Range("U3:U9999")
        'This is the range of cells to compare
            If ThisCell1.Value = ThisCell2.Value Then
                ThisCell1.Offset(, 3).Value = "TRUE"
                Exit For
                End If
            Next ThisCell2
        Next ThisCell1


This process is slow what's wrong?


Best regards
User avatar
snasui
Site Admin
Site Admin
Posts: 31205
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: VBA copy date

#8

Post by snasui »

:D This code loop many cells (9997 * 9997).
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#9

Post by sna »

How to fix this?

Thanks
User avatar
snasui
Site Admin
Site Admin
Posts: 31205
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: VBA copy date

#10

Post by snasui »

:D Your code should scope only data not fixed range.
sna
Silver
Silver
Posts: 721
Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365

Re: VBA copy date

#11

Post by sna »

Ok thanks
Post Reply