0

For my work, I'm trying to automate some analysis into a standard Excel file to do the work for me. I thought I had assembled the right code to do so.

What I'm looking for is just a sheet where I can put the source file export from my financial programm en turn that into a more complex version with additional formulas to add some fields and the add different pivots to it.

I've have written code to basicly clean wipe the sheet where I want the sourcefile to be copied to, then I want Excel to copy all the data from the sourcefile sheet to the new sheet and then add the formulas. I'm already getting an error on the first part.

I used the following code:

    enter code here
Sub TransferSourcefile()
Dim EndRow, FirstRow

    'TransferSourcefile
   Sheets("Macro sortering").Select
   Range("A4").Select
   If Sheets("Macro sortering").Range("A4").Value > 0 Then
       Selection.CurrentRegion.Select
       EndRow = Selection.CurrentRegion.Rows.Count
       Sheets("Macro sortering").Range("A4:AG" & EndRow).Select
       Selection.ClearContents
   End If
   Sheets("Bronbestand").Select
    
   Sheets("Bronbestand").Select
   Range("A1").Select
   Selection.End(xlDown).Select
   EndRow = ActiveCell.Row
   Range("A1:AE" & EndRow).Select

   Selection.Copy
   Sheets("Macro sortering").Select
   Sheets("Macro sortering").Range("A4").Select
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False
   Application.CutCopyMode = False

End Sub



enter code here

Can you guys help me out?

1 Answer 1

1

Run this and press Ctrl + C when the error popup appears to copy the error text and paste it here. It will tell you what line is causing the error.

Sub TransferSourcefile()
      Dim EndRow, FirstRow

          'TransferSourcefile
10       On Error GoTo TransferSourcefile_Error

20       Sheets("Macro sortering").Select
30       Range("A4").Select
40       If Sheets("Macro sortering").Range("A4").Value > 0 Then
50           Selection.CurrentRegion.Select
60           EndRow = Selection.CurrentRegion.Rows.Count
70           Sheets("Macro sortering").Range("A4:AG" & EndRow).Select
80           Selection.ClearContents
90       End If
100      Sheets("Bronbestand").Select
          
110      Sheets("Bronbestand").Select
120      Range("A1").Select
130      Selection.End(xlDown).Select
140      EndRow = ActiveCell.Row
150      Range("A1:AE" & EndRow).Select

160      Selection.Copy
170      Sheets("Macro sortering").Select
180      Sheets("Macro sortering").Range("A4").Select
190      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
             :=False, Transpose:=False
200      Application.CutCopyMode = False

210      On Error GoTo 0
220      Exit Sub

TransferSourcefile_Error:

230       MsgBox "Line:" & Erl & vbCrLf & "Error " & Err.Number & " (" & Err.Description & ") in procedure TransferSourcefile"

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

--------------------------- Microsoft Excel --------------------------- Line:100 Error 9 (Het subscript valt buiten het bereik) in procedure TransferSourcefile --------------------------- OK ---------------------------
Does a sheet named exactly "Bronbestand" exist?
Yes it does, I recopied your data into my vba and it works now.... maybe I had too many spaces somewhere?
Additionally: If I want to add a new row to the copied data with a formula, how do I make that row go to the exact ending of the copied data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.