【发布时间】:2016-09-15 10:07:11
【问题描述】:
我有这个 vba 宏,它从文本文件中提取数据并将其放入 Excel 中的列中。这些文件以天数 (2016mmdd) 命名。目前,我每天都运行这个宏。现在我希望它在运行此宏时,声明月份(例如八月)中所有天的数据将自动提取到不同的列中(每个月的每一天一列)。这样如果一个月有 31 天,我就不必手动运行 31 次。感谢您的帮助。
Sub Macro7()
'
' Macro7 Macro
'
' Keyboard Shortcut: Ctrl+x
'
Dim fileDate, rng, rng1, rng2, rng3, rcell As String
b = InputBox("Enter file Name mmdd", "File name")
rcell = InputBox("Enter cell reference", "Reference name")
rng = "$" & rcell & "$2"
rng1 = rcell & "2:" & rcell & "14"
rng2 = rcell & "52:" & rcell & "62"
rng3 = rcell & "2:" & rcell & "101"
Filename = "j:\files\2016" & b & "2259.txt"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;j:\files2016" & b & "2259.txt", Destination:= _
Range(rng))
.Name = "tr" & b
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 1, 9)
.TextFileFixedColumnWidths = Array(103, 4)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range(rng1).Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=45
Range(rng2).Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=-60
Range(rng3).Select
End Sub
【问题讨论】:
-
你用
for-loop标记了这个...你试过用一个吗?如果我理解正确,您想为b="0801" ... b="0831"运行宏吗? -
不是真的,@arcadeprecinct。这个宏在用户输入日期和 excel 列(比如 a、b、c 或 d)之后从文本文件中提取数据,并且他/她必须每天都这样做,现在,我只想要一个月将被输入,所有日期的数据将被提取到 Excel 工作表的不同列中。谢谢。