이전에 사용자가 선택한 파일 이름을 찾기 위해 A 열에서 find를 사용하는 Mac Office 2011 용 vba 매크로 스크립트를 설계하려고합니다.찾기 루프가 올바르게 작동하지 않습니다.
사용자가 .csv 파일을 선택하면 매크로가 .csv 확장명없이 파일 이름을 찾기 위해 A 열을 찾습니다. 일단 발견되면, 하나의 열 (B 열)으로 오프셋하고 csv 정보를 가져옵니다.
내가 현재 갖고있는 것은 무엇입니까? 나는 내가 여기서 잘못하고있는 것을 알아낼 수 없다.
csv는 가져 오지만 셀 바로 옆에서 매크로를 실행하기 전에 활성 상태였습니다. 이것이 Find가 작동하지 않는 이유입니다.
모든 도움을 주시면 감사하겠습니다.
Sub CSVauto()
'
' CSVauto Macro
'
' Keyboard Shortcut: Option+Cmd+x
'
' Declaring and setting variables for choosing CSV to import
Dim csvFileName As Variant
''Prompt window to choose csv file
csvFileName = Application.GetOpenFilename(FileFilter:="")
If csvFileName = False Then Exit Sub
'Setting a variable to find Experimental form name in Data Summary
Dim whatToFind As String 'Declaring that variable
If Right(csvFileName, 4) = ".csv" Then
whatToFind = Replace(csvFileName, ".csv", "")
Else
MsgBox "Selected File Not .csv)"
End If
'Looping through A column to find csvFileName without .csv extension
Set cell = Range("A:A").Find(What:=whatToFind, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
If Not cell Is Nothing Then
cell.Select
End If
'Speeding macro up by making it work in background
Sheets("DataSummary").DisplayPageBreaks = False
Application.DisplayAlerts = False
Dim MyRange As Range
Set MyRange = ActiveCell.Offset(0, 1)
MyRange.Select
'xlOverwriteCells
On Error Resume Next
'Formatting for CSV and input
With MyRange.Parent.QueryTables.Add(Connection:="TEXT;" & csvFileName, Destination:=MyRange)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlMacintosh
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.Refresh BackgroundQuery:=False
.UseListObject = False
End With
'Formatting DataSummary sheet to fit "requirements" :)
Cells.Replace What:=">=", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False
Cells.Replace What:="C121", Replacement:="C2", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False
Cells.Replace What:="P1211", Replacement:="P21", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False
Cells.Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
End With
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
End With
Range("A4").Select
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
' Insert your code here.
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
'undoing everything working in background
Sheets("DataSummary").DisplayPageBreaks = True
Application.ScreenUpdating = True
End Sub
내가 파일 이름을 수동으로 입력됩니다 뭐하는 거지 먼저 열 A에 나는 GetOpenFilename에가 가지고있는 (.CSV 제외) csv를 선택하는 윈도우. 그런 다음 해당 파일 이름 (.csv 제외)에 대해 열 A를 검색하려고합니다. 찾은 후에는 한 열씩 상쇄하고 .csv 파일에 정보를 가져옵니다. If/else 문을 꺼내어도 찾기가 여전히 올바르게 작동하지 않습니다. – JJcarter
문제를 발견했지만 해결 방법을 모릅니다. 열 A를 통해 반복하면 testname.csv 대신 전체 파일 이름 경로를 찾습니다. 그것은 매킨토시 HD를 찾습니다 : 사용자 : joshuacarter : 데스크탑 : 경로 : to : filname. 파일 이름 만 찾고 전체 경로가 아닌 파일을 찾는 방법에 대한 아이디어가 있습니까? – JJcarter
@JJcarter는 위의 편집을 확인합니다. 그 코드에 나를 위해 일하고 당신을 위해 일해야한다 – Andy