我有这个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
不是真的,@arcadeprecinct。这个宏在用户输入日期和excel列(比如a,b,c或d)和他/她必须每天都这样做之后从文本文件中提取数据。现在,我希望它只有一个月将被输入并且所有日期的数据将被提取到Excel工作表的不同列中。谢谢。 – MordC