2017-08-16 72 views
0

我一直在设计一个将文本文件导入Excel的宏。该计划的目的是最初将所有数据导入到工作表1中,但在得到反馈后,我被告知要将所有数据导入工作表2中。在代码行的开头使用诸如Activesheet之类的命令时,该宏没有任何问题,因为sheet1始终是活动表单。 *请注意,所有工作表都有其默认名称。范围类选择多个表之间的语法错误

我已经在和试图改变我的所有范围FNS由不是键入Worksheets("Sheet2").Range("A1")...都把表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 

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

回答

1

两个答案:

坏消息:你不能select从工作表是不活跃格或单元格区域。好消息:不需要选择一个单元格来分配一个值(或者用它做任何其他的事情)。事实上,你应该避免在VBA中使用select任何东西,几乎没有理由这样做。相反,只需做一些类似于

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

好的。谢谢FunThomas。这是朝着正确方向迈出的重要一步。所以你说我的下面的代码用于格式化范围将无法以我想要的方式工作? (2).Range(“A3:D10003”)。 Selection.Horizo​​ntalAlignment = xlLeft –

+0

只需更改为'Worksheets(2).Range(“A3:D10003”)。Horizo​​ntalAlignment = xlLeft' – FunThomas

+0

哇,这很直观。我很感激你的耐心。很明显,我是基于对象编程的新手。 –