0
두 번 VlookUp을 사용하여 VBA 코드를 작성하려고하지만 런타임 오류 '1004'가 표시됩니다. 응용 프로그램 정의 또는 개체 정의 오류. 이것의 목표는 : 나는 워크 시트 "데이터"를 .csv 파일을로드하고 복사하는 VLOOKUP을 실행 Login
, Name eMail
, Card Number
, Host Login
등 : 런타임 오류 '1004'- VLOOKUP 용
Sub browseFileTest()
Dim desPathName As Variant
Dim DestCell As Range
Dim iemail As Integer
Dim PosEmail As Integer
Dim icard As Integer
Dim Poscard As Integer
Dim ihost As Integer
Dim Poshost As Integer
Dim iemailD As Integer
Dim PosEmailD As Integer
Dim icardD As Integer
Dim PoscardD As Integer
Dim ihostD As Integer
Dim PoshostD As Integer
'Import file to worksheet Data
desPathName = Application.GetOpenFilename(fileFilter:="Excel Files (*.*), *.*", Title:="Please select a file")
If desPathName = False Then
MsgBox "Stopping because you did not select a file. Reselect a destination file through the menu"
Exit Sub
Else
With Sheets("Data").QueryTables.Add(Connection:= _
"TEXT;" & desPathName, Destination:=Sheets("Data").Range("$A$1"))
.Name = "users"
.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 = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Find cells position to 1º Vlookup
For iemail = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, iemail), "Email") Then
PosEmail = iemail - 1
End If
Next
For icard = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, icard), "CardNumber") Then
Poscard = icard - 1
End If
Next
For ihost = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, ihost), "HostLogin") Then
Poshost = ihost - 1
End If
Next
Sheets("Data").Select
' Find cells position to 2ª Vlookup
For iemailD = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, iemailD), "Email") Then
PosEmailD = iemailD - 1
End If
Next
For icardD = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, icardD), "CardNumber") Then
PoscardD = icardD - 1
End If
Next
For ihostD = 1 To Cells(1, 1).End(xlToRight).Column
If InStr(Cells(1, ihostD), "HostLogin") Then
PoshostD = ihostD - 1
End If
Next
' Copy cells from Worksheet Data to WorkSheet Users
**With Sheets("Users").Range("A2", Sheets("Users").Cells(Rows.Count, "A").End(xlUp))
.Offset(, PosEmail).Formula = "=VLOOKUP(A" & .Row & ",'Data'!$A:$I,(,""" & PosEmailD & """),FALSE)"**
.Offset(, 1).Value = .Offset(, 1).Value
End With
End If
End Sub
이것이 가능하다고 생각하십니까?
SQL을 사용하여 CSV 파일을 쿼리 한 적이 있습니까? – Brad
안녕하세요 Brad, 데이터를 워크 시트 사용자에게 보낸 후 작업을 다시해야하기 때문에이 작업을 사용해야합니다. 따라서 이것이 목표를 달성하는 가장 좋은 방법이라고 생각합니다. 모든 경우에, 당신의 의견에 대해 대단히 고마워요. – Carlos
나는 그것을 데이터베이스에 넣는 것을 의미하지는 않습니다. 데이터가 CSV 파일에있는 동안 SQL을 사용하거나 워크 시트를 "테이블"로 처리하고 VBA를 통해 SQL로 쿼리 할 수 있습니다. 데이터를 좀 더 읽기 쉽게 만들 수 있습니다. – Brad