Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use column headers to select different ranges of cells to populate data from a filename
    primarykey
    data
    text
    <p>This is a separate question stemming from this post: <a href="https://stackoverflow.com/questions/11192167/how-to-use-the-filename-of-an-excel-file-to-change-a-column-of-cells">How to use the filename of an excel file to change a column of cells?</a></p> <p>I noticed that in the last post's code it was referencing specific cells (J2,K2). However when using the code, I came into an error when the columns changed. So now I am seeking a way to modify the below code to use the names of the header columns to populate the 2nd column instead of referencing specific cells. I think the only line that really needs adjusting is the myRng line, but I will provide all the code I am trying for reference.</p> <p>In case you don't read the other post, I will describe the issue. I am trying to fill in the 2nd column (name+type) based on the "name" column and the filename. When I was referencing the K or J row in the code, everything was working fine, but when I load a different file and the columns positions have changed, everything gets messed up.</p> <p>I need to populate the 2nd column (name+type) to be the exactly the same number or rows as the 1st column (name) which is why I am using the Range ("K2:K" &amp; lastCell) formula.</p> <p>Is there a way to do this?</p> <p>Current Attempted VBA code:</p> <pre><code>' Insert Column after name and then rename it name+type Rows(1).Find("name").Offset(0, 1).EntireColumn.Insert Rows(1).Find("name").Offset(0, 1).FormulaR1C1 = "name+type" Dim myRng As Range Dim lastCell As Long Dim myOtherRange As Range Dim column2Range As Range myOtherRange = Rows(1).Find("name") column2Range = Rows(1).Find("name+type") lastCell = Range(myOtherRange).End(xlDown).Row Set myRng = Range("K2:K" &amp; lastCell) myOtherRange.FormulaR2C1 = "=LEFT(MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-1),5)&amp;RC[-1]" myOtherRange.FormulaR2C1.Select Selection.Copy myRng.Select ActiveSheet.Paste </code></pre> <p>First Draft VBA code:</p> <pre><code>' Insert Column after name and then rename it name+type Rows(1).Find("name").Offset(0, 1).EntireColumn.Insert Rows(1).Find("name").Offset(0, 1).FormulaR1C1 = "name+type" 'Add the contents to the name+type column Range("K2").Select ActiveCell.FormulaR1C1 = "=LEFT(MID(CELL(""filename"",RC[-1]),SEARCH(""["",CELL(""filename"",RC[-1]))+1,SEARCH(""]"",CELL(""filename"",RC[-1]))-SEARCH(""["",CELL(""filename"",RC[-1]))-1),5)&amp;RC[-1]" Range("K2").Select Selection.Copy Range("K2:K8294").Select ActiveSheet.Paste </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload