Im a very novice VBAer so please forgive me for asking this, but i cannot find the answers no matter how much searching and experimenting ive done.
I am hoping to create a macro that will allow the user to match values between workbooks and update the matching data from one workbook to the other. For example, in Workbook1, column1 has values and column2 has data. In Workbook2 I would have values in column1, but no data. I would like the macro to update the data from Workbook1 column2 into Workbook2 column2. I have been able to do this using a simple VLOOKUP function, but I am not able to find how to update the macro to ask which two open workbooks and sheets to run the macro on. I am hoping to get it flexible enough to display an input box asking the user to choose which workbook/sheets to reference from, and which workbook/sheets to update the info into.
I have also not been able to figure out how to get the macro to stop when it sees a blank cell. The code i have just runs until row 5000, which is ok but I am hoping to find a better solution.
Here is the code i currently have:
Sub Match_Update()
' Match_Update
' Match 1st column and update column to right
'
'
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A5000")
End Sub
I have tried to change the 'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"' to reference objects as the table_array, but have not had much luck.
Any suggestions would be much appreciated!!!
Related
I am attempting to efficiently copy columns of data from one worksheet to a second worksheet in Excel using VBA, starting with a defined name for the column.
I am unsure why my code doesn't work. I cannot emphasis enough, how little I know about coding. I am attempting to teach myself VBA in order to manipulate vast quantities of data in Excel.
Function SortDataC()
'cuts and pastes columns from the unsorted worksheet to the sorted worksheet
Worksheets("UnsortedData").Range("DeltaModScore").Copy Destination:=Worksheets("SortedData").Columns(1)
End Function
DeltaModScore is the column header. If I look in defined named it is present on the sheet UnsortedData with workbook scope. Thus, I assume I have screwed up the syntax somewhere?
I have used the term Sheets("UnsortedData").Range..... as well as Worksheets.... as you see above. I've been basically searching the web for code examples and trying to get them to work with my data. Inevitably, I end up with errors I have much difficulty fixing. I hope this is something simple someone can point out.
You only require the single top left cell of a destination to complete a Copy & Paste.
SUB SortDataC()
'COPIES and pastes columns from the unsorted worksheet to the sorted worksheet
Worksheets("UnsortedData").Range("DeltaModScore").Copy _
Destination:=Worksheets("SortedData").Cells(1, "A")
End SUB
Functions are intended to return a value. If you simply want to complete an operation, a sub procedure is more appropriate.
I have data in Excel which is in table format, half of which (columns) contains data and the rest contains some formulas. I am writing some results on the data part, so I want to delete it before each update.
I tried to delete data using a VBA code, which contains similar piece of codes as below:
Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
However, it deletes the whole table. I tried various version of this code, but did not work. Also, I realized that, when the data is not in table format the macro works correctly. However, I need it to be a table to update the formulas in a dynamic range.
Does this error is related to table format & any ideas how can I fix it?
You need to use the .DataBodyRange property of the ListObjects object (this is what tables are called in VBA). The code below will clear the non formula elements of a table that is on the active sheet.
Sub ClearTableButLeaveFormulas()
With ActiveSheet.ListObjects(1)
.DataBodyRange.SpecialCells(xlCellTypeConstants).ClearContents
End With
End Sub
I got stuck at a problem with Excel VBA.
I am supposed to do the kinda easy task of copy/paste a variable range of cells from "sheet2" into the same range in "sheet1".
500 Rows like in my code is far too much, but I tried it this way, to "catch" the variable aspect.
The tricky part is, that the range in "sheet1" is a table(which gets created from TFS).
Sub CopyP()
Sheets("Sheet1").Range("B3:F500").Value = Sheets("Sheet2").Range("B3:F500").Value
SheetObject.ListObjects (ListObjectName)
Range("NAME OF TABLE[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
The [Iteration Path] is a column name of my table, i want to check in this column/with this parameter, if the "row" is empty.
I know the code is far from being good or clean but the table is giving me a hard time copying.
With this code I got another problem: the table gets created from TFS, no problem to copy into that, BUT the name of the table is variable(seems like TFS creates the name), so unless I put the name manually in the code, the "program" cant execute, because of missing range.
Didn't find a way to get a return of the table name somehow.
But I think I am just following the wrong path overall, maybe someone can be bring me on the right track.
My other Idea is to Iterate through the Rows in Sheet 2 to fetch just as much data is needed and then copy them with an iteration into the table. But i guess I would be the same problem with the table-name there.
Every information I find using google , talks about tables where the user can "name" the table. In my case I cant, so I have to work with the name TFS uses for my table.
Further to my comments below your question, I just typed this in notepad. Please amend it to suit your need.
This will give you the names of all tables in the activesheet. If there are multiple tables then you will get multiple names. (UNTESTED AS POSTING FROM PHONE)
Sub sample()
Dim objLB As ListObject, TableName As String
For Each objLB In ActiveSheet.ListObjects
TableName = objLB.Name
Exit For
Next
Range(TableName & "[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
I'm very new to this. However, I've created a small VBA script in Excel. This script highlights the row the button is on, asks for a name and then inserts today's date and the name entered on to some cells in the row. It's basically used to book out media that is recorded on this spreadsheet.
The issue I have is that I'll need a CommandButton for every row, and I have no idea how to get the command button to change the the row values within the script to reflect the row that the button is on. Is this possible?
I think you should specify your question more clearly, but if I understand your question right, you need to write data in the next empty cell. If that is the case, you can use this:
ActiveSheet.Range("B10").Select
If ActiveCell.Offset(1, 0) <> "" Then
Selection.End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
This will search for and select the next empty cell in column B, starting from B10. Hope this helps!
I have a question about freezing certain cells. But first let me explain the situation.
I have made a search box in my excel sheet and when you search for a letter or word; the results show up in cells below the search box. Now I want to freeze those cells, so that wherever I go in my sheet. I can always use the search box and see the results.
The cells for the searchbox and results are B2:B25. Those are the ones I want to freeze. Also the only sheet I want to use this on is the sheet "Reading". On the rest of my sheets I do not use a search function.
So my question(s) is : Do I need to put the code inside a module or on that sheet, and how do I do this?
Now I have tried the following
Range(Cells(2,2), Cells(25, 2)).Select
ActiveWindow(or maybe Reading?).FreezePanes = True
Inside a module. But it did not work and I do not know what else to do.
Any help is much appreciated! Since I am very new to VBA.
Almost there. Problem is that the 'range' and 'cells' needs to be directed to the 'Reading' sheet, like so:
Sheets("Reading").Range(Sheets("Reading").Cells(2,2), Sheets("Reading").Cells(25, 2)).Select
ActiveWindow.FreezePanes = True
but if it always is B2->B25, why not use:
Sheets("Reading").Range("B2:B25").Select
ActiveWindow.FreezePanes = True
This should work. Select is not very desirable, because it is very slow, but in this case, you need to (as far as I know).
EDIT
BTW, you can do this from within a code module or from within a sheet, but if you choose to do it from within a sheet, you cannot select another sheet. So just use the range.
EDIT 2
whoopsy, typo corrected. 'Sheet' should have been 'Sheets'