• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA - Making a Range and Copying contents over

dnegrotto

New Member
So I am trying to do some code, but i am quite the novice. I have a masterfile which has dates (1/1/2012, 12/1/2011, etc) that correspond with months we do a specific task within these excel files call position exposures. In each 'position exposure' excel workbook I have a work sheet called "position exposure database". That excel sheet simply has month end dates in row 1 and in row 2 will have a complete or not complete depending on if it was completed. What I want this VBA code to do, is search for a series of dates in each of these excel workbooks in that specific worksheet, find that date, then offset by one row down, copy the contents (will be 'complete' or blank) and paste special the value inside my master file in a tab called monthlies in a cell which corresponds to the column which has that specific date in that column. At the end of all of this I want the code to go through 15 different excel work books and tell me whether or not they were completed in specific months. I started just trying to understand code so this may help in terms of what I am doing. This code below finds 1/1/2012, offsets the content by one row and than expands the finding for 3 columns (I was just tinkering with this column aspect) it then proceeds to paste it into a excel book called test run in a specific cell (which i'd want to be dynamic). Underneath this one piece of code i also will place another one I did which was opening the specific file name and closing it at the end.


Sub Find_First()

Dim rng As Range

Dim destsheet As Worksheet


Windows("b.xlsx").Activate

Set rng = Worksheets("position exposure database").Range("a1:zz1").Find(What:="1/1/2012", LookAt:=xlWhole, _

LookIn:=xlValues)

If rng Is Nothing Then

MsgBox "Data not found"

Exit Sub

Else ' the above tells you to set a range in the position exposure database tab as "x" and find a specific date if that

'is nothing than it will say not found


rng.offset(1, 0).Resize(1, 3).Copy 'this finds my variable offsets one row down and currently resizes it to 3 rows


Windows("test run.xlsm").Activate 'this is me activating the workbook I need

Set destsheet = Worksheets("monthlies") ' this is telling it this is the destination sheet

Range("d6").Select

Selection.PasteSpecial Paste:=xlPasteValues, _

Operation:=xlNone, _

SkipBlanks:=False, _

Transpose:=False


End If


End Sub


___________OTHER CODE BELOW


Option Explicit


Sub Find_First()

Dim FindString As Date

Dim rng As Range

Dim test As Worksheet

Dim monthlies As Worksheet

Dim sourcesheet As Worksheet

Dim destsheet As Worksheet


'above are the things which i defined not sure what this does


Workbooks.Open Filename:="Z:APM COMMON FOLDERSMANAGER'S MATERIALSPosition ExposureEmoryb.xlsx"

Set sourcesheet = Worksheets("position exposure database")

FindString = "1/1/2012" 'you can also put ranges with # signs as well

If Trim(FindString) <> "" Then

With Sheets("position exposure database").Range("A1:z1")

Set rng = .Find(What:=FindString, _

After:=.Cells(.Cells.Count), _

LookIn:=xlValues, _

LookAt:=xlWhole, _

SearchOrder:=xlByRows, _

SearchDirection:=xlNext, _

MatchCase:=False)

If Not rng Is Nothing Then

Application.Goto rng, True

Else

MsgBox "Nothing found"

End If

End With

End If


'above essentially is find the date in a specific range and then selecting that cell


Selection.offset(1, 0).Select

'above takes the selected cell and now offets by 1 row down so I can get the value


Selection.Copy

'i think this is self explantory


Windows("test run.xlsm").Activate

Set destsheet = Worksheets("monthlies")

Range("d6").Select

Selection.PasteSpecial Paste:=xlPasteValues, _

Operation:=xlNone, _

SkipBlanks:=False, _

Transpose:=False


'above is taking the value that is below the date and inputting it into the cell


Windows("b.xlsx").Activate

ActiveWorkbook.Close False
 
two things which I don't understand, how to create my "find" variable as dynamic (as in I want it to be a range of dates not just 1/1/2012. This range would be found in the master file. Also I don't understand how to make the VBA "loop" or do this code a bunch of times with different dates etc- all round i am just plain confused!
 
Hi ,


Is it possible you can upload your workbook ? Looking at code without the underlying framework makes it that much more time-consuming.


Narayan
 
narayan - how do you upload the files>? is there anyway I can email them to you, I cannot figure out how to upload ?
 
https://docs.google.com/open?id=0BwKyVQi8QwbPQ2xMOUZ1RE1UeHVSWktUS0t6NUpLZw


https://docs.google.com/open?id=0BwKyVQi8QwbPby0tVDM2RGVSeDIzSThzWTFsTzlnUQ
 
https://docs.google.com/open?id=0BwKyVQi8QwbPby0tVDM2RGVSeDIzSThzWTFsTzlnUQ


this should be one link to the document
 
apologies - i revised the test run excel sheet so its much more basic, the rest of the tabs had no real relevance to what I wanted to do.


here is the link - https://docs.google.com/open?id=0BwKyVQi8QwbPMGVjQ3liUGpUbGFGR0RpSDFRamtCdw
 
apologies for so many threads - i also revamped the "b" position exposure just to take some info out of it - so this is the link to the "b" excel sheet


https://docs.google.com/open?id=0BwKyVQi8QwbPUjQzamNZbGRTMkMyU1k1T1czakR4dw


For ease of this thread here is the test run excel sheet which is really the "master" file i speak of - https://docs.google.com/open?id=0BwKyVQi8QwbPMGVjQ3liUGpUbGFGR0RpSDFRamtCdw


I am trying to get the "completed" cells which are in row 2 on the position exposure database excel sheet in the "b" file over to the corresponding "month column" in the master sheet.
 
Hi ,


So the two workbooks which are to be used are ( from your latest post on this ) :


1. https://docs.google.com/open?id=0BwKyVQi8QwbPUjQzamNZbGRTMkMyU1k1T1czakR4dw


2. https://docs.google.com/open?id=0BwKyVQi8QwbPMGVjQ3liUGpUbGFGR0RpSDFRamtCdw


Narayan
 
correct


1st link - called "b" has a tab called position exposure database. There is only two things going on in this worksheet. Dates are in row 1 and either "blank" or "complete is in row 2 corresponding to the date (there is a complete if that month was done and its blank if it still hasn't been done yet.


I am trying to get the results for each one (complete vs "blank") and paste the corresponding results in the "test run" excel sheet in the corresponding column which has the dates listed in there.


I have 15 "b" excel sheets (different reports) but all have that same position exposure database worksheet inside,


so the overall goal is to take all those workbooks look at the dates and paste the value that is one row below that date in the corresponding "test run" sheet.
 
Hi ,


Taking the two workbooks :


1. B has cells L2 and M2 having "Complete" in them.


2. The dates in L1 and M1 are 1/1/2012 and 12/1/2011 ( mm/dd/yyyy ).


3. "Test Run" has the above dates in cells O5 and P5


4. Should the text "Complete" be filled in , in all the cells of column O and P , from O6 through P31 ?


Narayan
 
Yes L2 and M2 have complete in them, however A2-ZZ2 have this formula (=IF('Position Exposure'!CX2="","","Complete"). In the position exposure sheet (first sheet) in row 2 there is formulas which cover a large part of that row essentially saying =IF(SUM(W7:W128)=0,"",W4) and W4=the date.


So to give you background, on the position exposure sheet every month there is a series of numbers inputted in a specific column which has a heading that is a date. So if its Jan 2012, there should be numbers underneath that column. Once there are, a formula in row 2 goes from Blank to a Date because now the cells equal more than 0. When that happens, the position exposure database sheet will go from "blank" to have the word Complete letting me know that the particular month has been done. Now could I prob remove that whole entire step and just do a VBA code from the position exposure tab - but I didn't because I just started with what I had originally.


To hopefully clarify if you look at the position exposure sheet Jan-12 and Dec-11 are displayed in w4 and x4 while those same dates are displayed in w2 and x2. Nov-11 is displayed in y4 but it is not displayed in y2 and that is because there are no numbers below that so therefore the total = 0 and so its blank. If there were numbers put there it would trigger y2 to = Nov-11 and then this would be reflected in the position exposure database tab underneath nov-11 it would have "complete" instead of blank.


So for the Test Run Sheet - it has those dates in 05 and p5. Say excel book "b" = Fund6 - there should be the world "complete" for the exact coordinates of the row of fund6 and the column of date of Jan-12 and Dec-11. For all other dates which are there, nothing should be there. So basically if the excel workbook "b" is really fund 6's position exposure, i want the "test run" work book to have completes in 06 and p6 which is one row under the dates which is the same row that the word fund6 is located. It wouldn't have any completes or anything in the other column dates, but if I had entered numbers in the excel work book "b" in the position exposure datasheet in the Nov-11 column then that would trigger the position exposure database sheet to have the word "complete" under the Nov-11 column, which than would trigger the hope to be VBA code to take that word complete and place it in the workbook "test run" under the Nov-11 column in the row where fund6 is.


I hope that makes more sense, but basically the excel work book "B" is really a hedge funds exposures and i called that hedge fund Fund6 in my "test" run sheet, when I do this for myself I will replace the names with actual real names, but changed it due to the fact they are real hedge funds.


The test run workbook hopefully will act as a master file so that all the position exposure workbooks can be looked it and examined as to whether or not a specific month has been completed and record that information of "complete" in the corresponding cell which goes in the test run work book under the column for that specific month and in the row that corresponds to the fund
 
Hi ,


Sorry if I am not able to understand what you have taken a lot of pains to explain !


From what I can understand , your TEST RUN workbook summarises everything ; so it has the names of all the funds , from Fund6 through Fund31 ; each of those funds will have its own workbook which will contain data pertaining to that fund.


As and when each fund workbook gets the word "Complete" under any particular date , in the "Position Exposure Database" tab , the macro should put in the TEST RUN workbook , the word "Complete" in the cell which is an intersection of the date column corresponding to the date under which the word "Complete" appeared in that fund's workbook , and the row for that particular fund.


To make the whole thing concrete , let us assume fund17 has the word "Complete" against the date 4/1/2012 ( mm/dd/yyyy ) , then in the TEST RUN workbook , the word "Complete" should appear in the cell L17.


Is that right ?


Narayan


P.S. How will the individual fund workbooks be named ? Are you following any convention ?
 
Hi Narayan - thank you so much for your patience! I started just really tinkering with the actual VBA code as I have not fully laid out concrete plans to the individual workbooks etc. I am up for any ideas on how to make this easier to solve. almost all workbooks which are like "b" are called "Fund Name Position Exposure" and "fund name" essentially changes for each Fund. Part of me wanted to do something where I have the filenames inside the mastersheet so that the VBA code can reference the file names from there incase I add or change any (not sure how to even remotely do that). But say Cell c6 right now is "fund6" which is really talking about position exposure "b" if there was a convention to put the file name in A6 and have the VBA code read those file names to do the searches in. Incase I add or delete or change the names of the workbooks.


You are correct as to what you are saying though if I had a workbook for position exposure fund 17 and there were numbers in the column 4/1/2012 on the position exposure work sheet, the position exposure database work sheet would have a corresponding "complete" underneath the column which has 4/1/2012 in row 1. Then the test run workbook would have the word complete in Cell L17 - so yes you are correct.


Now You can make this more simple if you want, I am sure the whole position exposure work sheet to position exposure database worksheet is convoluted I mean realistically if I knew how to do just do it from the position exposure worksheet I wouldn't even need the position exposure database worksheet.


But to answer your question, yes you are correct as to what I am trying to say.
 
And to help you out with the stated purpose - i have 3 analyst who will do these position exposures every month - and there are many different excelwork books for position exposures for different funds. This master file will help me know which ones have been completed or not for what month which will make it easier than looking in each individual workbook to check if it has been completed.
 
Hi ,


Try the following :


1. Create a named range in your TEST RUN workbook as follows :


Name : Dates_Range

Refers To : =OFFSET(Monthlies!$D$5,0,0,1,COUNTA(Monthlies!$5:$5)-2)


This is basically to keep track of how many months we need to check out in the individual fund files. The -2 at the end of the formula is because though the dates start from column D ( cell D5 ) , the cells B5 and C5 are not blank ; cell A5 is blank ; if you enter something in A5 , then this -2 will have to be changed to -3.


2. In the same workbook , in cell D6 , put in the following formula :


=IFERROR(INDEX(OFFSET(INDIRECT("'[&$C6&.xlsx]Position Exposure Database'!$A$2"),0,0,1,COLUMNS(Dates_Range)),MATCH(D$5,OFFSET(INDIRECT("'[&$C6&.xlsx]Position Exposure Database'!$A$1"),0,0,1,COLUMNS(Dates_Range)),0)),"")


Copy this to all the other relevant cells.


The only other thing you need to do is to ensure that all the individual fund files are named according to whatever text you have entered in column C ( cells C6 downwards ) in TEST RUN.


Narayan
 
hi narayan - thanks for the help - i have used the iferror index thing in the past - i've been trying to do VBA to extent my horizons so to speak - but this would work for general purposes for this type of issue!
 
narayan - was wondering if I could ask you one more question - just let me know if you are available I can send you my email address?
 
Back
Top