snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
คำแสดงเจตนา
ขอผลแห่งการให้ความรู้นี้ จงกลับไปยังผู้ที่เป็นเจ้าของเดิม แม้ข้าพเจ้าจะไม่รู้จักท่านก็ตาม ขอให้แสงแห่งปัญญาที่ท่านเคยจุดไว้ ได้กลับไปเติมเต็มชีวิตของท่านอีกครั้ง และขอให้เจตนาของข้าพเจ้าเป็นการคืนความดีอย่างสงบ
ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
sna
Silver
Posts: 699 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#1
Post
by sna » Wed Aug 14, 2024 4:13 pm
hi there
I need your help with vba to transform to convert values from one currency to another currency with division of exchange rate in AD2.
I also need to extract name in column A to column AE like example.how to merge my code into one with simplified vba
Code: Select all
Sub Combinetabs()
Sheets.Add
ActiveSheet.Name = "New Sheet"
Set Dsheet = ActiveSheet
For Each ws In Sheets
If ws.Name <> "New Sheet" Then
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Dsheet.Cells(Rows.Count, "A").End(xlUp).Row
If lr2 = 1 Then lr2 = 0
ws.Rows("1:" & lr).Copy Dsheet.Range("A" & lr2 + 1)
End If
Next ws
End Sub
Sub Transform()
Set sh = ThisWorkbook.Sheets("New Sheet")
lr = sh.Cells(Rows.Count, "B").End(xlUp).Row
usdRate = sh.Range("AD2").Value
For i = 2 To lr
With sh
If .Cells(i, "F").Value = "KHR" Then
.Range("D" & i).Value = .Range("D" & i).Value / usdRate
.Range("E" & i).Value = .Range("E" & i).Value / usdRate
.Range("N" & i).Value = .Range("N" & i).Value / usdRate
.Range("P" & i).Value = .Range("P" & i).Value / usdRate
.Range("Q" & i).Value = .Range("Q" & i).Value / usdRate
.Range("R" & i).Value = .Range("R" & i).Value / usdRate
.Range("T" & i).Value = .Range("T" & i).Value / usdRate
.Range("U" & i).Value = .Range("U" & i).Value / usdRate
.Range("W" & i).Value = .Range("W" & i).Value / usdRate
.Range("X" & i).Value = .Range("X" & i).Value / usdRate
.Range("Y" & i).Value = .Range("Y" & i).Value / usdRate
.Range("Z" & i).Value = .Range("Z" & i).Value / usdRate
End If
End With
Next
End Sub
thx
You do not have the required permissions to view the files attached to this post.
snasui
Site Admin
Posts: 31175 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#2
Post
by snasui » Wed Aug 14, 2024 7:18 pm
The example code is below.
Code: Select all
Sub combinetabs()
Sheets.Add before:=Sheets(1)
ActiveSheet.Name = "New Sheet"
Set Dsheet = ActiveSheet
For Each ws In Sheets
If ws.Name <> "New Sheet" Then
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Dsheet.Cells(Rows.Count, "A").End(xlUp).Row
If lr2 = 1 Then lr2 = 0
ws.Rows("1:" & lr).Copy Dsheet.Range("A" & lr2 + 1)
End If
Next ws
With Worksheets("New Sheet")
lr = .Cells(Rows.Count, "B").End(xlUp).Row
usdRate = .Range("AD2").Value
If IsEmpty(usdrange) Then
MsgBox "Please enter USD rate in cell AD2.", vbCritical
Exit Sub
End If
For i = 2 To lr
If .Cells(i, "F").Value = "KHR" Then
.Range("D" & i).Value = .Range("D" & i).Value / usdRate
.Range("E" & i).Value = .Range("E" & i).Value / usdRate
.Range("N" & i).Value = .Range("N" & i).Value / usdRate
.Range("P" & i).Value = .Range("P" & i).Value / usdRate
.Range("Q" & i).Value = .Range("Q" & i).Value / usdRate
.Range("R" & i).Value = .Range("R" & i).Value / usdRate
.Range("T" & i).Value = .Range("T" & i).Value / usdRate
.Range("U" & i).Value = .Range("U" & i).Value / usdRate
.Range("W" & i).Value = .Range("W" & i).Value / usdRate
.Range("X" & i).Value = .Range("X" & i).Value / usdRate
.Range("Y" & i).Value = .Range("Y" & i).Value / usdRate
.Range("Z" & i).Value = .Range("Z" & i).Value / usdRate
End If
Next
End With
End Sub
sna
Silver
Posts: 699 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#3
Post
by sna » Fri Aug 16, 2024 9:54 am
thanks
One more i need your help with code to add to the previous to get ID name in column AC without writing formula as in attach
thanks
You do not have the required permissions to view the files attached to this post.
snasui
Site Admin
Posts: 31175 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#4
Post
by snasui » Fri Aug 16, 2024 8:55 pm
The example code is below.
Code: Select all
Sub combinetab()
Sheets.Add before:=Sheets(1)
ActiveSheet.Name = "New Sheet"
Set Dsheet = ActiveSheet
For Each ws In Sheets
If ws.Name <> "New Sheet" Then
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Dsheet.Cells(Rows.Count, "A").End(xlUp).Row
If lr2 = 1 Then lr2 = 0
ws.Rows("1:" & lr).Copy Dsheet.Range("A" & lr2 + 1)
End If
Next ws
usdrate = Sheet1.Range("AD1").Value
If IsEmpty(usdrate) Then
MsgBox "Please enter USD rate ", vbCritical
Exit Sub
End If
With Worksheets("New Sheet")
lr = .Cells(Rows.Count, "B").End(xlUp).Row
idn = ""
For i = 2 To lr
If IsNumeric(VBA.Left(.Cells(i, "a"), 4)) And IsEmpty(.Cells(i, "b").Value) Then
idn = .Cells(i, "a").Value
End If
If .Cells(i, "F").Value = "KHR" Then
.Range("D" & i).Value = .Range("D" & i).Value / usdrate
.Range("E" & i).Value = .Range("E" & i).Value / usdrate
.Range("N" & i).Value = .Range("N" & i).Value / usdrate
.Range("P" & i).Value = .Range("P" & i).Value / usdrate
.Range("Q" & i).Value = .Range("Q" & i).Value / usdrate
.Range("R" & i).Value = .Range("R" & i).Value / usdrate
.Range("T" & i).Value = .Range("T" & i).Value / usdrate
.Range("U" & i).Value = .Range("U" & i).Value / usdrate
.Range("W" & i).Value = .Range("W" & i).Value / usdrate
.Range("X" & i).Value = .Range("X" & i).Value / usdrate
.Range("Y" & i).Value = .Range("Y" & i).Value / usdrate
.Range("Z" & i).Value = .Range("Z" & i).Value / usdrate
End If
If Not IsEmpty(.Range("ab" & i).Value) Then
.Range("ac" & i).Value = idn
End If
Next
End With
End Sub
sna
Silver
Posts: 699 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#5
Post
by sna » Fri Aug 16, 2024 10:40 pm
Thanks so much