多个表格之间的范围类选择语法错误

时间:2017-08-16 13:51:43

标签: excel vba excel-vba csv range

我一直在设计一个将文本文件导入Excel的宏。该程序旨在最初将所有数据导入到表1中,但在得到反馈之后,我被告知要将所有数据放入表2中。在代码行的开头使用Activesheet等命令时,此宏可以正常工作,因为sheet1始终是活动工作表。 *请注意,所有工作表都有其默认名称。

我已经进去并试图通过输入Worksheets("Sheet2").Range("A1")...来改变我所有的范围fns来看表2,但这给了我

  

"选择Range类的方法"

错误。在我的初始fn之后,使用Query表导入文件时会发生此错误。

Option Explicit
Sub importtxt()

Dim txtloc As Variant

Dim build As String

Dim bit As String

Dim rng As Range

'Asks user for the build number that has been imported, then assigns that 
string to cell B1
build = InputBox("What build of SoundCheck is this?")

'Prompt Bitness
bit = InputBox("Please provide the bitness of this SoundCheck")

'Asks user for location of the Time_Memlog.txt file to be imported
txtloc = Application.GetOpenFilename _
     (FileFilter:="Text Filer (*.txt),*.txt", _
     title:="Open File(s)", MultiSelect:=False)

'Imports .txt file designated in the txtloc string
With Sheets("Sheet2").QueryTables.Add(Connection:="TEXT;" & txtloc, 
destination:=Worksheets(2).Range("$A$1"))

    .Name = "Sample"
    .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)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

'Clears the garbage in cell A1
Worksheets("Sheet2").Range("$A$1").Select
ActiveCell.Clear



'Places the string build in cell A1
Worksheets(2).Range("A1").Select
ActiveCell.Value = "Build:"



Worksheets(2).Range("B1").Select
ActiveCell.Value = build

Worksheets(2).Range("C1").Select
ActiveCell.Value = bit

'Selects all columns of the Time_Memlog and adjusts the column width to fit 
heading
Worksheets(2).Range("A1:S10003").Select
Selection.Columns.AutoFit

'Makes column headers bold text
Sheets("Sheet2").Range("A2:D2").Font.Bold = True

'Create borders around cell range A2:D2
Set rng = Worksheets(2).Range("A2:D2")

With rng.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
End With

'Give background color to cells A2:D2
With rng.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent6
    .TintAndShade = 0.599993896298105
    .PatternTintAndShade = 0
End With

'Aligns all cells below Column headers to the left
Worksheets(2).Range("A3:D10003").Select
Selection.HorizontalAlignment = xlLeft

'Give background color to cells A1:C1
Worksheets(2).Range("A1:C1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.149998474074526
    .PatternTintAndShade = 0
End With
Selection.HorizontalAlignment = xlLeft

Worksheets(2).Range("D1").Select
Selection.Clear



End Sub

这似乎是一个非常简单的问题,但我不知道如何解决这些错误。

1 个答案:

答案 0 :(得分:1)

两个答案:

坏消息:您不能select来自未激活的工作表的单元格或范围。

好消息:无需选择一个单元格来分配值(或者用它做任何其他事情)。实际上你应该避免在VBA内select任何事情,几乎没有理由这样做。相反,只需执行类似

的操作
with Worksheets(2)
    .range("A2").value = "Build:"
    ' or: .cells(1,1).value = "Build:"
    ...
end with