質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Q&A

解決済

1回答

458閲覧

VBAで転記をしたいのですが、表示形式が任意の形式にならない

kimibun0509

総合スコア1

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

0グッド

0クリップ

投稿2022/04/23 23:30

お知恵をお貸しくださいませ。。

■前提・実現したいこと
・csvからVBAで転記をしたい
■発生している問題・エラーメッセージ
・特定のセルのみ「ユーザー定義」に変更される
(転記作業は複数Bookにわたり、そのうち1つの転記元の列がユーザー定義になっている)
・転記元CSVを標準に戻しているつもりだが、転記先のExcel内ではG4セルのみがユーザー定義で表記され
整数で表示したいが日付形式で表示されてしまう
■該当のソースコード
Sub 不支給通知リストに転記_4()
'”C~csv”の” ”の中身をデスクトップのCSV名に書き換える
Workbooks.Open Filename:="C:xxxxxx.csv", ReadOnly:=True

Dim ws, ws1, ws2 As Worksheet
Dim ListLastRow, ListLastRow1, ListLastRow2 As Long
Dim OutputRow1, OutputRow2 As Long
Dim i, x, j, a, b As Long

Set ws = ActiveWorkbook.Worksheets("受付")
Set ws1 = ThisWorkbook.Worksheets("オンライン")
Set ws2 = ThisWorkbook.Worksheets("郵送")

'最終行とる
ListLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
ListLastRow1 = ws1.Cells(Rows.Count, "B").End(xlUp).Row
ListLastRow2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row

OutputRow1 = ListLastRow1 + 1
OutputRow2 = ListLastRow2 + 1

ws.Range("PJ:PJ").NumberFormatLocal = "G/標準"

For i = 2 To ListLastRow
If ws.Cells(i, 1) <> "" Then
If ws.Cells(i, 4) = "確認審査完了" Then
If ws.Cells(i, 5) = "郵送" Then
ws2.Cells(OutputRow2, 2) = ws.Cells(i, 3)
ws2.Cells(OutputRow2, 3) = ws.Cells(i, 8)
ws2.Cells(OutputRow2, 4) = ws.Cells(i, 9)
ws2.Cells(OutputRow2, 5) = ws.Cells(i, 19)
ws2.Cells(OutputRow2, 6) = ws.Cells(i, 112)
ws2.Cells(OutputRow2, 7) = ws.Cells(i, 426)
ws2.Cells(OutputRow2, 8) = ws.Cells(i, 427)
ws2.Cells(OutputRow2, 9) = ws.Cells(i, 428)
ws2.Cells(OutputRow2, 10) = ws.Cells(i, 5)
ws2.Cells(OutputRow2, 11) = ws.Cells(i, 434)
OutputRow2 = OutputRow2 + 1

Else ws1.Cells(OutputRow1, 2) = ws.Cells(i, 3) ws1.Cells(OutputRow1, 3) = ws.Cells(i, 8) ws1.Cells(OutputRow1, 4) = ws.Cells(i, 9) ws1.Cells(OutputRow1, 5) = ws.Cells(i, 19) ws1.Cells(OutputRow1, 6) = ws.Cells(i, 112) ws1.Cells(OutputRow1, 7) = ws.Cells(i, 426) ws1.Cells(OutputRow1, 8) = ws.Cells(i, 427) ws1.Cells(OutputRow1, 9) = ws.Cells(i, 428) OutputRow1 = OutputRow1 + 1 End If End If

End If
Next i

ListLastRow1 = ws1.Cells(Rows.Count, "B").End(xlUp).Row
ListLastRow2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row
ws1.Range("G:G").NumberFormatLocal = "G/標準"
ws2.Range("G:G").NumberFormatLocal = "G/標準"

For x = 4 To ListLastRow1
ws1.Cells(x, 7) = Replace(ws1.Cells(x, 7), "44867", "11-2")
ws1.Cells(x, 7) = Replace(ws1.Cells(x, 7), "44610", "18-2")
ws1.Cells(x, 7) = Replace(ws1.Cells(x, 7), "44576", "15-1")
ws1.Cells(x, 7) = Replace(ws1.Cells(x, 7), "44652", "4-1")
ws1.Cells(x, 7) = Replace(ws1.Cells(x, 7), "2022/4/1", "4-1")

Next x

For j = 4 To ListLastRow2
ws2.Cells(j, 7) = Replace(ws2.Cells(j, 7), "44867", "11-2")
ws2.Cells(j, 7) = Replace(ws2.Cells(j, 7), "44610", "18-2")
ws2.Cells(j, 7) = Replace(ws2.Cells(j, 7), "44576", "15-1")
ws2.Cells(x, 7) = Replace(ws2.Cells(x, 7), "44652", "4-1")
ws2.Cells(x, 7) = Replace(ws2.Cells(x, 7), "2022/4/1", "4-1")
Next j

ListLastRow1 = ws1.Cells(Rows.Count, "B").End(xlUp).Row

For a = 2 To ListLastRow
If ws.Cells(a, 7) = "2022/4/1" Or ws.Cells(a, 7) = "44652" Then
ws.Cells(a, 7) = ""
End If
Next

'CSV閉じる
ActiveWorkbook.Close SaveChanges:=False

End Sub

■自分で調べたことや試したこと
・標準におきかえるコードや置換コードは入れてみています
∟ただしG4セルのみ置換が反映されません

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tatsu99

2022/04/24 01:55

CSVファイルの内容を提示していただけませんでしょうか。
kimibun0509

2022/04/24 06:13

申し訳ありません。内容の提示が難しいため他回答いただいた方の方法で試したいと思います。
guest

回答1

0

ベストアンサー

CSVファイルをエクセルで読み込む際の仕様ですね。

CSVファイルをテキストファイルとして読み込むのが順当な解決策かと。

VBA

1Option Explicit 2 3Sub 不支給通知リストに転記_4() 4 Dim varFileName As Variant 5 Dim ws1 As Worksheet, ws2 As Worksheet 6 Dim ListLastRow1 As Long, ListLastRow2 As Long 7 Dim OutputRow1 As Long, OutputRow2 As Long 8 Dim intFree As Integer, strRec As String, strSplit() As String 9 varFileName = Application.GetOpenFilename("CSVファイル(*.csv),*.csv") 10 If VarType(varFileName) <> vbString Then Exit Sub 11 intFree = FreeFile '空番号を取得 12 Open varFileName For Input As #intFree 'CSVファィルをオープン 13 Set ws1 = ThisWorkbook.Worksheets("オンライン") 14 Set ws2 = ThisWorkbook.Worksheets("郵送") 15 '最終行とる 16 ListLastRow1 = ws1.Cells(Rows.Count, "B").End(xlUp).Row 17 ListLastRow2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row 18 OutputRow1 = ListLastRow1 + 1 19 OutputRow2 = ListLastRow2 + 1 20 ws1.Range("G:G").NumberFormatLocal = "@" 21 ws2.Range("G:G").NumberFormatLocal = "@" 22 Do Until EOF(intFree) 23 Line Input #intFree, strRec '1行読み込み 24 strSplit = Split(strRec, ",") 'カンマ区切りで配列へ 25 If strSplit(0) <> "" Then 26 If strSplit(3) = "確認審査完了" Then 27 If strSplit(4) = "郵送" Then 28 ws2.Cells(OutputRow2, "B") = strSplit(2) 29 ws2.Cells(OutputRow2, "C") = strSplit(7) 30 ws2.Cells(OutputRow2, "D") = strSplit(8) 31 ws2.Cells(OutputRow2, "E") = strSplit(18) 32 ws2.Cells(OutputRow2, "F") = strSplit(111) 33 ws2.Cells(OutputRow2, "G") = strSplit(425) 34 ws2.Cells(OutputRow2, "H") = strSplit(426) 35 ws2.Cells(OutputRow2, "I") = strSplit(427) 36 ws2.Cells(OutputRow2, "J") = strSplit(4) 37 ws2.Cells(OutputRow2, "K") = strSplit(433) 38 OutputRow2 = OutputRow2 + 1 39 Else 40 ws1.Cells(OutputRow1, "B") = strSplit(2) 41 ws1.Cells(OutputRow1, "C") = strSplit(7) 42 ws1.Cells(OutputRow1, "D") = strSplit(8) 43 ws1.Cells(OutputRow1, "E") = strSplit(18) 44 ws1.Cells(OutputRow1, "F") = strSplit(111) 45 ws1.Cells(OutputRow1, "G") = strSplit(425) 46 ws1.Cells(OutputRow1, "H") = strSplit(426) 47 ws1.Cells(OutputRow1, "I") = strSplit(427) 48 OutputRow1 = OutputRow1 + 1 49 End If 50 End If 51 End If 52 Loop 53 'CSV閉じる 54 Close #intFree 55End Sub 56

投稿2022/04/24 04:11

iruyas

総合スコア1067

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kimibun0509

2022/04/24 06:12

ありがとうございます! csvに読込形式がある認識がなく目からうろこでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問