范围类select多个表之间的语法错误

我一直在devise一个将文本文件导入Excel的macros。 该程序的devise初衷是将所有数据导入到工作表1中,但在得到反馈后,我被告知要将所有数据导入到工作表2中。 这个macros在代码行开头使用Activesheet等命令时没有任何问题,因为sheet1总是活动页面。 *请注意,所有表都有其默认名称。

我已经去了,试图改变我所有的范围fns,而不是通过键入Worksheets("Sheet2").Range("A1")... ,但这给了我

“select范围类的方法”

错误。 我的初始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 

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

两个答案:

坏消息是:你不能从工作表中select一个单元格或者范围。

好消息:不需要select一个单元来分配一个值(或者做任何其他的事情)。 事实上,你应该避免在VBA中select任何东西,几乎没有理由这样做。 相反,只需做一些类似的事情

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