这是我的代码。它说匹配没有找到.我共享的代码和上传的文件数据以及.
Option Explicit
Sub UpdateMasterWorksheet()
Dim xlApp As Application
Dim intFF As Integer
Dim strFileName As String
Dim vntFileName As Variant
Dim importedData() As Variant
Dim InputCSVSheet As Worksheet
Dim importedColumnG As Range
Dim masterColumnA As Range
Dim i As Long
Dim matchFound As Boolean ' Flag to track if a match is found
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Call SetASheetName
' Prompt user to select a file
Set xlApp = Application
vntFileName = xlApp.GetOpenFilename(FileFilter:="All Files (*.*), *.*", Title:="Select File to Import")
' Check if a file is selected
If VarType(vntFileName) = vbBoolean Then
MsgBox "No file selected. Import canceled.", vbExclamation
Exit Sub
End If
' Get selected file name
strFileName = vntFileName
' Read data from the file into an array
intFF = FreeFile
Open strFileName For Binary Access Read As intFF
' Read the file content into a byte array
Dim fileContentBytes() As Byte
ReDim fileContentBytes(LOF(intFF) - 1)
Get #intFF, , fileContentBytes
Close intFF
' Convert the byte array to a string
Dim fileContent As String
fileContent = StrConv(fileContentBytes, vbUnicode)
' Split the file content into an array of lines
Dim fileLines() As String
fileLines = Split(fileContent, vbCrLf)
' Resize the importedData array to match the number of lines
ReDim importedData(1 To UBound(fileLines) + 1, 1 To 1)
' Fill the importedData array with the file lines
For i = 0 To UBound(fileLines)
importedData(i + 1, 1) = fileLines(i)
Next i
' Set references to the InputCSVSheet and the respective columns
Set InputCSVSheet = ThisWorkbook.Sheets("InputData") ' Replace "InputData" with your actual sheet name
Set importedColumnG = InputCSVSheet.Range("G14:G" & InputCSVSheet.Cells(Rows.Count, "G").End(xlUp).row)
Set masterColumnA = InputCSVSheet.Range("A14:A" & InputCSVSheet.Cells(Rows.Count, "A").End(xlUp).row)
' Loop through the imported data
For i = LBound(importedData, 1) To UBound(importedData, 1)
' Extract the second value from the importedData array
Dim importedValues() As String
importedValues = Split(importedData(i, 1), ",")
If UBound(importedValues) >= 1 Then
Dim importedValue As String
importedValue = Trim(importedValues(1))
' Find matching values in column G of the InputCSVSheet
Dim matchCell As Range
Set matchCell = importedColumnG.Find(importedValue, LookIn:=xlValues, LookAt:=xlWhole)
' If a match is found, update the corresponding cell in column A with the last value from the imported file
If Not matchCell Is Nothing Then
Dim lastValue As String
lastValue = Trim(importedValues(UBound(importedValues)))
' Update the corresponding cell in column A with the last value from the imported file
masterColumnA.Cells(matchCell.row).Value = lastValue
matchFound = True ' Set the flag indicating a match is found
End If
End If
Next i
' Check if a match was found or not
If Not matchFound Then
MsgBox "No match found in the worksheet.", vbInformation
End If
' Close the opened text file workbook without saving changes
Close intFF
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
MsgBox "Data imported and master worksheet updated successfully.", vbInformation
End Sub
' シート名の設定
Public Sub SetASheetName()
Set InputCSVSheet = Worksheets("InputData") ' Replace "InputData" with your actual sheet name
End Sub
谢谢你。
“我想将InputData工作表的G列中的数据与上传的CSV文件中的第二个值(1,2,3,4,5,6)进行比较。如果有匹配项,我想将上传文件的第6列中的相应值复制到InputData的第A列。我已经包括代码和数据从文件上传。但是,当我运行代码时,它会显示'match not found'作为结果。”
上载的数据188215,% 1,% 1,错误,,指定的存储代码无效。[01][00188215] 188215,1,1,错误,,指定的代码未在产品中注册188215,1,1,错误,,产品代码未在设置的产品中注册188220,2,1,错误,,指定的商店代码无效。[01][00188220]
1条答案
按热度按时间x8diyxa71#
Sub AmazonErrorImport()'将文本导入Excel
结束子
此代码正在运行。谢谢大家