Subscript out of range error in this Excel VBA script

前端 未结 3 1529
南旧
南旧 2020-12-20 03:44

I would like to copy data from a CSV file into an Excel worksheet. There are 11 .csv files. So far I have this (it is a modified version from a previous post):



        
3条回答
  •  难免孤独
    2020-12-20 04:21

    This looks a little better than your previous version but get rid of that .Activate on that line and see if you still get that error.

    Dim sh1 As Worksheet
    set sh1 = Workbooks.Add(filenum(lngPosition) & ".csv")
    

    Creates a worksheet object. Not until you create that object do you want to start working with it. Once you have that object you can do the following:

    sh1.Range("A69").Paste
    sh1.Range("A69").Select
    

    The sh1. explicitely tells Excel which object you are saying to work with... otherwise if you start selecting other worksheets while this code is running you could wind up pasting data to the wrong place.

提交回复
热议问题