vba - Inserting, searching, copying, pasting across 2 spreadsheets in Excel -


i thought i'd able figure out based off analysis of similar code had written else, code isn't forte.

diving vba without guidance has proved daunting me proceed.

forgive terminology if use wrong language, i'm happy learn , corrected.

this shouldn't difficult knows they're doing, don't @ all.

i'm trying create macro enabled workbook following:

  • open "data.csv" folder called "data" in same directory macro. we'll call workbook - wba.

  • insert column on wba after column c titled "group image name." column d want data end up.

  • open "groups.csv" folder called "groups" in same directory macro. we'll call workbook b - wkb.

(this next part needs loop starts @ c1 on wba , proceeds down column until end of spreadsheet)

  • copy value selected cell in column c on wba
  • search column c on wbb copied value. when found, move selection on corresponding cell in column a. (if c2, a2)
  • copy contents of column value wbb column d cell on wba corresponds original starting point on wba.

basically in plain language: search column c contents wba on column c of wbb. when found, move column of same cell # on wbb, copy contents, , paste cell # of column d on wba corresponds cell # of starting point column c.

i hope that's clear; please feel free ask more details if necessary. anyone's in advance!

here terrible code i'm working @ moment:

    sub opendataaddgroupimagetitlecolumn()  chdir "c:\[realcodehascorrectfilepath..]\desktop\test" workbooks.open filename:="c:\[realcodehascorrectfilepath..]\desktop\test\data.csv" columns("d:d").select selection.insert shift:=xltoright, copyorigin:=xlformatfromleftorabove cells.select cells.entirecolumn.autofit range("d1").select activecell.formular1c1 = "group image title" range("c2").select 'variables storing row, column location, , value here dim groupname string dim rowlocationx long dim columnlocationz integer groupname = activecell.value rowlocationx = activecell.row columnlocationz = activecell.column workbooks.open filename:="c:\[realcodehascorrectfilepath..]\desktop\groups.csv" columns("c:c").select selection.find(what:="groupname", after:=activecell, lookin:=xlformulas, lookat _     :=xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, matchcase:= _     false, searchformat:=false).activate end sub 

the current snag can't figure out after googling this: possible search variable groupname using find/replace feature? or should taking different approach using involving copying clipboard?

the first problems see are:

1) don't put functions in middle of sub routines. if want function, put on own such:

 sub mysub()       'do stuff       x = myfunction()       debug.print x  end sub   function myfunction() string       'do stuff       myfunction = "test"  end function 

2) code have provided won't compile reason 1 , because have ended sub "end function" instead of end sub. try running code , error messages. can research error message , try fix it. further, if don't error messages, can step through code make sure working way intend to.

3) first line isn't going work. need use entire path of file, if in same folder file have open. there ways directory of file have open (google surely show many of them), , append filename directory of file have open.

4) want loop, haven't put in looping structions. google "excel vba loop through cells" , can find many examples use.

5) think biggest issue having overwhelmed because trying @ once. suggest solving 1 problem @ time , putting code together. instance, want open 2 files. start writing code open 1 file. try value out of file open workbook. open file. data out of file. test looping through cells in current workbook , checking desired criteria , pasting results if match. combine of these things coherent code.

nobody writes efficient code first time try, if end long code isn't efficient, you'll learn lot , have something. experienced programmers can stuck or write code doesn't work first or tenth time. if stuck, ask specific question specific error message or specific issue can't resolve.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -