Excel 将首字母大写文本和首字母小写文本复制到两列
我需要读取一个txt文件,并将第一个字母大写的元素复制到一列,将第一个字母小写的元素复制到另一列Excel 将首字母大写文本和首字母小写文本复制到两列,excel,vba,Excel,Vba,我需要读取一个txt文件,并将第一个字母大写的元素复制到一列,将第一个字母小写的元素复制到另一列 Public Sub uzd3() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogOpen) Dim filename As String If fd.Show = -1 Then filename = fd.SelectedItems(1) MsgBox ("The path is: " &a
Public Sub uzd3()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
Dim filename As String
If fd.Show = -1 Then
filename = fd.SelectedItems(1)
MsgBox ("The path is: " & filename)
Open "C:\Users\KlaavsZK\Desktop\uzd3.txt" For Input As #1
Else
MsgBox ("No file selected")
Exit Sub
End If
Dim txt As String
Dim i As Integer
i = 0
Do Until EOF(1)
Line Input #1, txt
Cells(1, 1) = "Lielais sakuma burts"
Cells(1, 2) = "mazais sakuma burts"
If Left(Cells(2, 1).Offset(i, 0), 1) = LCase(Left(Cells(2, 1).Offset(i, 0), 1)) Then
Cells(2, 1).Offset(i, 0) = txt
ElseIf Left(Cells(2, 2).Offset(i, 0), 1) = UCase(Left(Cells(2, 2).Offset(i, 0), 1)) Then
Cells(2, 2).Offset(i, 0) = txt
i = i + 1
End If
Loop
Close #1
End Sub
它将元素复制到列中,但不按大小写随机复制。我相信您的问题在于这种比较
Left(Cells(2, 1).Offset(i, 0), 1) = LCase(Left(Cells(2, 1).Offset(i, 0), 1))
Left(Cells(2, 2).Offset(i, 0), 1) = UCase(Left(Cells(2, 2).Offset(i, 0), 1))
您检查的是单元格的值,而不是从文本文件中读取的值
试试这个
Public Sub uzd3()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
Dim filename As String
If fd.Show = -1 Then
'filename = fd.SelectedItems(1)
'MsgBox ("The path is: " & filename)
Open "C:\Users\KlaavsZK\Desktop\uzd3.txt" For Input As #1
Else
MsgBox ("No file selected")
Exit Sub
End If
Dim txt As String
Dim i As Integer
Cells(1, 1) = "Lielais sakuma burts"
Cells(1, 2) = "mazais sakuma burts"
i = 0
Do Until EOF(1)
Line Input #1, txt
If Left(txt, 1) = LCase(Left(txt, 1)) Then
Cells(2, 1).Offset(i, 0) = txt
ElseIf Left(txt, 1) = UCase(Left(txt, 1)) Then
Cells(2, 2).Offset(i, 0) = txt
End If
i = i + 1
Loop
Close #1
End Sub
好运建议您添加一个输入数据、期望输出和实际输出的示例,以便更清楚地了解问题所在。这将使问题更清楚,更容易帮助你解决