: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

ขอแก้ไขปรับ Code 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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: ขอแก้ไขปรับ Code VBA ในการเพิ่มข้อมูล

Re: ขอแก้ไขปรับ Code VBA ในการเพิ่มข้อมูล

#4

by snasui » Sat Jan 11, 2025 8:41 am

:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub test()

    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim lastRow As Long
    Dim targetRow As Long
    Dim col As String
    
    ' Create a new worksheet for the target
    Set wsTarget = Worksheets.Add
    wsTarget.Name = "TargetSheet"

    ' Specify the column to check (e.g., "A" for column A)
    col = "i"

    ' Initialize the target row
    targetRow = 1

    For Each wsSource In Worksheets
        If wsSource.Name <> "TargetSheet" Then
            ' Set the source worksheet
        '    Set wsSource = ActiveSheet
        
            ' Loop through each row from the bottom up
            lastRow = wsSource.Cells(wsSource.Rows.Count, col).End(xlUp).Row
            For i = lastRow To 1 Step -1
                Set cell = wsSource.Cells(i, col)
        
                ' Check if the cell in the specified column is not empty
                If cell.Value <> "" Then
                    ' Copy the row to the target sheet
                    wsSource.Rows(i).Copy Destination:=wsTarget.Rows(targetRow)
                    ' Delete the row from the source sheet
                    wsSource.Rows(i).EntireRow.Delete
                    ' Move to the next row in the target sheet
                    targetRow = targetRow + 1
                End If
            Next i
        End If
    Next wsSource
End Sub

Re: ขอแก้ไขปรับ Code VBA ในการเพิ่มข้อมูล

#3

by predee16 » Fri Jan 10, 2025 10:13 am

Sub test() ครับ
Attachments
AddDATA.xlsm
(28.74 KiB) Downloaded 15 times

Re: ขอแก้ไขปรับ Code VBA ในการเพิ่มข้อมูล

#2

by logic » Thu Jan 09, 2025 4:44 pm

เปิดไฟล์ดูแล้วไม่เห็นมีโค้ด ลืมแนบหรือเปล่าครับ

🤔

ขอแก้ไขปรับ Code VBA ในการเพิ่มข้อมูล

#1

by predee16 » Wed Jan 08, 2025 12:28 pm

ผมมีข้อมูลอยู่ 3 Sheet -> Sheet1,Sheet2,Sheet3 และต้องการข้อมูลบางส่วนมาเพิ่มใน ->TargetSheet
เงื่อนไขคือ คอลัมภ์ i <> ""
ผมต้องการแก้ไข Code คือ ย้ายนำข้อมูลมาตามเงื่อนไขมาเพิ่มที่ TargetSheet ต่อลงมาเรื่อยๆ
และลบข้อมูลออกจาก Sheet เดิม ตามตัวอย่างครับ

Code: Select all

Sub test()

Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim lastRow As Long
    Dim targetRow As Long
    Dim col As String

    ' Set the source worksheet
    Set wsSource = ActiveSheet

    ' Create a new worksheet for the target
    Set wsTarget = Worksheets.Add
    wsTarget.Name = "TargetSheet"

    ' Specify the column to check (e.g., "A" for column A)
    col = "i"

    ' Initialize the target row
    targetRow = 1

    ' Loop through each row from the bottom up
    lastRow = wsSource.Cells(wsSource.Rows.Count, col).End(xlUp).Row
    For i = lastRow To 1 Step -1
        Set cell = wsSource.Cells(i, col)

        ' Check if the cell in the specified column is not empty
        If cell.Value <> "" Then
            ' Copy the row to the target sheet
            wsSource.Rows(i).Copy Destination:=wsTarget.Rows(targetRow)
            ' Delete the row from the source sheet
            wsSource.Rows(i).EntireRow.Delete
            ' Move to the next row in the target sheet
            targetRow = targetRow + 1
        End If
    Next i
End Sub
Attachments
AddDATA.png
AddDATA.png (131.52 KiB) Viewed 206 times
AddDATA.xlsm
(22.82 KiB) Downloaded 12 times

Top