Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将首字母大写文本和首字母小写文本复制到两列_Excel_Vba - Fatal编程技术网

Excel 将首字母大写文本和首字母小写文本复制到两列

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

我需要读取一个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: " & 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

好运

建议您添加一个输入数据、期望输出和实际输出的示例,以便更清楚地了解问题所在。这将使问题更清楚,更容易帮助你解决