Vba 多个工作表之间的范围类选择语法错误

Vba 多个工作表之间的范围类选择语法错误,vba,excel,csv,range,Vba,Excel,Csv,Range,我一直在设计一个宏,将文本文件导入Excel。该程序最初设计为将所有数据导入表1,但在收到反馈后,我被告知将所有数据导入表2。在代码行的开头使用诸如Activesheet之类的命令时,该宏不会出现任何问题,因为sheet1始终是活动工作表*注意:所有图纸都有其默认名称 我已经进去了,并试图通过键入工作表Sheets2.RangeA1…,将我所有的范围FN改为表2,但这给了我机会 范围类的选择方法 错误。此错误发生在使用查询表导入文件的初始fn之后 Option Explicit Sub impo

我一直在设计一个宏,将文本文件导入Excel。该程序最初设计为将所有数据导入表1,但在收到反馈后,我被告知将所有数据导入表2。在代码行的开头使用诸如Activesheet之类的命令时,该宏不会出现任何问题,因为sheet1始终是活动工作表*注意:所有图纸都有其默认名称

我已经进去了,并试图通过键入工作表Sheets2.RangeA1…,将我所有的范围FN改为表2,但这给了我机会

范围类的选择方法

错误。此错误发生在使用查询表导入文件的初始fn之后

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
这似乎是一个非常简单的问题,但我不知道如何避免这些错误

两个答案:

坏消息是:无法从非活动工作表中选择单元格或区域

好消息是:无需选择单元格来赋值或对其执行任何其他操作。事实上,您应该避免选择VBA中的任何内容,几乎没有理由这样做。相反,只需做如下事情

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

好啊谢谢你,托马斯。这是朝着正确方向迈出的一大步。那么你是说我下面用来格式化一个范围的代码将不能以我想要的当前方式工作?工作表2.RangeA3:D10003.Select Selection.HorizontalAlignment=xlLefts只需更改为工作表2.RangeA3:D10003.HorizontalAlignment=xlLeftWow,这很直观。我感谢你的耐心。显然,我对基于对象的编程还不熟悉。