2015-01-15 2 views
1

내가 줄 뒤에 실수를하고있는 중이 야 알고에 침입 : 는 If intChoice <> 0 Then
사람은 내가 그것을 수습하는 데 도움이 수 있습니까?가져 오기 txt 파일하고 세포

Private Sub CommandButton1_Click() 
    Dim intChoice As Integer 
    'Select the start folder 
    Application.FileDialog(msoFileDialogOpen).InitialFileName = "I:\Dunnings" 
    'make the file dialog visible to the user 
    intChoice = Application.FileDialog(msoFileDialogOpen).Show 
    'determine what choice the user made 

    If intChoice <> 0 Then 
     Workbooks.OpenText.Filename:= intChoice, Origin:=xlMSDOS, StartRow:=23, DataType:=xlFixedWidth, FieldInfo:= _ 
      Array(Array(0, 1), Array(6, 2), Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _ 
      Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), TrailingMinusNumbers:= _ 
      True 
     NewPath = Mid(ThisWorkbook.FullName, 1, _ 
     Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name)) & "\" & _ 
      "Dunnings - " & Format(Date, "dd-mm-yyyy") & ".xlsm" 
     ThisWorkbook.SaveAs (NewPath) 
    End If 
End Sub 
+0

어떤 오류와 줄을 알려주십시오. – L42

+0

다음 라인에 있습니다. intChoice <> 0 ... Cant가 해당 변수를 intChoice로 사용합니다 ... – Isu

답변

0

IntChoice은 정수입니다.
파일 이름 인수는 문자열을 필요로합니다. 이런 식으로 뭔가 :

With Application.FileDialog(msoFileDialogOpen) 
    .InitialFileName = "I:\Dunnings" 
    .Filters.Clear 
    .title = "Your Title" 
    If Not .Show Then 
     MsgBox "No file selected.": Exit Sub 
    End If 
    Workbooks.OpenText .SelectedItems(1) 'and the rest of your code. 
End With 
+0

그래, 특정 위치의 파일을 호출하는 것이 아니라 내가 선택한 파일을 호출해야합니다. 윗부분 .. 그게 내가 투쟁하는 곳이다 ... 나는 VBA에 능숙하지 않다. ... 이것에 대한 주요한 문제이다 .... – Isu

+0

점은 실수로 오타가된다 .... – Isu

+0

제안은 나를 위해 완벽하게 작동했다. 내가 원하는 결과를 얻었다. 정말 고마워. 미래에 누군가가 필요할 경우 최종 결과입니다 ... – Isu

1
Private Sub CommandButton1_Click() 
    With Application.FileDialog(msoFileDialogOpen) 
     .InitialFileName = "I:\" 
     .Filters.Clear 
     .Title = "Your Title" 
     If Not .Show Then 
      MsgBox "No file selected.": Exit Sub 
     End If 
     Workbooks.OpenText .SelectedItems(1), Origin:=xlMSDOS, StartRow:=23, _ 
       DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(6, 2), _ 
       Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _ 
       Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), _ 
       TrailingMinusNumbers:=True 
     NewPath = Mid(ThisWorkbook.FullName, 1, _ 
      Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name)) & "\" & _ 
      "Dunnings - " & Format(Date, "dd-mm-yyyy") & ".xlsm" 
     ThisWorkbook.SaveAs (NewPath) 
    End With 
End Sub 

기본적으로 코드의 나머지 부분은 RecordMacro를 사용하여 기록 할 수 다음 VB 코드에 결과 코드를 복사합니다.

+0

새 파일에서이 동일한 기술을 시도하면 오류가 발생합니다 ... "Dunnings -"& Format (날짜, "dd-mm-yyyy") "New"= ") & ".xlsm" ThisWorkbook.SaveAs (NewPath) 이 질문과 동일한 오류는 http://stackoverflow.com/questions/17173898/how-to-do-a-save-as-in-vba- 코드 절약형 my-current-excel- workbook-with-datesta – Isu