[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『Excelマクロで更新日時最新のテキストファイルを取り込みたい』(まーちゅん)
Excelマクロで[ダウンロード]フォルダ内の更新日時が最新のテキストファイルをExcelに取り込むマクロのコードはどう書けばいいですか?テキストファイルのデータは全て文字列(UTF-8)で取り込みたいです。行や列がズレないようにしたいです。よろしくお願いします。
< 使用 Excel:Excel2013、使用 OS:Windows10 >
>マクロのコードはどう書けばいいですか? 人任せではなくマクロ記録してみればどう。 (nm) 2021/12/22(水) 16:35
Sub Sample() Dim FilePath As String Dim FileName As String Dim NewestDate As Date Dim NewFileName As String Dim d As Date With CreateObject("Wscript.Shell") FilePath = .SpecialFolders("MyDocuments") End With FilePath = Left(FilePath, InStrRev(FilePath, "\")) & "DownLoads" If Dir(FilePath, vbDirectory) = "" Then Exit Sub 'DownLoadsフォルダが見つからなかったら終了
FileName = Dir(FilePath & "\*.*") Do While FileName <> "" d = FileDateTime(FilePath & "\" & FileName) If d > NewestDate Then NewestDate = d NewFileName = FileName End If FileName = Dir Loop MsgBox FilePath & "\" & NewFileName & vbCrLf & NewestDate End Sub
続きは以下参照
http://officetanaka.net/excel/vba/file/file10.htm
(きまぐれおじさん) 2021/12/22(水) 16:52
他に何か新しいファイルがあってもそれは無視して
最新のテキストファイルを取り込むマクロが知りたいです
(まーちゅん) 2021/12/22(水) 17:00
Dim buf As String, Target As String, i As Long Dim tmp As Variant, j As Long Target = "D:\Work\UTF-8のテキスト.csv" With CreateObject("ADODB.Stream") .Charset = "UTF-8" .Open .LoadFromFile Target Do Until .EOS buf = .ReadText(-2) i = i + 1 tmp = Split(buf, ",") For j = 0 To UBound(tmp) Cells(i, j + 1) = tmp(j) Next j Loop .Close End With End Sub
不慣れですみません
(まーちゅん) 2021/12/22(水) 17:03
https://www.google.com/search?q=VBA+DIR+%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB
(ひまつぶし) 2021/12/22(水) 17:17
With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\あああ\Downloads\7033415433018981.txt", Destination:= _ Range("$A$1")) .Name = "7033415433018981" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, _ 2, 2, 2, 2, 2, 2, 2, 2) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub
(ららんらん) 2021/12/22(水) 19:03
具体的には、Dir関数でワイルドカードを使うことによって可能になります。
下記のサイトを参考にして情報を取得してください。
http://officetanaka.net/excel/vba/tips/tips69.htm
(γ) 2021/12/22(水) 20:10
Sub test() Dim wsh As Object Dim p As String Dim cmd As String Dim target As String
Set wsh = CreateObject("wscript.shell") p = wsh.SpecialFolders("mydocuments") & "\..\Downloads\" cmd = "cmd /c dir """ & p & "*.txt"" /b/o-d" target = Split(wsh.exec(cmd).stdout.readall, vbCrLf)(0) MsgBox target
End Sub (マナ) 2021/12/22(水) 20:14
二重人格?
(?) 2021/12/22(水) 20:34
>ちなみにマクロの記録で作成したコードは以下になります >.TextFilePlatform = 932
操作を間違えていませんか。
(マナ) 2021/12/22(水) 20:42
2021/12/22(水) 20:10のコメントは無視ですか? | 回答ありがとうございます | 早速実行してみたのですが、ダウンロードフォルダ内の更新日時が一番新しい | テキストファイルを取り込みたいのですが、今回 私のダウンロードフォルダ内の最新のファイルはpdfでした | なので取り込むことはできませんでした。(メッセージにもpdfと表示されました) ということだったので、その対応策を提示したつもりです。 参照サイトの一番始めに書いてあるように、 FileName = Dir(FilePath & "\*.txt") のようにすると改善するんですが。
(γ) 2021/12/22(水) 23:13
同じ人
(ベム) 2021/12/23(木) 01:38
(マナ) 2021/12/23(木) 18:36
(マナ) 2021/12/23(木) 18:49
(?) 2021/12/23(木) 20:16
(ららんらん) 2021/12/23(木) 20:31
そうですか、マクロクくれくれ君がよくつかう手法ですね。
(くれくれ君?) 2021/12/23(木) 20:39
(マナ) 2021/12/23(木) 20:41
(マナ) 2021/12/23(木) 21:59
(マナ) 2021/12/23(木) 22:34
Sub test() Dim wsh As Object Dim p As String Dim cmd As String Dim src As String Dim dst As Worksheet
Set wsh = CreateObject("wscript.shell") p = wsh.SpecialFolders("mydocuments") & "\..\Downloads\" cmd = "cmd /c dir """ & p & "*.txt"" /b/o-d" src = p & Split(wsh.exec(cmd).stdout.readall, vbCrLf)(0)
Set dst = ThisWorkbook.Sheets("sheet1") dst.UsedRange.ClearContents With dst.QueryTables.Add( _ Connection:="TEXT;" & src, Destination:=dst.Range("A1")) .TextFilePlatform = 932 .TextFileTabDelimiter = True .Refresh BackgroundQuery:=False .Delete End With
End Sub
(マナ) 2021/12/23(木) 23:51
Sub test2() Dim wsh As Object Dim p As String Dim cmd As String Dim src As String Dim dst As Worksheet Dim k As Long Dim fi(1 To 50) As Long '★txtファイルの列数が50以下
Set wsh = CreateObject("wscript.shell") p = wsh.SpecialFolders("mydocuments") & "\..\Downloads\" cmd = "cmd /c dir """ & p & "*.txt"" /b/o-d" src = p & Split(wsh.exec(cmd).stdout.readall, vbCrLf)(0)
For k = 1 To UBound(fi) fi(k) = xlTextFormat Next
Set dst = ThisWorkbook.Sheets("sheet1") dst.UsedRange.ClearContents With dst.QueryTables.Add( _ Connection:="TEXT;" & src, Destination:=dst.Range("A1")) .TextFilePlatform = 932 .TextFileTabDelimiter = True .TextFileColumnDataTypes = fi .Refresh BackgroundQuery:=False .Delete End With
End Sub
(マナ) 2021/12/24(金) 17:40
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.