chatgpt吧 关注:137,674贴子:425,452
  • 1回复贴,共1
写一个EXCEL VBA代码,把SHEET1中单元格A1中的文字内容,每行15个字一个字一格写到SHEET2内。


IP属地:辽宁1楼2023-11-23 15:47回复
    Sub CopyText()
    Dim sourceText As String
    Dim targetSheet As Worksheet
    Dim targetRow As Integer
    Dim targetColumn As Integer
    Dim i As Integer
    '获取源文本
    sourceText = Sheet1.Range("A1").Value
    '设置目标工作表
    Set targetSheet = ThisWorkbook.Sheets("Sheet2")
    '计算目标行和列
    targetRow = 1
    targetColumn = 3
    '逐字复制文本到目标工作表
    For i = 1 To Len(sourceText)
    targetSheet.Cells(targetRow, targetColumn).Value = Mid(sourceText, i, 1)
    targetColumn = targetColumn + 1
    If Mid(sourceText, i, 1) = Chr(10) Then
    targetRow = targetRow + 1
    targetColumn = 3
    End If
    '如果列数达到15,则换行并重置列数
    If targetColumn > 15 Then
    targetRow = targetRow + 1
    targetColumn = 1
    End If
    Next i
    End Sub


    IP属地:辽宁3楼2023-12-20 15:43
    回复