Как импортировать текстовый файл при форматировании текста в верхний регистр - PullRequest
0 голосов
/ 10 января 2019

Как импортировать данные при форматировании всего текста в верхний регистр?

Я попытался 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

1 Ответ

0 голосов
/ 10 января 2019

Это то, что вы пытаетесь?

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...