excel - Error : runtime error 9 subscript out of range : While assigning worksheet variable for data copy -


although common error , tried find relevant topics failed learn fix code. i'm trying copy cells of excel sheet particular vendor when receive updates them, avoid manual copy. see error @

set source = workbooks(strfilename).worksheets("demand request details") 

step. please help.

sub mergeinflight01()     dim j long     dim long     dim ret     dim wbk workbook     dim numofrows long     dim strfilename string     dim strvendorname string     dim source worksheet, destination worksheet     dim arra(1 15, 1 2) variant      sheets("demand request details").select      strfilename = inputbox("please enter source file path take data from")     strvendorname = inputbox("please enter vendor name xyz")      if fileinuse(strfilename)         ' open work-book if not opened         set wkbsource = workbooks.open(strfilename)     end if      'error here     set source = workbooks(strfilename).worksheets("demand request details")      numofrows = sheet4.cells(rows.count, 1).end(xlup).row + 5     strfilename = inputbox("please enter destination file path take data from")      if fileinuse(strfilename)         ' open work-book if not opened         set wkbsource = workbooks.open(strfilename)     end if       set destination = workbooks(strfilename).worksheets("demand request details")      = 1 numofrows        if (source.cells(i, 22).value = "delivery") , (source.cells(i, 14).value = strvendorname)          j = 1 numofrows             if (source.cells(i, 1).value = destination.cells(j, 1).value) , (source.cells(i, 6).value = destination.cells(j, 6).value)              source.cells(i, 20).value = destination.cells(j, 20).value              source.cells(i, 38).value = destination.cells(j, 38).value              source.cells(i, 39).value = destination.cells(j, 39).value              source.cells(i, 40).value = destination.cells(j, 40).value              source.cells(i, 41).value = destination.cells(j, 41).value              source.cells(i, 42).value = destination.cells(j, 42).value             elseif (source.cells(i, 1).value = destination.cells(j, 1).value) , (source.cells(i, 6).value <> destination.cells(j, 6).value)              source.cells(i, 1).interior.colorindex = 3             end if          next j        end if     next  end sub  public function fileinuse(sfilename) boolean     on error resume next     open sfilename binary access read lock read #1     close #1     fileinuse = iif(err.number > 0, true, false)     on error goto 0 end function 

also instead of using input box user enter file name , path, use application.getopenfilename

also need extract file name full path can work open workbook.

is trying?

sub mergeinflight01()     dim wkbsource workbook     dim filetoopen     dim wbname string      '~~> let user select file     filetoopen = application.getopenfilename("excel files (*.xls*), *.xls*")      if filetoopen <> false         wbname = getfilenamefrompath(filetoopen)          if isworkbookopen(wbname)             set wkbsource = workbooks(wbname).worksheets("demand request details")         else             set wkbsource = workbooks.open(filetoopen)         end if          '         '~~> rest of code         '      end if end sub  '~~> check if workbook open function isworkbookopen(filename)     dim ff long, errno long      on error resume next     ff = freefile()     open filename input lock read #ff     close ff     errno = err     on error goto 0      select case errno     case 0:    isworkbookopen = false     case 70:   isworkbookopen = true     case else: error errno     end select end function  '~~> filename path public function getfilenamefrompath(byval strpath string) string     if right$(strpath, 1) <> "\" , len(strpath) > 0         getfilenamefrompath = _         getfilenamefrompath(left$(strpath, len(strpath) - 1)) + right$(strpath, 1)     end if end function 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -