dポイントプレゼントキャンペーン実施中!

こんばんは。
excel VBAでおしえてください。
シート1(data),シート2(名簿)があります。
シート2(名簿)に列Bに社員番号、列Cに氏名があります。
シート1のB列に社員番号のみが入っています。
シート1のB列の社員番号をシート2の名簿のB列の横にある氏名に変換したいのですがreplaceがいいかselect case がいいかわかりません。ご教示よろしくお願い致します。

A 回答 (4件)

No2です。

先ほどのマクロに誤りがありました。前回のは破棄してください。
--------------------------------
Option Explicit
Public Sub 社員番号変換()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim dicT As Object
Dim maxrow1 As Long
Dim maxrow2 As Long
Dim okctr As Long
Dim ngctr As Long
Dim row As Long
Dim key As String
Set sh1 = Worksheets("data")
Set sh2 = Worksheets("名簿")
Set dicT = CreateObject("Scripting.Dictionary")
maxrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).row
maxrow2 = sh2.Cells(Rows.Count, "B").End(xlUp).row
Application.ScreenUpdating = False

For row = 2 To maxrow2
key = sh2.Cells(row, "B").Value
dicT(key) = sh2.Cells(row, "C").Value
Next
okctr = 0
ngctr = 0
For row = 2 To maxrow1
key = sh1.Cells(row, "B").Value
If dicT.exists(key) = True Then
sh1.Cells(row, "B").Value = dicT(key)
okctr = okctr + 1
Else
ngctr = ngctr + 1
End If
Next
Application.ScreenUpdating = True
MsgBox ("処理件数=" & okctr & " 未処理件数=" & ngctr)
End Sub
----------------------------------
    • good
    • 0

Match関数を使ってみました。



'//標準モジュール
Sub FindNames()
 Dim sh1 As Worksheet: Set sh1 = Worksheets("名簿")
 Dim sh2 As Worksheet: Set sh2 = Worksheets("Data")
 Dim i As Variant
 Dim c As Variant
 Dim rngNumList As Range
 With sh1
  '必ず、頭出しは、B1にしてください。
  Set rngNumList = .Range("B1", .Cells(Rows.Count, 2).End(xlUp))
 End With
 Application.ScreenUpdating = False
 With sh2
  'データがB2から始まっているなら、B2から
  For Each c In .Range("B2", .Cells(Rows.Count, 2).End(xlUp))
   If c.Value <> "" Then
    i = Application.Match(c.Value, rngNumList, False)
    If IsNumeric(i) Then
     c.Value = sh1.Cells(i, 3).Value
    End If
   End If
  Next
 End With
 Application.ScreenUpdating = True
End Sub
    • good
    • 0
この回答へのお礼

WindFallerさん、返信ありがとうございました。自分のPCでは少し時間がかかるみたいです。勉強になりました。感謝致します。

お礼日時:2017/10/12 21:00

No1です。

以下のマクロを標準モジュールへ登録してください。
---------------------------------------
Option Explicit
Public Sub 社員番号変換()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim dicT As Object
Dim maxrow1 As Long
Dim maxrow2 As Long
Dim okctr As Long
Dim ngctr As Long
Dim row As Long
Dim key As String
Set sh1 = Worksheets("data")
Set sh2 = Worksheets("名簿")
Set dicT = CreateObject("Scripting.Dictionary")
maxrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).row
maxrow2 = sh2.Cells(Rows.Count, "B").End(xlUp).row
Application.ScreenUpdating = False

For row = 2 To maxrow2
key = sh2.Cells(row, "B").Value
dicT(key) = sh2.Cells(row, "C").Value
Next
okctr = 0
ngctr = 0
For row = 2 To maxrow1
key = sh1.Cells(row, "B").Value
If dicT.exists(key) = True Then
sh1.Cells(row, "B").Value = dicT(key)
okctr = okctr + 1
Else
ngctr = ngctr + 1
End If
dicT(key) = sh2.Cells(row, "C").Value
Next
Application.ScreenUpdating = True
MsgBox ("処理件数=" & okctr & " 未処理件数=" & ngctr)
End Sub
-----------------------------------------
    • good
    • 0
この回答へのお礼

tatsu99さん、コードありがとうございました。無事作成することができました。感謝致します。

お礼日時:2017/10/12 21:02

補足要求です。


1)シート1、シート2共に1行目は見出しでしょうか。
2)replaceもselect caseも使用しませんが、それでも良いですか。
3)シート1の社員番号を直接変換してしまうと、リラン(マクロの再実行)ができなくなります。
その為、他の列(例えばC列)に、名前を出力したほうが良いと、考えますが、いかがでしょうか。
どうしても、B列を直接変換したい場合は、事前シート1のバックアップをとることをお勧めします。
    • good
    • 0
この回答へのお礼

tatsu99さん、返信ありがとうございます。
(1)シート1、シート2とも1行目は見出しです。
(2)方法は特にこだわりはありません。できれば高速処理できると助かり
   ます。
(3)元ファイルはバックアップしているので、直接変換できたほうがいい
   です。
申し訳ありませんがご教示よろしくお願い致します。

お礼日時:2017/10/12 07:02

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!