snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Sub FormatLoanData()
Dim ws As Worksheet
Dim outputWs As Worksheet
Dim lastRow As Long
Dim currentRow As Long
Dim outputRow As Long
Dim currentOfficer As String
' Set the worksheets
Set ws = ThisWorkbook.Sheets("RawData") ' Change to your actual data sheet name
Set outputWs = ThisWorkbook.Sheets.Add
outputWs.Name = "Output"
outputRow = 2 ' Start output from the second row
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Get last row with data
For currentRow = 2 To lastRow ' Assuming first row is header
' Check for Credit Officer header
If InStr(ws.Cells(currentRow, 1), "Credit Officer:") > 0 Then
currentOfficer = ws.Cells(currentRow, 1).Value ' Store current officer
' Skipping the credit officer line
ElseIf ws.Cells(currentRow, 1).Value <> "" Then
' Copy Data to formatted worksheet
outputWs.Cells(outputRow, 1).Value = ws.Cells(currentRow, 1).Value
outputWs.Cells(outputRow, 2).Value = ws.Cells(currentRow, 2).Value
outputWs.Cells(outputRow, 3).Value = ws.Cells(currentRow, 3).Value
outputWs.Cells(outputRow, 4).Value = ws.Cells(currentRow, 4).Value
outputRow = outputRow + 1 ' Increment output row counter
End If
Next currentRow
End Sub
Please help fix it to get outputs like the hightlighted sheet.
Noted:you can change variable to whatever it is easier to read as well.
thank you in advance
You do not have the required permissions to view the files attached to this post.
Sub TransformData()
Dim i As Integer, srAll As Range, tgAll As Range, r As Range
With Worksheets("RawData")
Set srAll = .Range("a6", .Range("d" & .Rows.Count).End(xlUp))
End With
With Worksheets("required")
.UsedRange.ClearContents
.Range("a1").Resize(srAll.Rows.Count, srAll.Columns.Count) _
.Value = srAll.Value
Set tgAll = .Range("b2", .Range("b" & .Rows.Count).End(xlUp))
For Each r In tgAll
If IsNumeric(VBA.Right(r.Value, 9)) Then
r.Offset(0, -1).Value = r.Offset(-1, -1).Value + 1
End If
Next r
tgAll.SpecialCells(xlCellTypeBlanks).EntireRow.Delete shift:=xlUp
End With
End Sub