• 締切済み

マクロのWEBデータの取り込み

下のように書いてマクロを実行したのですが(EXCEL2003) Sheet1のA1からDP1000のデータをクリアにしてから URLのデータを取り込んで D列の最終行の値を Sheet2のA列の日付+1行に書き込みたいのですが Sheet1のA1からDP1000のデータをクリアにする前に D列の最終行の値を Sheet2のA列の日付+1行に書き込んでしまいます。 あとConst Col = 4の部分が 同じ範囲内で宣言が重複しています。 とエラーになります。 どこが悪いのかがわかりません。 よろしくお願いします。 Sub 抽出() Worksheets("Sheet1").Range("A1:DP1000").Value = "" nen = InputBox("読み込む年度、西暦4桁(半角)読み込む月(半角)を入力") tuki = InputBox("読み込む日(半角)を入力") strUrl= "URL;http://○○○○★★★★DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("野菜").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet1").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With Const Col = 4 Dim Rw As Long With Sheets("Sheet1") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet2").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With strUrl= "URL;http://○○○○■■■■DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("Sheet3").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet3").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With Const Col = 4 Dim Rw As Long With Sheets("Sheet3") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet4").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With End Sub

みんなの回答

  • goota33
  • ベストアンサー率53% (7/13)
回答No.1

二か所ほど変更したので試してみてください。 一か所目はSheet1のA1からDP1000のデータをクリアにする前にD列の最終行の値を Sheet2のA列の日付+1行に書き込んでしまうとのことだったので、 変数RwにD列の最後の値を取得させたあと、 すぐA1からDP1000までの範囲をクリアするようにしました。 二か所目は同じ関数内に二回同じ変数の名前を宣言することはできないので その宣言の部分を削除しました。 変更した場所はコメントアウトで書いておいたので参考にしてください。 Sub 抽出() Worksheets("Sheet1").Range("A1:DP1000").Value = "" nen = InputBox("読み込む年度、西暦4桁(半角)読み込む月(半角)を入力") tuki = InputBox("読み込む日(半角)を入力") strUrl = "URL;http://○○○○★★★★DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("野菜").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet1").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With Const Col = 4 Dim Rw As Long With Sheets("Sheet1") Rw = .Cells(Rows.Count, Col).End(xlUp).Row '変更箇所 'D列の最終行の値を取得してからSheet1の内容をクリア Worksheets("Sheet1").Range("A1:DP1000").Clear Sheets("Sheet2").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With strUrl = "URL;http://○○○○■■■■DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("Sheet3").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet3").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With '変更箇所 'Const Col = 4 'Dim Rw As Long 'の二行を削除 '同じ関数内で同じ変数名を二回宣言することはできません。 With Sheets("Sheet3") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet4").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With End Sub

kinoko6211
質問者

補足

回答ありがとうございます。 '同じ関数内で同じ変数名を二回宣言の方は 'Const Col = 4 'Dim Rw As Long を削除してうまくいきました。 一か所目の方ですが説明不足だった為にうまく伝わらなかったのですね。ごめんなさい。 URLのデータを取り込む前に 下の処理をしてしまいます。 Const Col = 4 Dim Rw As Long With Sheets("Sheet1") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet2").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value よろしくお願いします。

関連するQ&A

  • webデータの取り込み

    winxp he sp3, excel2003 下記a1は、データが取り込めます。a2はデータが取り込めません。web接続中の表示が出ます。 対策を教えてください。よろしくお願いします。 Sub a1() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://member.rakuten-sec.co.jp/ITS/Companyfile/margin_restriction.html#9000" _ , Destination:=Range("A1")) .WebFormatting = xlWebFormattingNone .Refresh BackgroundQuery:=False End With End Sub Sub a2() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://member.rakuten-sec.co.jp/app/com_page_template.do;BV_SessionID=MbGLLn3TQcKDczyjTp3j!-1697526501?type=info&sub_type=" _ , Destination:=Range("A1")) .WebFormatting = xlWebFormattingNone .Refresh BackgroundQuery:=False End With End Sub

  • データの取込におけるドライブの指定について

    エクセルのSheet1に設置した集計ボタンによって、Sheet2にCSVを取り込む処理をさせました。 しかし、csvのあるドライブが常に固定されていないためにドライブを選択できるようにしたいのです。 下記はマクロの記録で作成した記述ですが、3行目のHをSheet1のセルA1の値で指定させる方法を教えて下さい。 Sheets2.Select Cells.Select With ActiveSheet.QueryTables.Add(Connection:="TEXT;H:\売上集計表.csv", _  Destination:=Range("A1")) .Name = "売上集計表_29" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .Refresh BackgroundQuery:=False End With 素人のため、上記マクロをApplication.Run"@"で処理させています。 他にスマートな記述がありましたらその方法も教えて下さい。

  • マクロでセル内の改行を削除する方法

    システムから抽出したCSVファイルをExcelで読み込むと同時に不要な列を削除します。 その際、セル内の改行が邪魔で行がずれてしまいます。 すでに記述したコードを生かしながら、セル内の改行を削除する方法はありますでしょうか? 以下がコードです Private Sub CommandButton1_Click() '//Sheet7のセルをクリア Sheets("Sheet7").Cells.Clear '// Sheet7のA1にissue_export.csvを読み込み With Sheets("Sheet7").QueryTables.Add(Connection:="TEXT;C:\*****.csv", Destination:=Sheets("sheet7").Range("A1")) Dim R As Range For Each R In Sheets("Sheet7").Range("A1") R.Value = Trim(Replace(R.Value, vbCr, "")) R.Value = Trim(Replace(R.Value, vbLf, "")) Next R .Name = "test" .FieldNames = True .RowNumbers = False .Refresh BackgroundQuery:=False .RefreshPeriod = 0 .RefreshOnFileOpen = False .PreserveFormatting = True .AdjustColumnWidth = True .FillAdjacentFormulas = False .RefreshStyle = xlInsertEntireRows .SavePassword = False .SaveData = True .TextFilePromptOnRefresh = False .TextFileParseType = xlDelimited .TextFileStartRow = 1 .TextFilePlatform = 65001 .TextFilePlatform = xlWindows .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = True .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileTrailingMinusNumbers = False .TextFileColumnDataTypes = Array(9, 9, 9, 9, 2, 9, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 2, 2, 9, 2, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9) .Refresh .Parent.Names(.Name).Delete .Delete End With End Sub

  • VBA マクロ処理時間の短縮について

    下記のコードを作りましたが、マクロを実行すると砂時計マークが表示されて、処理が終了するまでに30秒くらいかかります。 コードを変更して、マクロ処理時間を短縮する事はできないでしょうか? Sub A列のコピー() Dim rw2 As Long Dim rw1 As Long Dim newdate As Date With Worksheets("sheet1") rw2 = .cells(.Rows.Count, "c").End(xlUp).Row newdate = .Range("c" & rw2).value For rw1 = rw2 - 1 To 1 Step -1 If .Range("c" & rw1).value <> newdate Then Exit For Next rw1 .Range(.cells(rw1 + 1, 1), .cells(rw2, 1)).Copy Worksheets("sheet2").Range("v6").PasteSpecial xlValue If rw1 + 26 <= rw2 Then .Range(.cells(rw1 + 26, 1), .cells(rw2, 1)).Copy Worksheets("sheet2").Range("v40").PasteSpecial xlValue Application.CutCopyMode = False End If Application.CutCopyMode = False End With End Sub 各セルは、6000行くらいまで表示されています。  よろしくお願いします。

  • 自動Web取込み

    例えば、ゆうパック 都道府県別の基本運賃表「http://www.post.japanpost.jp/service/you_pack/charge/ichiran.html」の県名がハイパーリンクになっておりA3からA49にデータが入っているとします。 各県を自動的に開け、運賃「http://www.post.japanpost.jp/service/you_pack/charge/ichiran/01.html」をB53以降に取込みたいと思っております。 Sub sample() Dim h As Hyperlink For Each h In ActiveSheet.Hyperlinks h.Range.Offset(0, 1) = h.Address Next Dim i As Long Dim myAddress As String For i = 3 To 49 myAddress = Range("B" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & myAddress, Destination:=Range("B53")) .Name = "01" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "1" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Next i End Sub URL「http://www.post.japanpost.jp/service/you_pack/charge/ichiran/01.html」のコピーされたB4、B5と変化させる方法がうまくいきません。 宜しくお願い致します。

  • Excel "A1"からのデータ取込

    お世話になります。 Excel VBAを使って開発しています。 下記のコードでCSVファイルを読み込んでいます。 データが必ず"AC1"にデータが取込されています。 "A1"からデータを取り込むようにしたいときは、どうすればいいのでしょうか ActiveWorkbook.Worksheets("system-view").Activate sheets("system-view").Select Cells.Select With Selection.QueryTable .Connection = "TEXT;" & nm .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=True End With 以上、よろしくお願いします。

  • Webクエリで困っています

    以下のマクロを作りました。Webクエリで、セルA1に入力してあるURLを読み込んで表を取り込み、必要なところを選択してコピーするようにしたいのです。しかし、実行すると上から3行目まで黄色になってエラーになります。特に3行目には矢印が表示されています。いろいろ調べましたが結局分かりませんでした。宜しくお願いします。 ------------------------------------------------------ Sub クエリで取得() ' ' クエリで取得 Macro ' マクロ記録日 : 2009/7/30 ユーザー名 : charlie ' ' With ActiveSheet.QueryTables.Add(Connection:= _ Range("A1").Value _ , Destination:=Range("A2")) .Name = "resultlist?tbws=x0p01a&hd=20090716&jcd=01_2" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = False .RefreshOnFileOpen = False .BackgroundQuery = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = True .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Range("A19:Q54").Select Selection.Copy End Sub ----------------------------------------------------------

  • excel2007マクロ複数のtxtファイル取り込みについて

    excel2007マクロ複数のtxtファイル取り込みについて 複数のtxtファイルを1つのシートに1つのファイルを取り込むようなマクロを作成したいのですが上手くいきません。以下は実際の入力内容です。このマクロを実行すると下から5行目の .Refresh BackgroundQuery:=False の箇所にエラーが表示されるのですが、解決方法がわかりません。 わかりにくい質問で申し訳ありませんがお答え頂けると助かります。 宜しくお願いします。 Sub Macro4() ' ' Macro4 Macro ' ' Dim i As Integer For i = 1 To 10 With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Documents and Settings\owner\My Documents\test3\BD3.ds0_i.txt", _ Destination:=Range("$A$1")) .Name = "BD3.ds0_i" .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 = True .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = True .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Sheets("Sheet(i+1)").Select Next i End Sub

  • データのある所まで指定したい

    下記のようにB3からB80までマクロ実行できる様に設定しておきます。 例えばB38までしかデータがない場合、B39でデバッグになってしまいます。 データの入っている所まで実行できるようにお願い致します。 e = 70 ***************************************************** Dim y3 As String y3 = Range("B3").Value Dim cp3 As String cp3 = Range("P3").Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y3, Destination:=Range("E" & e + 1)) .Name = "000" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "" & cp3 .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row ******************************************************      ・      ・      ・ ****************************************************** Dim y80 As String y80 = Range("B80").Value Dim cp80 As String cp80 = Range("P80").Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y80, Destination:=Range("E" & e + 1)) .Name = "000" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "" & cp80 .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row ********************************************************

  • エクセルマクロ WEBクエリを使用。WEBのURLを一部変数にし順次データを表示させたいのですが、うまくいきません。

    WEBクエリでWEB画面上の一部の情報をエクセル上に表示させます。 その際、URLの一部を変更して、順次新しいデータを表示させたいのです。 具体的にはURLに数字があり、その数字を変数にし、エクセル上にある複数の数字を順次読ませて次々表示させるイメージです。 まず「マクロの記録」を使って、WEBクエリを読ませるベースを作りました。 例として、yahooファイナンスを使ったケースを用いますと ---------------------------------------------------- Sub Macro1() ' Macro1 Macro With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://quote.yahoo.co.jp/q?s=6758.t&d=t", Destination:=Range("$A$1")) .Name = "q?s=6758.t&d=t" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "10" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub ---------------------------------------------------- とマクロが書かれました。 この中の6758は証券コードで、別の証券コードにしたときの情報を取得したいため、6758を変数にしました。 ---------------------------------------------------- Dim SIC As Integer SIC = Range("A1").Text  ---------------------------------------------------- (注)Range("A1")に6758という数値が入っています この2文を上記ベースマクロの冒頭に加え、 ベースマクロ内の"6758"(2箇所)を"SIC"に打ちかえたら、 うまく読んでこないのです。 読ませるにはどうしたらよいでしょう。 素人なため、質問内容がわかりにくかったらご容赦ください。 わかりやすく教えて頂けるとありがたいです。 よろしくお願いします。

専門家に質問してみよう