Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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_Text - Fatal编程技术网

Excel 如何在将文本格式化为大写时导入文本文件

Excel 如何在将文本格式化为大写时导入文本文件,excel,vba,text,Excel,Vba,Text,如何在将所有文本格式化为大写时导入数据 我尝试了Destination:=Range(UCase($A$1”)没有错误,但这不起作用,我不想在导入文件后运行循环 Option Explicit Public Sub ImportData() ActiveWorkbook.Worksheets.Add With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & Environ("USERPROFILE"

如何在将所有文本格式化为大写时导入数据

我尝试了
Destination:=Range(UCase($A$1”)
没有错误,但这不起作用,我不想在导入文件后运行循环

Option Explicit
Public Sub ImportData()
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & Environ("USERPROFILE") & _
        "\Desktop\cisco.txt", Destination:=Range("$A$1") _
        )
        .Name = "ImportingFileName"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, xlTextFormat)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        .WorkbookConnection.Delete
    End With
End Sub

这就是你想要的吗

Option Explicit

Dim ws As Worksheet

Sub ImportData()
    Set ws = ThisWorkbook.Worksheets.Add

    With ws.QueryTables.Add(Connection:= _
        "TEXT;" & Environ("USERPROFILE") & _
        "\Desktop\cisco.txt", Destination:=ws.Range("$A$1") _
        )
        .Name = "ImportingFileName"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, xlTextFormat)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        .WorkbookConnection.Delete
    End With

    ChangeRngToUpperCase
End Sub

'~~> Function to Change an entire range to upper case without
'~~> looping through each cell
Sub ChangeRngToUpperCase()
    Dim lRow As Long, lCol As Long
    Dim tmpAr()
    Dim rng As Range

    With ws
        '~~> Find last row and last column
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row

            lCol = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Column

            '~~> Indentify your range
            Set rng = .Range(.Cells(1, 1), .Cells(lRow, lCol))
            '~~> Convert entire range to upper case and store it in an array
            tmpAr = Evaluate("INDEX(UPPER(" & rng.Address(External:=True) & "),)")
            '~~> Tranfer data back to range
            rng = tmpAr
        End If
    End With
End Sub
选项显式
将ws设置为工作表
子导入数据()
设置ws=ThisWorkbook.Worksheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”&Environ(“用户配置文件”)&_
“\Desktop\cisco.txt”,目的地:=ws.Range(“$A$1”)_
)
.Name=“导入文件名”
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,xlTextFormat)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
.WorkbookConnection.Delete
以
转换双酶
端接头
“~~>函数将整个范围更改为大写,而不使用
“~~>在每个单元格中循环
子变更rngtouppercase()
变暗lRow尽可能长,lCol尽可能长
Dim tmpAr()
变暗rng As范围
与ws
“~~~>查找最后一行和最后一列
如果Application.WorksheetFunction.CountA(.Cells)为0,则
lRow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
lCol=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
SearchOrder:=xlByColumns_
搜索方向:=xlPrevious_
MatchCase:=False)。列
“~~>确定您的范围
设置rng=.Range(.Cells(1,1),.Cells(lRow,lCol))
“~~>将整个范围转换为大写并存储在数组中
tmpAr=评估(“索引(上限(&rng.Address(外部:=True)&”))
“~~>将数据传输回范围
rng=tmpAr
如果结束
以
端接头

这就是您要尝试的吗

Option Explicit

Dim ws As Worksheet

Sub ImportData()
    Set ws = ThisWorkbook.Worksheets.Add

    With ws.QueryTables.Add(Connection:= _
        "TEXT;" & Environ("USERPROFILE") & _
        "\Desktop\cisco.txt", Destination:=ws.Range("$A$1") _
        )
        .Name = "ImportingFileName"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, xlTextFormat)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        .WorkbookConnection.Delete
    End With

    ChangeRngToUpperCase
End Sub

'~~> Function to Change an entire range to upper case without
'~~> looping through each cell
Sub ChangeRngToUpperCase()
    Dim lRow As Long, lCol As Long
    Dim tmpAr()
    Dim rng As Range

    With ws
        '~~> Find last row and last column
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row

            lCol = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Column

            '~~> Indentify your range
            Set rng = .Range(.Cells(1, 1), .Cells(lRow, lCol))
            '~~> Convert entire range to upper case and store it in an array
            tmpAr = Evaluate("INDEX(UPPER(" & rng.Address(External:=True) & "),)")
            '~~> Tranfer data back to range
            rng = tmpAr
        End If
    End With
End Sub
选项显式
将ws设置为工作表
子导入数据()
设置ws=ThisWorkbook.Worksheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”&Environ(“用户配置文件”)&_
“\Desktop\cisco.txt”,目的地:=ws.Range(“$A$1”)_
)
.Name=“导入文件名”
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,xlTextFormat)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
.WorkbookConnection.Delete
以
转换双酶
端接头
“~~>函数将整个范围更改为大写,而不使用
“~~>在每个单元格中循环
子变更rngtouppercase()
变暗lRow尽可能长,lCol尽可能长
Dim tmpAr()
变暗rng As范围
与ws
“~~~>查找最后一行和最后一列
如果Application.WorksheetFunction.CountA(.Cells)为0,则
lRow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
lCol=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
SearchOrder:=xlByColumns_
搜索方向:=xlPrevious_
MatchCase:=False)。列
“~~>确定您的范围
设置rng=.Range(.Cells(1,1),.Cells(lRow,lCol))
“~~>将整个范围转换为大写并存储在数组中
tmpAr=评估(“索引(上限(&rng.Address(外部:=True)&”))
“~~>将数据传输回范围
rng=tmpAr
如果结束
以
端接头

您是否尝试过
目的地:=UCase(范围($A$1”)
?您是否尝试过
目的地:=UCase(范围($A$1”)
?您是这样的人,但我在
范围($A$1)上遇到了错误
.Range
编译错误
无效或不合格的引用
如果我删除
它works@0m3r对不起,我错过了。是在其前面添加
ws
。我在复制和粘贴时错过了它…类似这样的东西,但是如果我删除
点,我在
.Range($A$1”)
上得到了错误