Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, _
ByVal dwFlags As Long, ByVal lpMultiByteStr As String, ByVal cbMultiByte As Long, _
ByVal lpWideCharStr As String, ByVal cchWideChar As Long) As Long
Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, _
ByVal dwFlags As Long, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As String, ByVal cbMultiByte As Long, _
ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As LongPtr) As Long
Function GetByteLength(ByVal text As String) As Long
Dim byteLength As Long
byteLength = LenB(StrConv(text, vbFromUnicode))
GetByteLength = byteLength
End Function
Sub TrimTo55BytesExcelStandard()
Dim ws As Worksheet
Dim cell As Range
Dim text As String
Dim i As Integer
Dim byteLen As Long
' Set the worksheet (assuming the data is in the first sheet)
Set ws = ThisWorkbook.Sheets(1)
' Loop through each cell in column B, starting from row 2 (assuming row 1 contains a header)
For Each cell In ws.Range("B2:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row)
text = cell.Value
' Keep removing characters from the end until the byte length is <= 55 based on Excel LENB behavior
Do While GetByteLength(text) > 55
text = Left(text, Len(text) - 1)
Loop
' Update the cell value with the shortened text
cell.Value = text
Next cell
MsgBox "Processing complete!"
End Sub
#엑셀
#VBA
#EXCEL